如何确定 UsbRequest.queue(..) 方法接收的字节长度?
我在 Android 3.1 中遇到 UsbRequest 类的问题。
这是我的代码:
ByteBuffer buffer = ByteBuffer.allocate(4096);
buffer.order(ByteOrder.LITTLE_ENDIAN);
UsbRequest request = new UsbRequest();
request.initialize(mConnection, mEndpointIn);
request.queue(buffer, 4096);
if (mConnection.requestWait() == request) {
byte[] data = buffer.array();
}
数组data
的大小是4096,但真正接收到的字节长度要小得多。
如何确定实际接收到的字节大小?
谢谢。
I have troubles with UsbRequest class in Android 3.1.
This is my code:
ByteBuffer buffer = ByteBuffer.allocate(4096);
buffer.order(ByteOrder.LITTLE_ENDIAN);
UsbRequest request = new UsbRequest();
request.initialize(mConnection, mEndpointIn);
request.queue(buffer, 4096);
if (mConnection.requestWait() == request) {
byte[] data = buffer.array();
}
The size of array data
is 4096, but the length of really received bytes is much more smaller.
How can i determine the size of really received bytes?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是Android 中的一个错误。在受影响的版本上,没有解决方法,因为实现根本不传递长度。
它已在 JB-MR1(API 级别 17 及以上)中修复。
This was a bug in Android. On afflicted versions, there's no workaround, because the implementation simply doesn't pass the length up.
It was fixed in JB-MR1 (API level 17 onwards).
在我看来,当前的异步USB API没有办法返回读取的大小。 2 个“解决方法”使用同步传输,因为您会收到读/写的字节数,或者您正在实现的协议可能会向您发送您将收到的字节数。例如,我目前正在实现一些功能,其中我收到的每个更高级别的数据包在数据包的前 4 个字节中都有字节数。根据这个数字我知道我是否必须进行多次读取。
It seems to me that the current asynchronous USB API has no way to return the read size. 2 "workarounds" use synchronous transfers as there you receive the number of bytes read/written or maybe the protocol you are implementing sends you the number of bytes you'll receive. E.g. i'm currently implementing something where every higher-level packet i receive has the number of bytes in the first 4 bytes of the packet. Based on this number i know if i have to do multiple reads.
您可以使用 request.queue(buffer, bufferLength); 。这应该可以解决你的问题。现在,您应该参考 android 文档,它有详细的文档并且很有帮助。
You can use
request.queue(buffer, bufferLength);
. This should solve your problem. Now, you should refer android documentation, it's well documented and helpful.我相信 buffer.limit() 应该返回接收到的字节数。那有用吗?
I believe
buffer.limit()
should return the number of received bytes. Does that work?