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

I am having problems with the 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 data array is 4096 in size, but the bytes actually received are much shorter.

How to determine the size of actually received bytes?

Thanks.

+6
source share
4 answers

It was a bug in Android . There is no workaround in scary versions, because the implementation simply does not miss the length.

It has been fixed in JB-MR1 (starting at API level 17).

+5
source

It seems to me that the current asynchronous USB interface does not have the ability to return the read size. The 2 "workarounds" use simultaneous translations, since there you get the number of bytes read / written, or maybe the protocol you implement sends you the number of bytes you get. For instance. I am currently implementing something where each higher level packet that I receive has the number of bytes in the first 4 bytes of the packet. Based on this issue, I know if I need to do a few readings.

+2
source

You can use request.queue(buffer, bufferLength); . This should solve your problem. Now you should turn to the android documentation, well documented and useful.

+1
source

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

0
source

Source: https://habr.com/ru/post/894126/


All Articles