Communication between Android and USB devices

Asked

Viewed 3,428 times

19

I’m developing an application for Android that communicates with a USB device, a printer, an Arduino or another Android device.

Is it possible to read and write on the Android USB port? How should I proceed with the drivers? Does Android have libraries to work with USB devices? I’ve heard of a project called FTDI Chip to communicate with a USB device, I am on the right track?

  • If you plug an Android into a PC, you can read and write to its internal memory. Also the device itself usually allows you to choose the way it communicates with the PC. Which you can read and write on the door, that’s for sure - it’s not just for feeding the device. Drivers vary from manufacturer to manufacturer, but the API is one.

  • Here is the official documentation. If anyone wants to elaborate an answer on that, welcome.

1 answer

9


There is a project on Github usb-serial-for-android

It is a library to communicate with Arduinos and other devices via USB on Android, using the Android USB Host API (Android 3.1+)

You don’t need access to Root because it implements the drives in Java itself, which provides an api for Android to receive (read) and send (write) to USB.

Follow the project link: https://github.com/mik3y/usb-serial-for-android


Code snippet, see more in sample project (https://github.com/mik3y/usb-serial-for-android/tree/master/UsbSerialExamples)

// Encontra o UsbManager do Android.
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);

// Encontra o primeiro Driver disponivel.
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();
  } 
}

Not all are flowers, because the project is still in version v0.1.0 and the last time it was updated was in 2012. At least the project be well documented.

I hope I’ve helped you.

  • 2

    Gabriel Santos his response was very constructive I will study this project, thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.