Skip to content

Instantly share code, notes, and snippets.

@dinocore1
Created November 5, 2015 22:21
Show Gist options
  • Save dinocore1/567ac8af275aa017f7de to your computer and use it in GitHub Desktop.
Save dinocore1/567ac8af275aa017f7de to your computer and use it in GitHub Desktop.
ByteBuffer buff = ByteBuffer.allocateDirect(2);
buff.order(ByteOrder.LITTLE_ENDIAN);
res = LibUsb.controlTransfer(handle,
(byte) (LibUsb.ENDPOINT_IN | LibUsb.REQUEST_TYPE_VENDOR),
(byte) 51,
(short) 0,
(short) 0,
buff,
0);
assert (res == 2);
short protocolVersion = buff.getShort(0);
System.out.println("Android Accessory Mode protocol: " + protocolVersion);
res = sendStr(handle, 0, MANUFACTURER_NAME);
res = sendStr(handle, 1, MODEL_NAME);
res = sendStr(handle, 2, DESCRIPTION);
res = sendStr(handle, 3, VERSION);
res = sendStr(handle, 4, URI);
res = sendStr(handle, 5, SERIAL);
buff = ByteBuffer.allocateDirect(0);
res = LibUsb.controlTransfer(handle,
(byte) (LibUsb.ENDPOINT_OUT | LibUsb.REQUEST_TYPE_VENDOR),
(byte) 53,
(short) 0,
(short) 0,
buff,
0
);
static int sendStr(DeviceHandle handle, int index, String str) {
byte[] strdata = str.getBytes(Charsets.UTF_8);
ByteBuffer buff = ByteBuffer.allocateDirect(strdata.length);
buff.put(strdata);
/*
byte[] strdata = str.getBytes(Charsets.UTF_8);
ByteBuffer buff = ByteBuffer.allocateDirect(strdata.length + 1);
buff.put(strdata);
buff.put((byte)0);
*/
int res = LibUsb.controlTransfer(handle,
(byte) (LibUsb.ENDPOINT_OUT | LibUsb.REQUEST_TYPE_VENDOR),
(byte) 52,
(short) 0,
(short) index,
buff,
0
);
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment