最近追加された Android 用の USB API を使用して、Android 3.1 OS (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 を巻き戻そうとしましたが、読み取りによってマークが設定されません。読み取られたバイト数を正確に判断する方法はありますか?
ありがとう