Android异步USB读取ByteBuffer,但无法确定大小

发布于 2024-11-15 14:11:00 字数 1093 浏览 9 评论 0原文

我正在使用最近添加的 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 技术交流群。

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

发布评论

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

评论(2

执妄 2024-11-22 14:11:00

我不确定这是你的问题,但看起来你在处理端点 0 时必须做一些特殊的事情。来自 UsbRequest:

此类不支持端点零上的请求;使用 controlTransfer(int, int, int, int, byte[], int, int) 为相反,端点零请求。

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.

静赏你的温柔 2024-11-22 14:11:00

显然你不能。 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?

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