Android serial port read/write library.
-
Import AAR
Gradle or Download SerialPort
//kotlin dependencies { implementation("cn.lalaki:SerialPort.Android:1.4") }
//groovy dependencies { implementation 'cn.lalaki:SerialPort.Android:1.4' }
-
Code Sample
//Kotlin val b115200 = "0010002".toInt(8) val serialPort = SerialPort("/dev/ttySX", b115200, object : SerialPort.DataCallback { override fun onData(data: ByteArray) { //... } }) serialPort.write(byte[]) serialPort.close()
//Java import cn.lalaki.SerialPort; SerialPort serialPort1 = new SerialPort("/dev/ttySX",0010002,null); // write only serialPort1.write(byte[]); SerialPort serialPort2 = new SerialPort("/dev/ttySX", 0010002, new SerialPort.DataCallback() { @Override public void onData(@Nullable byte[] data) { //... } }); serialPort2.write(byte[]); serialPort2.close();
-
Params
new SerialPort(path, speed, callback);
param value type example path Serial port path String "dev/ttyS0" speed is baud rate See termbits.h int B9600 value is 0000015, B115200 value is 0010002 callback DataCallback interface ... -
Common problems
### Checking Permissions adb shell su setenforce 0 chmod 0766 /dev/ttyS*
Generating electricity for love.
- feedback:[email protected]