Open source code in github
https://github.com/mik3y/usb-serial-for-android
Step 1: Create a new project or open an existing project
Step 2: In your manifest, add the USB permission
<uses-feature android:name="android.hardware.usb.host" />
<uses-permission android:name="android.permission.USB_PERMISSION"
/>
Step 3: Add the intent USB ATTACHED
<intent-filter>
<action
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
Step 4: Add the meta data for USB Driver
<meta-data
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />
Step 5: Create the xml folder, located under the res folder
Step 6: Create the xml file, named device_filter, located in the xml folder. Copy this content to the
device_filter.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 0x0403 / 0x6001: FTDI FT232R UART -->
<usb-device vendor-id="1027" product-id="24577" />
<!-- 0x0403 / 0x6015: FTDI FT231X -->
<usb-device vendor-id="1027" product-id="24597" />
<!-- 0x2341 / Arduino -->
<usb-device vendor-id="9025" />
<!-- 0x16C0 / 0x0483: Teensyduino -->
<usb-device vendor-id="5824" product-id="1155" />
<!-- 0x10C4 / 0xEA60: CP210x UART Bridge -->
<usb-device vendor-id="4292" product-id="60000" />
<!-- 0x067B / 0x2303: Prolific PL2303 -->
<usb-device vendor-id="1659" product-id="8963" />
<!-- 0x1a86 / 0x7523: Qinheng CH340 -->
<usb-device vendor-id="6790" product-id="29987" />
<usb-device vendor-id="1155" product-id="22352" />
<usb-device vendor-id="8208" product-id="30264" />
<usb-device vendor-id="3368" product-id="516" />
</resources>
Step 7: Open the build.graddle (module app) to add the usb to uart library
compile 'com.github.mik3y:usb-serial-for-android:2.+'
Step 8: Add the repository for the library
allprojects {
repositories {
maven { url 'https://jitpack.io'}
}
}
Step 9: Open your MainActivity.java for coding:
final String TAG = "MAIN_TAG";
private static final String ACTION_USB_PERMISSION =
"com.android.recipes.USB_PERMISSION";
private static final String INTENT_ACTION_GRANT_USB = BuildConfig.APPLICATION_ID +
".GRANT_USB";
UsbSerialPort port;
private void openUART(){
UsbManager manager = (UsbManager)
getSystemService(Context.USB_SERVICE);
List<UsbSerialDriver> availableDrivers =
UsbSerialProber.getDefaultProber().findAllDrivers(manager);
if (availableDrivers.isEmpty()) {
Log.d(TAG, "UART is not available");
}else {
Log.d(TAG, "UART is available");
UsbSerialDriver driver = availableDrivers.get(0);
UsbDeviceConnection connection =
manager.openDevice(driver.getDevice());
if (connection == null) {
PendingIntent usbPermissionIntent =
PendingIntent.getBroadcast(this, 0, new
Intent(INTENT_ACTION_GRANT_USB), 0);
manager.requestPermission(driver.getDevice(),
usbPermissionIntent);
manager.requestPermission(driver.getDevice(),
PendingIntent.getBroadcast(this, 0, new
Intent(ACTION_USB_PERMISSION), 0));
return;
} else {
port = driver.getPorts().get(0);
try {
port.open(connection);
port.setParameters(115200, 8,
UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
SerialInputOutputManager usbIoManager = new
SerialInputOutputManager(port, this);
Executors.newSingleThreadExecutor().submit(usbIoManager);
Log.d(TAG, "UART is openned");
} catch (Exception e) {
Log.d(TAG, "There is error");
}
}
}
Step 10: There is an error in step 9 due to missing the uart receiver listener. Please correct this by
following:
public class MainActivity extends Activity implements
SerialInputOutputManager.Listener
Step11: Finally, override a method to receive a message from UART
String buffer = "";
@Override
public void onNewData(byte[] data) {
buffer += new String(data);
//TODO: Process your buffer here
}
@Override
public void onRunError(Exception e) {
Step 12: When you want to send something from the Android device to uart:
port.write("ABC#".getBytes(), 1000);