usb-serial-for-android

repository·master·Indexed 26 days ago

https://github.com/mik3y/usb-serial-for-android

A Java-based driver library for Android that enables communication with USB serial hardware, such as Arduinos, using Android USB Host Mode (OTG). It provides raw serial port access without requiring root or special kernel drivers. The library includes support for various drivers, including Prolific USB-to-serial chips, and allows for custom VID/PID mapping via UsbSerialProber.

Tokens
1.5K
Snippets
4
Records
6
Agent score
41%

What's inside usb-serial-for-android

  1. Configure USB Device Attachment Notifications

    master

    To ensure your app is notified when a USB device is attached, you must add a device_filter.xml file to your res/xml/ directory and configure your AndroidManifest.xml with an intent filter for USB_DEVICE_ATTACHED.

    <activity
        android:name="..."
        ...>
        <intent-filter>
            <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
        </intent-filter>
        <meta-data
            android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
            android:resource="@xml/device_filter" />
    </activity>
  2. Install usb-serial-for-android via JitPack

    master

    To use this library, add the JitPack repository to your project's build configuration and then add the library as a dependency.

    1. Add JitPack Repository

    For root build.gradle:

    allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }

    For Gradle 6.8+ using settings.gradle:

    dependencyResolutionManagement {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }

    For Gradle Kotlin DSL (build.gradle.kts):

    maven(url = "https://jitpack.io")

    2. Add Dependency

    Add the following to your dependencies block:

    dependencies {
        implementation 'com.github.mik3y:usb-serial-for-android:3.11.0'
    }
    dependencies {
        implementation 'com.github.mik3y:usb-serial-for-android:3.11.0'
    }
  3. Open and use a USB Serial Port

    master

    Follow these steps to find, open, and communicate with a USB serial device using the library's API.

    1. Find Drivers: Use UsbSerialProber.getDefaultProber().findAllDrivers(manager) to find available drivers.
    2. Open Connection: Get the UsbDevice from the driver and open a connection via UsbManager.openDevice(device).
    3. Open Port: Access the first port (usually index 0) and call port.open(connection).
    4. Configure Parameters: Set baud rate, data bits, stop bits, and parity using port.setParameters().
    5. Read/Write: Use port.write() and port.read() for direct communication, or use SerialInputOutputManager for event-driven reading.
    6. Close: Always call port.close() when finished.
    // 1. Find drivers
    UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
    List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager);
    if (availableDrivers.isEmpty()) return;
    
    // 2. Open connection
    UsbSerialDriver driver = availableDrivers.get(0);
    UsbDeviceConnection connection = manager.openDevice(driver.getDevice());
    if (connection == null) return;
    
    // 3. Open port and 4. Set parameters
    UsbSerialPort port = driver.getPorts().get(0);
    port.open(connection);
    port.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
    
    // 5. Direct read/write
    port.write(request, WRITE_WAIT_MILLIS);
    len = port.read(response, READ_WAIT_MILLIS);
    
    // OR Event-driven read
    usbIoManager = new SerialInputOutputManager(usbSerialPort, this);
    usbIoManager.start();
    port.write("hello".getBytes(), WRITE_WAIT_MILLIS);
    
    // 6. Close
    port.close();
  4. Probe for unrecognized USB devices with custom rules

    master

    If you need to support a device with a custom VID/PID that is not in the default driver list, you can create a custom UsbSerialProber using a ProbeTable. This allows you to map specific Vendor IDs (VID) and Product IDs (PID) to a specific driver class (e.g., FtdiSerialDriver.class).

    Note: As of v3.5.0, CDC/ACM devices are detected by USB interface types, so custom probers are often unnecessary for those devices.

    // Probe for our custom FTDI device, which use VID 0x1234 and PID 0x0001 and 0x0002.
    ProbeTable customTable = new ProbeTable();
    customTable.addProduct(0x1234, 0x0001, FtdiSerialDriver.class);
    customTable.addProduct(0x1234, 0x0002, FtdiSerialDriver.class);
    
    UsbSerialProber prober = new UsbSerialProber(customTable);
    List<UsbSerialDriver> drivers = prober.findAllDrivers(usbManager);