1

Some background: I have been trying to refactor this Android app's Usb system. Initially, in order to transfer data between endpoints, they used the UsbDeviceConnection.bulkTransfer() method. I refactored it to use the UsbRequest.initialize(), UsbRequest.queue(), and then UsbDeviceConnection.requestWait() methods. When the Android program is booted on the tablet, my new methods work fine (perhaps even better), but when there is a USB disruption/disconnection from the external device, once reconnected, it won't pick up where it left off. However, their old method, UsbDeviceConnection.bulkTransfer() DOES pick up communication upon reconnect.

Here is the code I refactored, along with the original method:

Original Method [bulkTransfer()] -

 byte[] response = new byte[64];
        int result = 0;
        int tries = 0;

        while (result < 1){
            if (tries > 5) {
                Log.d("PayloadTask", "PayloadTask Timed Out.");
                return null;
            } else if (isCancelled()) {
                Log.w("PayloadTask", "PayloadTask Cancelled!");
                return null;
            }

            /*synchronized (usbConnection)*/
           
               result = usbConnection.bulkTransfer(usbFromAggBoard, response, response.length, 25);
               
            tries += 1;

        }
        Log.i("PayloadTask", new Integer(response.length).toString() + " bytes received: ");
        Log.d("PayloadTask", "doInBackground returning some payload from the aggboard.");
        return Payload.CreatePayload(response);

Refactored Code [UsbRequest/requestWait()] -

byte[] response = new byte[64];
        int result = 0;
        int tries = 0;

        while (result < 1){
            if (tries > 5) {
                Log.d("PayloadTask", "PayloadTask Timed Out.");
                return null;
            } else if (isCancelled()) {
                Log.w("PayloadTask", "PayloadTask Cancelled!");
                return null;
            }

            /*synchronized (usbConnection)*/
            if (usbConnection != null) {
                UsbRequest request = new UsbRequest();
               
                ByteBuffer byteBuffer = ByteBuffer.wrap(response);
                byteBuffer.rewind();
                try {
                    request.initialize(usbConnection, usbFromAggBoard);
                    
                    if (!request.queue(byteBuffer, response.length)) {
                        throw new IOException("Error queueing USB request.");
                    }
                    usbConnection.requestWait();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if(usbConnection != null) {
                        result = request.hashCode();
                        request.close();
                  
                    }
                    else{
                        Log.d("PayloadTask", "Request not received by usbConnection");
                    }
                }
            }
            tries += 1;

        }
        Log.i("PayloadTask", new Integer(response.length).toString() + " bytes received: ");
        Log.d("PayloadTask", "doInBackground returning some payload from the aggboard.");
        return Payload.CreatePayload(response);

0 Answers0