一、添加maven仓库
maven {
url 'https://packages.aliyun.com/maven/repository/2386483-release-mhUqVd'
credentials {
username = "6166b8fa89e1d5005a9f5792"
password = "W49P9mYH7aoH"
}
}
二、添加依赖
implementation "com.lyn:serialport:1.0.0"
三、使用
val serialPort = SerialPort()
val fileDescriptor = serialPort.open("dev/ttyS3", 9600, 0)
if (fileDescriptor!=null){
Log.d(TAG,"打开成功")
}else{
Log.d(TAG,"打开失败")
}
//发送数据
serialPort.getOutStream()?.write(byteArrayOf(0x00,0x00))
//读取数据
serialPort.getInputStream()?.let {
Thread{
while (true){
val available = it.available()
if (available<=0){
SystemClock.sleep(10)
continue
}
val data = ByteArray(available)
val read = it.read(data)
if (read>0){
Log.d(TAG,"读取成功:${data.contentToString()}")
}
SystemClock.sleep(10)
}
}.start()
}
总结
总体使用还是比较简单的,希望对各位大佬有用,如有疑问欢迎评论区探讨,最后附上项目代码源码地址 gitee.com/longyn/I-an…