I am reading bytes from an FTDI-based USB device on the Android 3.1 OS (on a Motorola Xoom) using the recently-added USB API for Android: http://developer.android.com/guide/topics/usb/host.html
However, I'm running into a problem when doing Asynchronous reads.
ByteBuffer bb = ByteBuffer.allocate(128);
UsbRequest request = new UsbRequest();
request.initialize(deviceConnection, deviceInterface.getEndpoint(0));
boolean sent = request.queue(bb, 128);
request = deviceConnection.requestWait();
if(request.getEndpoint() == deviceInterface.getEndpoint(0)) {
// Send a message to the Activity with the read array
Message msg = usbTest.handleAsyncMessage.obtainMessage();
msg.obj = bb.array()
usbTest.handleAsyncMessage.sendMessage(msg);
}
This function reads the bytes properly from the device. However, it does not tell me how much is read. bb.position() always returns 0 and bb.remaining() always returns 128. I cannot ensure that my read will return 128 bytes (may reads, such as requesting the device's status, only send back around 12 bytes), and I have no way of determining where the read ends and the garbage in the B开发者_高级运维yteBuffer starts. I've tried rewinding the ByteBuffer but the mark doesn't get set by the read. Is there any way to determine exactly how many bytes are read?
Thanks
I'm unsure this is your problem, but it looks like you have to do something special when dealing with endpoint 0. From the javadocs of UsbRequest:
Requests on endpoint zero are not supported by this class; use controlTransfer(int, int, int, int, byte[], int, int) for endpoint zero requests instead.
Apparently you can't. A shortcoming of Android's USB support. :(
See How can I determine the length of received bytes of UsbRequest.queue(..) method?
精彩评论