2019-11-15 23:09:41 +00:00
[![Actions Status ](https://github.com/mik3y/usb-serial-for-android/workflows/build/badge.svg )](https://github.com/mik3y/usb-serial-for-android/actions)
2019-10-12 09:37:35 +00:00
[![Jitpack ](https://jitpack.io/v/mik3y/usb-serial-for-android.svg )](https://jitpack.io/#mik3y/usb-serial-for-android)
2023-08-25 06:51:25 +00:00
[![Codacy ](https://app.codacy.com/project/badge/Grade/ef799bba8a7343818af0a90eba3ecb46 )](https://app.codacy.com/gh/kai-morich/usb-serial-for-android-mik3y/dashboard?utm_source=gh& utm_medium=referral& utm_content=& utm_campaign=Badge_grade)
2019-10-20 20:12:56 +00:00
[![codecov ](https://codecov.io/gh/mik3y/usb-serial-for-android/branch/master/graph/badge.svg )](https://codecov.io/gh/mik3y/usb-serial-for-android)
2019-10-12 09:37:35 +00:00
2019-10-06 08:19:04 +00:00
# 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) ](http://developer.android.com/guide/topics/connectivity/usb/host.html )
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
2021-04-26 06:04:00 +00:00
Java. You get a raw serial port with `read()` , `write()` , and [other functions ](https://github.com/mik3y/usb-serial-for-android/wiki/FAQ#Feature_Matrix ) for use with your own protocols.
2019-10-06 08:19:04 +00:00
## Quick Start
2019-10-06 08:31:34 +00:00
**1.** Add library to your project:
Add jitpack.io repository to your root build.gradle:
2019-10-12 09:37:35 +00:00
```gradle
2019-10-06 08:31:34 +00:00
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
```
2023-01-04 06:10:54 +00:00
2023-01-12 16:21:32 +00:00
Starting with gradle 6.8 you can alternatively add jitpack.io repository to your settings.gradle:
2023-01-04 06:10:54 +00:00
```gradle
dependencyResolutionManagement {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
```
2023-10-15 15:06:14 +00:00
If using gradle kotlin use line
```gradle.kts
maven(url = "https://jitpack.io")
```
2019-10-06 08:31:34 +00:00
Add library to dependencies
2019-10-12 09:37:35 +00:00
```gradle
2019-10-06 08:31:34 +00:00
dependencies {
2023-10-15 15:07:10 +00:00
implementation 'com.github.mik3y:usb-serial-for-android:3.7.0'
2019-10-06 08:31:34 +00:00
}
```
2019-10-06 08:19:04 +00:00
2019-10-26 19:32:28 +00:00
**2.** If the app should be notified when a device is attached, add
[device_filter.xml ](https://github.com/mik3y/usb-serial-for-android/blob/master/usbSerialExamples/src/main/res/xml/device_filter.xml )
to your project's `res/xml/` directory and configure in your `AndroidManifest.xml` .
2019-10-06 08:19:04 +00:00
```xml
< activity
android:name="..."
...>
2019-10-26 19:32:28 +00:00
< 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" />
2019-10-06 08:19:04 +00:00
< / activity >
```
2019-10-26 19:32:28 +00:00
**3.** Use it! Example code snippet:
2019-10-06 08:19:04 +00:00
2019-11-15 23:09:41 +00:00
open device:
2019-10-06 08:19:04 +00:00
```java
2019-10-26 19:32:28 +00:00
// 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;
}
2019-10-06 08:19:04 +00:00
2019-10-26 19:32:28 +00:00
// 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;
}
2019-10-06 08:19:04 +00:00
2019-10-26 19:32:28 +00:00
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);
```
2019-11-15 23:09:41 +00:00
then use direct read/write
2019-10-26 19:32:28 +00:00
```java
2019-11-15 23:09:41 +00:00
port.write(request, WRITE_WAIT_MILLIS);
len = port.read(response, READ_WAIT_MILLIS);
2019-10-26 19:32:28 +00:00
```
2019-11-15 23:09:41 +00:00
or direct write + event driven read:
2019-10-26 19:32:28 +00:00
```java
2019-11-15 23:09:41 +00:00
usbIoManager = new SerialInputOutputManager(usbSerialPort, this);
2021-04-16 15:33:41 +00:00
usbIoManager.start();
2019-11-15 23:09:41 +00:00
...
port.write("hello".getBytes(), WRITE_WAIT_MILLIS);
2019-10-26 19:32:28 +00:00
@Override
public void onNewData(byte[] data) {
runOnUiThread(() -> { textView.append(new String(data)); });
2019-10-06 08:19:04 +00:00
}
```
2019-11-15 23:09:41 +00:00
and finally:
2019-10-26 19:32:28 +00:00
```java
port.close();
```
2019-10-06 08:19:04 +00:00
2019-10-26 19:32:28 +00:00
For a simple example, see
[UsbSerialExamples ](https://github.com/mik3y/usb-serial-for-android/blob/master/usbSerialExamples )
folder in this project.
2019-10-06 08:19:04 +00:00
2020-03-23 19:11:39 +00:00
For a more complete example with background service to stay connected while
the app is not visible or rotating, see separate github project
2019-10-26 19:32:28 +00:00
[SimpleUsbTerminal ](https://github.com/kai-morich/SimpleUsbTerminal ).
2019-10-06 08:19:04 +00:00
## 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
2023-03-11 18:12:42 +00:00
uses USB interface types and the built-in list of well-known VIDs and PIDs that
are supported by our drivers.
2019-10-06 08:19:04 +00:00
To use your own set of rules, create and use a custom prober:
```java
2023-03-11 18:12:42 +00:00
// Probe for our custom FTDI device, which use VID 0x1234 and PID 0x0001 and 0x0002.
2019-10-06 08:19:04 +00:00
ProbeTable customTable = new ProbeTable();
2023-03-11 18:12:42 +00:00
customTable.addProduct(0x1234, 0x0001, FtdiSerialDriver.class);
customTable.addProduct(0x1234, 0x0002, FtdiSerialDriver.class);
2019-10-06 08:19:04 +00:00
UsbSerialProber prober = new UsbSerialProber(customTable);
List< UsbSerialDriver > drivers = prober.findAllDrivers(usbManager);
// ...
```
2023-03-11 18:12:42 +00:00
*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.
2019-10-06 08:19:04 +00:00
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:
2020-08-24 15:32:03 +00:00
* FTDI FT232R, FT232H, FT2232H, FT4232H, FT230X, FT231X, FT234XD
2019-10-06 08:19:04 +00:00
* Prolific PL2303
2023-10-02 06:29:22 +00:00
* Silabs CP2102, CP210*
2022-07-22 06:12:37 +00:00
* Qinheng CH340, CH341A, CH9102
2019-10-06 08:19:04 +00:00
2023-10-02 06:29:22 +00:00
devices implementing the CDC/ACM protocol like
2019-10-06 08:19:04 +00:00
* Arduino using ATmega32U4
* Digispark using V-USB software USB
* BBC micro:bit using ARM mbed DAPLink firmware
* ...
2023-10-02 06:29:22 +00:00
and some device specific drivers:
* GsmModem devices, e.g. for Unisoc based Fibocom GSM modems
* Chrome OS CCD (Closed Case Debugging)
2019-10-06 08:19:04 +00:00
## Help & Discussion
2021-04-26 06:04:00 +00:00
For common problems, see the [FAQ ](https://github.com/mik3y/usb-serial-for-android/wiki/FAQ ) wiki page.
2019-10-06 08:19:04 +00:00
2019-10-12 09:37:35 +00:00
Are you using the library? Add your project to
2019-10-06 08:19:04 +00:00
[ProjectsUsingUsbSerialForAndroid ](https://github.com/mik3y/usb-serial-for-android/wiki/Projects-Using-usb-serial-for-android ).