+0
Daily
5,723
stars
1,694
forks
238
watchers
86
prs
562
issues
36
releases
README
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:
gradle
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Starting with gradle 6.8 you can alternatively add jitpack.io repository to your settings.gradle:
gradle
dependencyResolutionManagement {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
If using gradle kotlin use line
gradle.kts
maven(url = "https://jitpack.io")
Add library to dependencies
gradle
dependencies {
implementation 'com.github.mik3y:usb-serial-for-android:3.12.0'
}
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.
xml
3. Use it! Example code snippet:
open device:
java
// Find all available drivers from attached devices.
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
List 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
java
port.write(request, WRITE_WAIT_MILLIS);
len = port.read(response, READ_WAIT_MILLIS);
or direct write + event driven read:
java
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:
java
port.close();
For a simple example, see
UsbSerialExamples
folder in this project.
See separate github project SimpleUsbTerminal
for a more complete example with:
Background service to stay connected while the app is not visible or rotating
Flow control
Probing for UnrecognizedTop issues
#503Error message "USB get_status request failed" after a few hours working fine44 comments · 2023-06-08#599Missing/Corrupt data with high baud rates.22 comments · 2024-10-07#560Host and Accessory communication(NOT AN ISSUE)17 comments · 2024-02-01#417React Native Adapter?17 comments · 2022-02-20#546OTG PD(Power Delivery) issue10 comments · 2023-11-19#399is it suitable for UNITY 8 comments · 2021-11-15#574Communication ISSUE6 comments · 2024-04-29#625net9.0-android binding library compatibility5 comments · 2025-05-21
Latest release
v3.12.0 3.12.0 2026-11-10features: - support CH34x RTS/CTS flow control _note: looks like the chip handles CTS, but not RTS_ fixes: - CH34x baud rate 307200 with lower deviation