[Java] Use serial communication on Android

3 minute read


Since the method of serial communication on Android is not introduced so much, I will describe it.

Target device

REX-USB60MB (USB-serial conversion)

Target environment



From the FTDI website, click ** here ** in the ** Java D2XX Update including FT4222H support ** section to download the library.
FTDI Home Page

Unzip the downloaded compressed file

Use only ** d2xx.jar ** files located in the ** root ** folder.
** device_filter.xml ** in the ** res / xml / ** folder is old, so create it without using it.

Add d2xx.jar to library

Click the ▼ on the right that says ** Android ** at the top of the project tree and select ** Project ** from the list that appears.
Then, the tree display will be different from the previous one.

Add d2xx.jar to ** app / libs ** by drag and drop. Right-click on the added d2xx.jar and execute ** Add to Library **.
The additional operation only adds ** implementation files (‘libs / j2xx.jar’) ** to build.grade (app).
If the library is no longer needed, delete this line and delete libs / j2xx.jar to get it back.

After adding d2xx.jar to the library, click ▼ on the right to return it to ** Android **, which is the top ** project ** in the project tree.

Ready to use a USB device

In Androidmanifest.xml


        <activity android:name=".MainActivity">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
                android:resource="@xml/device_filter" />

Set up to use the USB device in this way.
I don’t use ** @ xml / device_filter **, but just in case.

Check if it is recognized


        var mFTDIManager = D2xxManager.getInstance(this.context)
        var deviceCount = mFTDIManager?.createDeviceInfoList(this.context) ?: 0

Check if these two lines can be recognized.
Since ** deviceCount ** is the number of recognized devices, it is successful if 1 or more is entered.

REX-USB60MB is not recognized

As you can see who made the sample work, the sample does not actually recognize REX-USB60MB.
The reason for not recognizing it is in the ** device_filter.xml ** file that I abandoned because it was old.


	<usb-device vendor-id="1412" product-id="45088" /> <!-- REX-USB60F -->

As commented, ** REX-USB60F ** is registered, but ** REX-USB60MB ** is not.
So why not define REX-USB60MB here? I thought, but that would make it inconvenient to use the higher model ** RS-USB60FC ** and ** REX-USB60MI ** for tablets, as it would need to be changed again.

So I will try to recognize the connected serial communication device and use it.


    var usbDevice = mutableMapOf<String,Any>()
    var usbDeviceList : MutableList<MutableMap<String,Any>> = mutableListOf()
        val manager = getSystemService<UsbManager>(context, UsbManager::class.java)
        if (manager == null) return
        for(item in manager.deviceList) {
            val device: UsbDevice = item.value
            val str : String = device.manufacturerName ?: ""
            val vid = device.vendorId
            val pid = device.productId
            usbDevice = mutableMapOf("vid" to vid,"pid" to pid ,"key" to item.key,"name" to  str)

A USB device seems to work if you know ** Vendor ID ** and ** Product ID **, so it’s okay if you can get it, but I’m worried if I don’t know what the device is, so I also get ** manufacturerName ** I am trying to do it.

Register the device to use

The device you want to use is not recognized, so register it manually.


        var mFTDIManager = D2xxManager.getInstance(this.context)
        var deviceCount = mFTDIManager?.createDeviceInfoList(this.context) ?: 0

You can force it to be recognized by passing ** VendorID ** and ** ProductID ** as arguments of ** setVIDPID **.
If the value of ** deviceCount ** is 1 or more, it is successful.

  • 1412,45115 are the values of REX-USB60MB

Reference site

Reference site