title: Sending and receiving data from an Android App via USB
body: <p>I am attempting to connect a PIC microprocessor to a tablet app via usb. A Send button should trigger the sending of a string consist of <code>"$$$"</code> to the PIC. By a process of elimination, it appears to work down to the "requestWait" line at the bottom. The button stays highlighted and the app appears to hang. I suspect that no answer is arriving for the requestWait and so it waits indefinitely. I would appreciate some help. I have looked at the AdbTest and Missile Launcher apps but there is too much going on in them for my level of Android expertise.</p>
  
  <p>Added later:</p>
  
  <p>Looking at the code for <code>UsbRequest queue(..)</code>, <code>result = native_queue_direct(...)</code>.</p>
  
  <p>I gather that this is native code. Where do I find it please?</p>
  
  <pre><code>import java.nio.ByteBuffer;
  import java.util.HashMap;
  import java.util.Iterator;
  
  import android.content.Context;
  import android.hardware.usb.UsbDevice;
  import android.hardware.usb.UsbDeviceConnection;
  import android.hardware.usb.UsbInterface;
  import android.hardware.usb.UsbManager;
  import android.hardware.usb.UsbRequest;
  import android.view.View;
  import android.widget.Toast;
  
      @Override
      protected void onResume() {
          super.onResume();
  
          mManager = (UsbManager) getSystemService(Context.USB_SERVICE);
  
          // check for existing devices
          UsbDevice device;
  
          HashMap&lt;String, UsbDevice&gt; deviceList = mManager.getDeviceList();
          Iterator&lt;UsbDevice&gt; deviceIterator = deviceList.values().iterator();
          while (deviceIterator.hasNext()) {
              device = deviceIterator.next();
              if (device.getVendorId() == 1027) {
                  UsbInterface intf = device.getInterface(0);
                  if (setUsbInterface(device, intf)) {
                      break;
                  }
              }
          }
  
          private UsbManager          mManager;
          private UsbDevice           mDevice;
          private UsbDeviceConnection mDeviceConnection;
          private UsbInterface        mInterface;
  
          private ScorerDevice        mScorerDevice ;
  
          // ***********************************
  
          // Sets the current USB device and interface
          private boolean setUsbInterface(UsbDevice device, UsbInterface intf) {
              if (mDeviceConnection != null) {
                  if (mInterface != null) {
                      mDeviceConnection.releaseInterface(mInterface);
                      mInterface = null;
                  }
                  mDeviceConnection.close();
                  mDevice = null;
                  mDeviceConnection = null;
              }
  
              if (device != null &amp;&amp; intf != null) {
                  UsbDeviceConnection connection = mManager.openDevice(device);
                  if (connection != null) {
                      if (connection.claimInterface(intf, true)) {
  
                          toast = Toast.makeText(getApplicationContext(), "claim interface succeeded", Toast.LENGTH_SHORT);
                          toast.show();
  
                          mDevice = device;
                          mDeviceConnection = connection;
                          mInterface = intf;
                          mScorerDevice = new ScorerDevice(this, mDeviceConnection, intf);
  
                          toast = Toast.makeText(getApplicationContext(), "USB started", Toast.LENGTH_SHORT);
                          toast.show();
  
                          mScorerDevice.start();
                          return true;
                      } else {
  
                          toast = Toast.makeText(getApplicationContext(), "claim interface failed", Toast.LENGTH_SHORT);
                          toast.show();
  
                          connection.close();
                      }
                  } else {
  
                      toast = Toast.makeText(getApplicationContext(), "USB failed", Toast.LENGTH_SHORT);
                      toast.show();
                  }
              }
  
              if (mDeviceConnection == null &amp;&amp; mScorerDevice != null) {
                  mScorerDevice.stop();
                  mScorerDevice = null;
              }
              return false;
          }
  
          // ***********************************
  
          public int onClick_Send(View view) {
  
              String dollars = "$$$";
              byte[] bytes = dollars.getBytes();
  
              int len = bytes.length;
              int offset = 0;
  
              int PacketSize = mScorerDevice.mEndpointOut.getMaxPacketSize();
  
              if ((len &gt; PacketSize) || (offset &lt; 0) || (len &lt; 0) || ((offset + len) &gt; bytes.length))
                  throw new IndexOutOfBoundsException();
  
              ByteBuffer sendBuffer = ByteBuffer.allocate(PacketSize);
  
              UsbRequest request = new UsbRequest();
              Boolean ret = request.initialize(mDeviceConnection, mScorerDevice.mEndpointOut);
              if (!ret ) {
                  return -1;
              }
  
              ret = request.queue(sendBuffer, len);
              if (!ret ) {
                  return -1;
              }
  
              request = mDeviceConnection.requestWait();
              if (request == null) {
                  return -1;
              }
  
          }        
  </code></pre>