6

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.

Slava
  • 96
  • 1
  • 3

4 Answers4

5

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

mik3y
  • 4,214
  • 1
  • 22
  • 29
3

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.

Maik
  • 3,419
  • 1
  • 23
  • 34
1

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

Arpan
  • 613
  • 7
  • 18
  • The link is broken and @Slava is already using that method anyway – miniBill Jun 03 '15 at 09:16
  • I had answered this question back in Oct 31 '12, back then this link was working fine and to the best of my knowledge I answered it when API 16 was in effect. Btw, it's a blocking queue it'll wait till bufferLength amount is available. – Arpan Jun 04 '15 at 07:15
  • You should fix your link – miniBill Jun 04 '15 at 09:03
0

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

Femi
  • 64,273
  • 8
  • 118
  • 148
  • Hello, No. In my case the limit is 4096. It is the limit of buffer. – Slava Aug 02 '11 at 17:06
  • Ah. Weird. Try `buffer.position()`? My understanding was that `limit` was not the same as `capacity`, but I guess I was wrong. – Femi Aug 02 '11 at 17:08
  • No. Position is `0`. Here all vars: _elementSizeShift=0, capacity=4096,effectiveDirectAddress=0, limit = 4096, mark = -1, offset=0, position =0 – Slava Aug 02 '11 at 17:19