Android异步USB读取ByteBuffer,但无法确定大小
我正在使用最近添加的 Android USB API 从 Android 3.1 操作系统(在 Motorola Xoom 上)上基于 FTDI 的 USB 设备读取字节: http://developer.android.com/guide/topics/usb/host.html
但是,我在进行异步读取时遇到了问题。
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);
}
该函数从设备正确读取字节。但是,它没有告诉我阅读了多少。 bb.position() 始终返回 0,bb.remaining() 始终返回 128。我无法确保我的读取将返回 128 字节(可能读取,例如请求设备的状态,仅发回大约 12 字节),并且我有无法确定读取在哪里结束以及 ByteBuffer 中的垃圾从哪里开始。我尝试倒回 ByteBuffer 但读取时未设置标记。有什么方法可以准确确定读取了多少字节?
谢谢
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 ByteBuffer 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定这是你的问题,但看起来你在处理端点 0 时必须做一些特殊的事情。来自 UsbRequest:
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:
显然你不能。 Android USB 支持的一个缺点。 :(
请参阅如何确定 UsbRequest.queue(..) 方法接收到的字节长度?
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?