如何确定 UsbRequest.queue(..) 方法接收的字节长度?

发布于 2024-11-27 07:45:41 字数 453 浏览 2 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

灰色世界里的红玫瑰 2024-12-04 07:45:41

这是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).

裸钻 2024-12-04 07:45:41

在我看来,当前的异步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.

初雪 2024-12-04 07:45:41

您可以使用 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.

白首有我共你 2024-12-04 07:45:41

我相信 buffer.limit() 应该返回接收到的字节数。那有用吗?

I believe buffer.limit() should return the number of received bytes. Does that work?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文