title: UsbDeviceConnection.requestWait() / UsbRequest.queue() stops communicating with device
body: <p>Anybody have any ideas as to why requestWait() or queue() might stop receiving information (payloads) from an external device (aggboard) after the device has been disconnected and then reconnected? If I use the bulktransfer() method (which is commented out) with otherwise the same code, the communication resumes, but bulktransfer() is dropping payloads, which is why we have tried refactoring to requestWait() and queue(). Any advice??</p>
  <pre><code>public class PayloadTask extends AsyncTask&lt;Payload, Void, Payload&gt; {
  
      private UsbInterface usbInterface;
      private UsbDeviceConnection usbConnection;
      private UsbEndpoint usbToAggBoard;
      private UsbEndpoint usbFromAggBoard;
      private AggBoard owner;
      private UsbRequest request = new UsbRequest();
  
      PayloadTask(AggBoard owner, UsbInterface intf, UsbDeviceConnection connection) {
          usbConnection = connection;
          usbInterface = intf;
          /*synchronized (intf)*/ {
              usbToAggBoard = intf.getEndpoint(1);
              usbFromAggBoard = intf.getEndpoint(0);
          }
          this.owner = owner;
      }
  
      @Override
      protected Payload doInBackground(Payload... payloads) {
  
          if (payloads != null) {
              if (payloads.length &gt; 1) {
                  Log.d(&quot;PayloadTask&quot;, &quot;There was more than one payload. Going to have to iterate.&quot;);
                  int i = 0;
                  try {
                      Thread.sleep(1);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
                  for (Payload payload : payloads) {
                      byte[] buffer = payload.getBytes();
  
                      while (AndroidUnityPlayerActivity.aggBoard == null)
                      {
                          try {
                              Log.d(&quot;PayloadTask&quot;, &quot;Waiting for AndroidUnityPlayerActivity.aggboard to not be null&quot;);
                              Thread.sleep(50);
                          } catch (InterruptedException e) {
                              e.printStackTrace();
                          }
                      }
  
                      ByteBuffer byteBuffer = ByteBuffer.wrap(buffer);
                      byteBuffer.rewind();
                      try {
                          request.initialize(usbConnection, usbToAggBoard);
                          if (!request.queue(byteBuffer, buffer.length)) {
                              throw new IOException(&quot;Error queueing USB request.&quot;);
                          }
                          usbConnection.requestWait();
                      } catch (IOException e) {
                          e.printStackTrace();
                      } finally {
                          if(usbConnection != null) {
                              int sent = request.hashCode();
                              request.close();
                          }
                          else{
                              Log.d(&quot;PayloadTask&quot;, &quot;Request not received by usbConnection&quot;);
                          }
                      }
  //  int sent = usbConnection.bulkTransfer(usbToAggBoard, buffer, buffer.length, 1500);
                      busyWaitMicros(20);
  
                      if (++i % 1024 == 0) {
                          Log.e(AndroidUnityPlayerActivity.TAG, new Integer(i / 1024 * 8).toString() + &quot;KB sent&quot;);
                      }
                      AggBoard.percentInstalled = i * 100 / payloads.length;
                     // Log.w(AndroidUnityPlayerActivity.TAG, new Float((float)i /payloads.length).toString());
  
                  }
                  Log.d(&quot;PayloadTask&quot;, &quot;Finished iterating over the payloads.&quot;);
                  // rk3188 doesn't seem to see the success/fail message.. so let's fake one :/
                  if (AggBoard.encryptedBytes != null) {
                      Log.d(&quot;PayloadTask&quot;, &quot;Aggboard.encryptedBytes was null. Creating some kind of success response payload??&quot;);
                      byte[] response = new byte [] { (byte)Payload.BOOTLOAD_STATUS,
                                                      (byte)BootloadStatusPayload.AGGREGATOR,
                                                      (byte)BootloadStatusPayload.SUCCESS,
                                                      0, 0, 0, 0,
                                                      (byte)Payload.AGGREGATOR_TAIL };
                      if (AggBoard.percentInstalled &lt; 100) {
                          response[2] = (byte)BootloadStatusPayload.ERROR ;
                      }
                      Log.d(&quot;PayloadTask&quot;, &quot;Creating bootload status SUCCESS task&quot;);
                      return Payload.CreatePayload(response);
                  }
              } else if (payloads.length == 1 &amp;&amp; payloads[0] != null) {
                  Log.d(&quot;PayloadTask&quot;, &quot;There was only one payload. Running it&quot;);
                  byte[] buffer = payloads[0].getBytes();
                  if (AndroidUnityPlayerActivity.aggBoard != null) {
                      Log.d(&quot;PayloadTask&quot;, &quot;Only one payload, beginning bulk transfer&quot;);
                      
                     /* if (Payload.START_SENDING_DATA == buffer[0]) {
                          AndroidUnityPlayerActivity.hasAggBoard = true;
                      }*/
                     
                      ByteBuffer byteBuffer = ByteBuffer.wrap(buffer);
                      byteBuffer.rewind();
                      try {
                          request.initialize(usbConnection, usbToAggBoard);
                          if (!request.queue(byteBuffer, buffer.length)) {
                              throw new IOException(&quot;Error queueing USB request.&quot;);
                          }
                          usbConnection.requestWait();
                      } catch (IOException e) {
                          e.printStackTrace();
                      } finally {
                          if(usbConnection != null) {
                              request.close();
                          }
                          else{
                              Log.d(&quot;PayloadTask&quot;, &quot;Request not received by usbConnection&quot;);
                          }
                      }
  //  usbConnection.bulkTransfer(usbToAggBoard, buffer, buffer.length, 1000);
                  }
                  else{
                      Log.d(&quot;PayloadTask&quot;, &quot;AndroidUnityPlayerActivity's aggboard was null. Can't perform bulk transfer of payload.&quot;);
                  }
              }
          } else {
              try {
                  Thread.sleep(5);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
  
          byte[] response = new byte[64];
          int result = 0;
          int tries = 0;
  
          while (result &lt; 1){
              if (tries &gt; 5) {
                  Log.d(&quot;PayloadTask&quot;, &quot;PayloadTask Timed Out.&quot;);
                  return null;
              } else if (isCancelled()) {
                  Log.w(&quot;PayloadTask&quot;, &quot;PayloadTask Cancelled!&quot;);
                  return null;
              }
  
              /*synchronized (usbConnection)*/
              //if (usbConnection != null) {
                  ByteBuffer byteBuffer = ByteBuffer.wrap(response);
                  byteBuffer.rewind();
                  try {
                      request.initialize(usbConnection, usbFromAggBoard);
                      if (!request.queue(byteBuffer, response.length)) {
                          throw new IOException(&quot;Error queueing USB request.&quot;);
                      }
                      usbConnection.requestWait();
                  } catch (IOException e) {
                      e.printStackTrace();
                  } finally {
                      if(usbConnection != null) {
                          result = request.hashCode();
                          request.close();
                          //result += 1;
                      }
                      else{
                          Log.d(&quot;PayloadTask&quot;, &quot;Request not received by usbConnection&quot;);
                      }
                  }
  //  result = usbConnection.bulkTransfer(usbFromAggBoard, response, response.length, 1000);
              tries += 1;
          }
          Log.i(&quot;PayloadTask&quot;, new Integer(response.length).toString() + &quot; bytes received: &quot;);
          Log.d(&quot;PayloadTask&quot;, &quot;doInBackground returning some payload from the aggboard.&quot;);
          return Payload.CreatePayload(response);
      }
  
      @Override
      protected void onPostExecute(Payload payload) {
          owner.handlePayload(payload);
      }
  
      @Override
      protected void onCancelled() {
          Log.d(&quot;PayloadTask&quot;, &quot;thread canceled&quot;);
          super.onCancelled();
      }
  
      @Override
      protected void onPreExecute() {
          Log.d(&quot;PayloadTask&quot;, &quot;started thread&quot;);
          if (!usbConnection.claimInterface(usbInterface, true)) {
                  Log.w(&quot;PayloadTask&quot;, &quot;Failed to claim interface!&quot;);
              }
          else{
              Log.w(&quot;PayloadTask&quot;, &quot;Claimed interface!&quot;);
          }
      }
  
      public static void busyWaitMicros(long micros){
          long waitUntil = System.nanoTime() + (micros * 1000);
          while(waitUntil &gt; System.nanoTime()){
  
          }
      }
  }
  </code></pre>