Reputation: 31
In Android Open Access Protocol 2.0, the section titled HID Support:
The AOA 2.0 protocol adds four new USB control requests to allow the accessory to act as one or more HID input devices to the Android device. Since HID support is done entirely through control requests on endpoint zero, no new USB interface is needed to provide this support.
Is that mean that, the Android device can act as a usb hid mouse?
I get a example for Arduino in https://github.com/YuuichiAkagawa/Arduino-AOA2/blob/master/AOA2/examples/AOA2HID/AOA2HID.ino.
And write a demo show as below. but it doesn't work. Whether a android device can become a usb hid mouse as the Arduino example do? Thanks!
private void initIdentity(UsbDeviceConnection connection) {
Log.i("initAccessory", "initStringControlTransfer");
initStringControlTransfer(connection, 2,
"showcasing android2android USB communication"); // DESCRIPTION
initStringControlTransfer(connection, 3, "0.1"); // VERSION
initStringControlTransfer(connection, 4, "http://minji.cn"); // URI
initStringControlTransfer(connection, 5, "42"); // SERIAL
}
private void initStringControlTransfer(
final UsbDeviceConnection deviceConnection, final int index,
final String string) {
deviceConnection.controlTransfer(0x40, 52, 0, index, string.getBytes(),
string.length(), USB_TIMEOUT_IN_MS);
}
private int HID_ID = 0;
private int GET_PROTOCOL = 51;
private int ACCESSORY_REGISTER_HID = 54;
private int ACCESSORY_UNREGISTER_HID = 55;
private int ACCESSORY_SET_HID_REPORT_DESC = 56;
private int ACCESSORY_SEND_HID_EVENT = 57;
char[] _hidReportDescriptor = {
// Mouse
0x05, 0x01, 0x09, 0x02, 0xA1, 0x01, 0x09, 0x01, 0xA1,
0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x08, 0x15, 0x00, 0x25, 0x01,
0x95, 0x08, 0x75, 0x01, 0x81, 0x02, 0x95,
0x00, 0x81, 0x03, 0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 0x09,
0x38, 0x15, 0x81, 0x25, 0x7F, 0x75, 0x08, 0x95, 0x03,
0x81, 0x06, 0x05, 0x0C, 0x0A, 0x38, 0x02, 0x95, 0x01,
0x81, 0x06, 0xC0, 0xC0, };
private void initUsbHid(UsbDeviceConnection connection) {
connection.controlTransfer(REQUEST_OUT_TYEP, ACCESSORY_REGISTER_HID,
HID_ID, _hidReportDescriptor.length, new byte[] {}, 0,
USB_TIMEOUT_IN_MS);
connection.controlTransfer(REQUEST_OUT_TYEP,
ACCESSORY_SET_HID_REPORT_DESC, HID_ID, 0, _hidReportDescriptor.toString().getBytes(),
0, USB_TIMEOUT_IN_MS);
}
private void unRegisteHid() {
UsbDeviceConnection connection = mUsbManager.openDevice(mUsbDevice);
connection.controlTransfer(REQUEST_OUT_TYEP, ACCESSORY_UNREGISTER_HID,
HID_ID, 0, new byte[] {}, 0, USB_TIMEOUT_IN_MS);
}
public void send(String msg) {
Log.i(this.getClass().getName(), msg);
UsbDeviceConnection connection = mUsbManager.openDevice(mUsbDevice);
byte[] m = new byte[5];
m[2] = 0;
m[1] = 0;
m[0] = 1;
connection.controlTransfer(REQUEST_OUT_TYEP, ACCESSORY_SEND_HID_EVENT,
HID_ID, 0, m, m.length, USB_TIMEOUT_IN_MS);
}
private void startAccessory(UsbDeviceConnection connection) {
connection.controlTransfer(REQUEST_OUT_TYEP, // request type for this
// transaction
53, // request ID for this transaction
0, // value field for this transaction
0, // index field for this transaction
new byte[] {}, // buffer for data portion of transaction, or
// null if no data needs to be sent or received
0, // the index of the first byte in the buffer to send or
// receive
USB_TIMEOUT_IN_MS);
}
private boolean initAccessory(final UsbDevice device) {
Log.i("initAccessory", "initAccessory start");
final UsbDeviceConnection connection = mUsbManager.openDevice(device);
if (connection == null) {
Log.i("initAccessory", "connection == null");
return false;
}
int protocol = getProtocol(connection);
if (protocol <= 1) {
Log.i("initAccessory", "Protocol: " + protocol
+ " not support USB HID");
return false;
}
//initIdentity(connection);
initUsbHid(connection);
//startAccessory(connection);
connection.close();
return true;
}
Upvotes: 2
Views: 2379
Reputation: 71
AOA 2.0 HID class implementation allows an Accessory attached to the Android phone to act as an HID device. But not the other way around.
Check out Android Keyboard Key Assignments here. I am not sure if they support HID Page for Mouse. I have tested Keyboard Page and Consumer Page, and found them working.
I found your Accessory ID is 0 (private int HID_ID = 0;), Maybe use something else. I am not sure if they enumerate a device with ID 0.
Also try to send a release key after few ms, where all the data is zero. I am not sure what is the release key for mouse.
Upvotes: 1