📜 ⬆️ ⬇️

Work with COM port in Android applications

Good day to all!

Once I thought about developing an application that would require communication with a hardware device through a COM port. Although Android carefully provides api for working with usb devices, it unfortunately did not suit me, because I wanted to support my old tablet based on Android version 2.2. Standard api from Google, did not fit

for two reasons:


So, after a long search on the Internet, I came across a very interesting solution android-serialport-api . This solution is a Java wrapper in which through the JNI in which calls to the USB device are made.
')
Library developers offer 4 different ways to connect an Android device to a COM port via USB.

image

Since I had at my disposal a tablet with a USB host port, and I have a USB to RS232 converter on hand, I went with option number 2.

The advantages and disadvantages of such a solution are

Benefits


disadvantages


So let's get started!

Here I will describe the approach to the use of this library.

serialPortFinder.getAllDevices(); //      usb-   . serialPortFinder.getAllDevicesPath(); //     SerialPort mSerialPort = new SerialPort(foundedDeviceFormSerialPortFinder, 9600, 0); OutputStream outputStream = mSerialPort.getOutputStream(); InputStream inputStream = mSerialPort.getInputStream(); //            


For serialPortFinder to work correctly, it is necessary that the USB-RS232 adapter be connected, defined in / dev.

This is the simple way to communicate with a very large amount of iron through the COM port,

Source: https://habr.com/ru/post/163913/


All Articles