2

我正在使用最近添加的适用于 Android 的 USB API 从 Android 3.1 操作系统(在摩托罗拉 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 但标记没有被读取设置。有什么方法可以准确地确定读取了多少字节?

谢谢

4

2 回答 2

0

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.

于 2011-06-14T23:53:43.533 回答
0

显然你不能。Android 的 USB 支持的一个缺点。:(

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

于 2012-02-15T21:05:23.640 回答