0% found this document useful (0 votes)
85 views4 pages

name :name : Uses-Feature Uses-Permission

The document provides steps to connect an Android device to a USB serial device using open source code from GitHub. It involves adding USB permissions to the manifest, configuring intent filters and metadata to detect USB attachments, specifying supported USB devices in an XML file, importing a USB serial library, opening a UART connection on device attachment, and implementing a listener to receive incoming serial data on the Android device.

Uploaded by

Minh Khoa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views4 pages

name :name : Uses-Feature Uses-Permission

The document provides steps to connect an Android device to a USB serial device using open source code from GitHub. It involves adding USB permissions to the manifest, configuring intent filters and metadata to detect USB attachments, specifying supported USB devices in an XML file, importing a USB serial library, opening a UART connection on device attachment, and implementing a listener to receive incoming serial data on the Android device.

Uploaded by

Minh Khoa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy