title: Android USB Host not able to read and write data for Cp2102SerialDriver
body: <p>I am working on Android host application where I need to read data from all attached device. Using usb to serial cable "Cp2102SerialDriver".</p>
  
  <p>Here is the example link which I referring </p>
  
  <p><a href="http://code.google.com/p/usb-serial-for-android/" rel="nofollow">http://code.google.com/p/usb-serial-for-android/</a></p>
  
  <p>I am successfully able to display attached devices with vendor and product ID, here is the sample code</p>
  
  <pre><code>/** Simple container for a UsbDevice and its driver. */
  private static class DeviceEntry {
      public UsbDevice device;
      public UsbSerialDriver driver;
  
      DeviceEntry(UsbDevice device, UsbSerialDriver driver) {
          this.device = device;
          this.driver = driver;
      }
  }
  
  final List&lt;DeviceEntry&gt; result = new ArrayList&lt;DeviceEntry&gt;();
              for (final UsbDevice device : mUsbManager.getDeviceList()
                      .values()) {
  
                  final List&lt;UsbSerialDriver&gt; drivers = UsbSerialProber
                          .probeSingleDevice(mUsbManager, device);
  
                  Log.d(TAG, "Found usb device: " + device);
                  if (drivers.isEmpty()) {
                      Log.d(TAG, "  - No UsbSerialDriver available.");
                      result.add(new DeviceEntry(device, null));
                  } else {
                      for (UsbSerialDriver driver : drivers) {
                          Log.d(TAG, "  + " + driver);
                          result.add(new DeviceEntry(device, driver));
                      }
                  }
  </code></pre>
  
  <p>I refer to the android host tutorial </p>
  
  <p><a href="http://developer.android.com/guide/topics/connectivity/usb/host.html" rel="nofollow">http://developer.android.com/guide/topics/connectivity/usb/host.html</a></p>
  
  <p>to communicate with the devices </p>
  
  <pre><code>mListView.setOnItemClickListener(new ListView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                  int position, long id) {
              Log.d(TAG, "Pressed item " + position);
              if (position &gt;= mEntries.size()) {
                  Log.w(TAG, "Illegal position.");
                  return;
              }
  
              final DeviceEntry entry = mEntries.get(position);
  
  
              if (entry.device != null) {
  
                  IntentFilter intentfilter = new IntentFilter(
                          "com.android.example.USB_PERMISSION");
                  DeviceListActivity.this.registerReceiver(
                          mUsbReceiverPermission, intentfilter);
  
                  Toast.makeText(DeviceListActivity.this,
                          " Registered for broadcast reciever ",
                          Toast.LENGTH_SHORT).show();
                  sendData();
  
              }
  
  
          }
      });
  
  
  private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
  private final BroadcastReceiver mUsbReceiverPermission = new BroadcastReceiver() {
  
      public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();
  
          Toast.makeText(getApplicationContext(), action, Toast.LENGTH_SHORT)
                  .show();
          if (ACTION_USB_PERMISSION.equals(action)) {
              synchronized (this) {
                  UsbDevice device = (UsbDevice) intent
                          .getParcelableExtra(UsbManager.EXTRA_DEVICE);
  
                   if (intent.getBooleanExtra(
                  UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                  if (device != null) {
                      usbDeviceConnection = mUsbManager.openDevice(device);
                      cp2102SerialDriver = new Cp2102SerialDriver(device,
                              usbDeviceConnection);
                      try {
                          cp2102SerialDriver.open();
  
                          Toast.makeText(getApplicationContext(),
                                  "cp2102SerialDriver is open successfully",
                                  Toast.LENGTH_SHORT).show();
  
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
  
                  }
                   } else {
                   Log.d(TAG, "permission denied for device " + device);
  
                   }
              }
          }
      }
  };
  </code></pre>
  
  <p>To write data I am using this code</p>
  
  <pre><code>String data = etText.getText().toString() + "/n";
  
                  bytes = data.getBytes(Charset.forName("UTF-8"));
                  try {
                      cp2102SerialDriver.write(bytes, 3000);
                  } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
              }
  </code></pre>
  
  <p>But while writing I am getting exception. Thanks in advance for any help here.</p>