Slava
Slava

Reputation: 96

How can I determine the length of received bytes of UsbRequest.queue(..) method?

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.

Upvotes: 6

Views: 4935

Answers (4)

Arpan
Arpan

Reputation: 623

You can use request.queue(buffer, bufferLength);. This should solve your problem. Now, you should refer android documentation, it's well documented and helpful.

Upvotes: 1

mik3y
mik3y

Reputation: 4284

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

Upvotes: 5

Maik
Maik

Reputation: 3549

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.

Upvotes: 3

Femi
Femi

Reputation: 64700

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

Upvotes: 0

Related Questions