Android UsbRequest.queue(ByteBuffer) ignores position, limit, etc.?

1.2k views Asked by At

I am currently experiencing problems when using UsbRequest.queue(ByteBuffer, int).

It seems to ignore the position of the ByteBuffer and always starts writing at position 0 in the buffer. It then sets the position of the buffer to the bytes read?

https://android.googlesource.com/platform/frameworks/base.git/+/a3665ba95d806fcb6780d29d49bd0f1032e8bc86%5E%21/#F0

How can that be, that this is just ignored? Any ideas on how to specify the start position in the ByteBuffer?

Thanks, Magnus

1

There are 1 answers

0
Magnus On BEST ANSWER

EDIT2: So what I am basically now doing is using a temporary buffer and then copy the data accordingly to the final buffer.

EDIT: This is not a solution! wrap just sets the position of the ByteBuffer to offset.

Okay I found a workaround using ByteBuffer#wrap(byte[] array, int offset, int length)

    int length = dest.remaining();

    // workaround: UsbRequest.queue always writes at position 0 :/
    ByteBuffer tmp = ByteBuffer.wrap(dest.array(), dest.position(), length);
    if(!inRequest.queue(tmp, length)) {
        throw new IOException("Error queueing request.");
    }

    UsbRequest request = deviceConnection.requestWait();
    if (request == inRequest) {
        dest.position(dest.position() + tmp.position());
        return tmp.position();
    }

    throw new IOException("requestWait failed! Request: " + request);