title: BulkTransfer & Android USB API
body: <p>I have a program in which I attempt to attach my android device to a webcam via USB. I'm having trouble with a few things, namely properly transferring data. I've tried using bulkTransfer and there seems to be no recognition of it being used. I've been trying to find examples that may assist me such as <a href="https://stackoverflow.com/questions/13982264/usb-bulktransfer-between-android-tablet-and-camera">here</a> but none are helping me - their structure seems to be better than mine but whenever I switch my program crashes on load. </p>
  
  <p>I'm fairly confident my bytes declaration is also incorrect and I should be somehow forwarding my data there, but I'm unsure how. Any help in terms of how to data transfer and how to structure my code would be appreciated.</p>
  
  <p>Some declarations:</p>
  
  <pre><code>private byte[] bytes = {1,2};
  private static int TIMEOUT = 0;
  private boolean forceClaim = true;
  </code></pre>
  
  <p>In On Create:</p>
  
  <pre><code>UsbDevice device = (UsbDevice) getIntent().getParcelableExtra(UsbManager.EXTRA_DEVICE);
      UsbManager mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
      HashMap&lt;String, UsbDevice&gt; deviceList = mUsbManager.getDeviceList();
      Iterator&lt;UsbDevice&gt; deviceIterator = deviceList.values().iterator();
      while(deviceIterator.hasNext()) {
          device = deviceIterator.next();
          PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this,0,new Intent(ACTION_USB_PERMISSION), 0);
          IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
          registerReceiver(mUsbReceiver, filter);
          mUsbManager.requestPermission(device, mPermissionIntent);
          UsbDeviceConnection connection = mUsbManager.openDevice(device);
          Log.d("CAM Connection", " " + connection);
          Log.d("CAM UsbManager", " " + mUsbManager);
          Log.d("CAM Device", " " + device);
  
  
  
          UsbInterface intf = device.getInterface(0);
          Log.d("CAM_INTF Interface!!!!", " " +  intf );
          UsbEndpoint endpoint = intf.getEndpoint(0);
          Log.d("CAM_END Endpoint", " " +  endpoint );
  
          connection.claimInterface(intf, forceClaim);
          StringBuilder sb = new StringBuilder();
          if(connection.bulkTransfer(endpoint,bytes,bytes.length,TIMEOUT) &lt; 2)
              Log.d("test", "");
         //Log.d("BULK", ""+ connection.bulkTransfer(endpoint, bytes, bytes.length, TIMEOUT)); //do in another thread
      }
  </code></pre>
  
  <p>Additional relevant code:    </p>
  
  <pre><code> private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
  
      public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();
          if (ACTION_USB_PERMISSION.equals(action)) {
              synchronized (this) {
                  UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
  
                  if (intent.getBooleanExtra(EXTRA_PERMISSION_GRANTED, false)) {
                      if(device != null){
  
  
  
                          //call method to set up device communication
                      }
                  }
                  else {
                      Log.d("Deny:", "permission denied for device " + device);
                  }
              }
          }
      }
  };
  </code></pre>