Use serial communication on Android

Introduction

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

Target device

Android REX-USB60MB (USB-serial conversion)

Target environment

Android6.0 AndroidStudio Kotlin

download

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

manifest.xml


        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <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" />
        </activity>

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

sample.kt


        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.

device-filter.xml


	<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.

comm.kt


    var usbDevice = mutableMapOf<String,Any>()
    var usbDeviceList : MutableList<MutableMap<String,Any>> = mutableListOf()
        val manager = getSystemService<UsbManager>(context, UsbManager::class.java)
        usbDeviceList.clear()
        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)
            usbDeviceList.add(usbDevice)
            mFTDIManager?.setVIDPID(vid,pid)
        }

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.

sample.kt


        var mFTDIManager = D2xxManager.getInstance(this.context)
        mFTDIManager?.setVIDPID(1412,45115)
        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.

Reference site

Reference site

Recommended Posts

Use serial communication on Android
Use native code on Android
[Adult free study] Acoustic communication on Android
Try communication using gRPC on Android + Java server
Use perltidy on CentOS 8
[Android] Notes on xml
Use mod_auth_cas on CentOS 8
Use Corretto 11 on Heroku
Use cpplapack on ubuntu
[Android Studio] I want to use Maven library on Android
Multipart transmission library on Android
How to use OpenCV 4 on Android and view camera live view
ROS app development on Android
Serial communication sample using jSerialComm
Serial communication sample using purejavacomm
Event handling with RxBus on Android
How to "hollow" View on Android
Bridge commentary on React Native Android
[Android] Get the date on Monday
Use Docker Compose on Windows 10 Home
Get JUnit code coverage on Android.
How to use Ruby on Rails
How to use Bio-Formats on Ubuntu 20.04
Watson Assistant (formerly Conversation) on Android
Notes on calling Installer on Android App
Easy to use Cloud Firestore (Android)
Save ArrayList using GSON on Android
Use the iostat command on CentOS 8
Use completion in Eclipse on mac
Notes on Android (java) thread processing
[Android] Receive intent on Broadcast Reciever
Use Docker CE (official) on CentOS 8
Implement ripple representation on images on Android
Speed up location acquisition on Android
What wasn't fair use in the diversion of Java APIs on Android