0

I have been trying to follow this tutorial to read data output from my arduino using my android phone, they are connected via OTG.

https://code.google.com/p/usb-serial-for-android/

I am able to print the result in a text view, however the output is 0. This is the code snippet I am using:

// Get UsbManager from Android.
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);

// Find the first available driver.
UsbSerialDriver driver = UsbSerialProber.acquire(manager);

if (driver != null) {
  driver.open();
  try {
    driver.setBaudRate(115200);

    byte buffer[] = new byte[16];
    int numBytesRead = driver.read(buffer, 1000);
    Log.d(TAG, "Read " + numBytesRead + " bytes.");
  } catch (IOException e) {
    // Deal with error.
  } finally {
    driver.close();
  } 
}

My error might be relating to driver.read, can't find much documentation on it. Any thoughts on how the data get's pulled from the Arduino through serial in this method? If not, better ways to do it?

Thanks! Arun

dsolimano
  • 8,870
  • 3
  • 48
  • 63
arun12345
  • 17
  • 5
  • First, this looks like primarily an Android development question, and (at least so far) only quite incidentally an Arduino one, so although an Arduino site exists, the question really is a better fit here on Stackoverflow rather than there. – Chris Stratton May 03 '14 at 04:14
  • You are calling the UsbSerialDriver.read() function with a timeout of 1 second. Perhaps you are getting 0 bytes read because no data has been received from the Arduino in that time period. Verify that the baud rate matches and try it with a sketch that generates a flood of continuous data. Also, at minimum put an e.printStackTrace() in your catch block - right now, **your empty catch block will hide any exceptions that occur**. – Chris Stratton May 03 '14 at 04:16

0 Answers0