• Stars
    star
    4,834
  • Rank 8,704 (Top 0.2 %)
  • Language
    Java
  • License
    MIT License
  • Created over 11 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Android USB host serial driver library for CDC, FTDI, Arduino and other devices.

Actions Status Jitpack Codacy codecov

usb-serial-for-android

This is a driver library for communication with Arduinos and other USB serial hardware on Android, using the Android USB Host Mode (OTG) available since Android 3.1 and working reliably since Android 4.2.

No root access, ADK, or special kernel drivers are required; all drivers are implemented in Java. You get a raw serial port with read(), write(), and other functions for use with your own protocols.

Quick Start

1. Add library to your project:

Add jitpack.io repository to your root build.gradle:

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

Starting with gradle 6.8 you can alternatively add jitpack.io repository to your settings.gradle:

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

Add library to dependencies

dependencies {
    implementation 'com.github.mik3y:usb-serial-for-android:3.5.1'
}

2. If the app should be notified when a device is attached, add device_filter.xml to your project's res/xml/ directory and configure in your AndroidManifest.xml.

<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>

3. Use it! Example code snippet:

open device:

    // Find all available drivers from attached devices.
    UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
    List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager);
    if (availableDrivers.isEmpty()) {
        return;
    }

    // Open a connection to the first available driver.
    UsbSerialDriver driver = availableDrivers.get(0);
    UsbDeviceConnection connection = manager.openDevice(driver.getDevice());
    if (connection == null) {
        // add UsbManager.requestPermission(driver.getDevice(), ..) handling here
        return;
    }

    UsbSerialPort port = driver.getPorts().get(0); // Most devices have just one port (port 0)
    port.open(connection);
    port.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);

then use direct read/write

    port.write(request, WRITE_WAIT_MILLIS);
    len = port.read(response, READ_WAIT_MILLIS);

or direct write + event driven read:

    usbIoManager = new SerialInputOutputManager(usbSerialPort, this);
    usbIoManager.start();
    ...
    port.write("hello".getBytes(), WRITE_WAIT_MILLIS);
    
@Override
public void onNewData(byte[] data) {
    runOnUiThread(() -> { textView.append(new String(data)); });
}

and finally:

    port.close();

For a simple example, see UsbSerialExamples folder in this project.

For a more complete example with background service to stay connected while the app is not visible or rotating, see separate github project SimpleUsbTerminal.

Probing for Unrecognized Devices

Sometimes you may need to do a little extra work to support devices which usb-serial-for-android doesn't (yet) know about -- but which you know to be compatible with one of the built-in drivers. This may be the case for a brand new device or for one using a custom VID/PID pair.

UsbSerialProber is a class to help you find and instantiate compatible UsbSerialDrivers from the tree of connected UsbDevices. Normally, you will use the default prober returned by UsbSerialProber.getDefaultProber(), which uses USB interface types and the built-in list of well-known VIDs and PIDs that are supported by our drivers.

To use your own set of rules, create and use a custom prober:

// 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);
// ...

Note: as of v3.5.0 this library detects CDC devices by USB interface types instead of fixed VID+PID, so custom probers are typically not required any more for CDC devices.

Of course, nothing requires you to use UsbSerialProber at all: you can instantiate driver classes directly if you know what you're doing; just supply a compatible UsbDevice.

Compatible Devices

This library supports USB to serial converter chips:

  • FTDI FT232R, FT232H, FT2232H, FT4232H, FT230X, FT231X, FT234XD
  • Prolific PL2303
  • Silabs CP2102 and all other CP210x
  • Qinheng CH340, CH341A, CH9102

and devices implementing the CDC/ACM protocol like

  • Arduino using ATmega32U4
  • Digispark using V-USB software USB
  • BBC micro:bit using ARM mbed DAPLink firmware
  • ...

Help & Discussion

For common problems, see the FAQ wiki page.

Are you using the library? Add your project to ProjectsUsingUsbSerialForAndroid.

More Repositories

1

django-db-multitenant

A simple multi-tenancy solution for Django apps.
Python
151
star
2

pymidi

Python library for building RTP-MIDI / AppleMIDI clients and servers
Python
47
star
3

airdash

A standalone, alternative ADS-B (readsb) web frontend
JavaScript
42
star
4

django-spicy-id

🌶️ Cool "Stripe-style" self-identifying row IDs for Django. A drop-in replacement AutoField.
Python
14
star
5

pylcdui

Python library for serial character LCD displays (MatrixOrbital, CrystalFontz, etc)
Python
12
star
6

flightmon

Simple command-line "GUI" for showing current dump1090/readsb data
Go
11
star
7

flyscoop

Mobile monitoring and management app for Fly.io.
JavaScript
6
star
8

django-simple-timeseries

A custom database field for recording simple and small timeseries vectors
Python
6
star
9

gatsby-starter-basic-bootstrap

A basic starter for GatsbyJS + Bootstrap 4
JavaScript
5
star
10

wireshark-nfc

Automatically exported from code.google.com/p/wireshark-nfc
C
5
star
11

git-split-commit

Easy command line UI for breaking one commit into two
Python
5
star
12

rtl-ais-docker

Shell
4
star
13

particle-heatpump

Firmware for Particle.io devices for controlling a Mitsubishi Heatpump
C++
2
star
14

jookytunes

A nice little karaoke player
JavaScript
2
star
15

radiotasty.com

The radiotasty.com homepage
SCSS
1
star
16

goadsb

ADS-B related utilities in go
Go
1
star
17

pyonewire

Automatically exported from code.google.com/p/pyonewire
Python
1
star
18

esp32-ble-pinpad

An ESPHome component for accepting a secret PIN over Bluetooth LE.
C++
1
star
19

git-split-commit-example-repo

An example for use with `git-split-commit`
HTML
1
star