Android串口通信封装之OkUSB的示例代码
作者:zhouzhuo810 时间:2023-08-22 21:17:29
本文介绍了Android串口通信封装之OkUSB的示例代码,分享给大家。具体如下:
Github传送门:OkUSB
OkUSB
一个简洁的Android串口通信框架。
功能简介
支持设置波特率
支持设置数据位
支持设置停止位
支持设置校验位
支持DTS和RTS
支持串口连接状态监听
用法简介
Gradle
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
compile 'com.github.zhouzhuo810:OkUSB:1.0.0'
}
具体用法
1.在AndroidManifest.xml中添加如下特性:
<uses-feature android:name="android.hardware.usb.host" />
在需要连接串口的Activity中添加:
<intent-filter>
<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" />
2.在res文件夹创建xml文件夹,新建device_filter.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 要进行通信的USB设备的供应商ID(VID)和产品识别码(PID)-->
<usb-device vendor-id="0403" product-id="6001" />
</resources>
3.前面都是准备工作,这里才真正使用。
//初始化
usb = new USB.USBBuilder(this)
.setBaudRate(115200) //波特率
.setDataBits(8) //数据位
.setStopBits(UsbSerialPort.STOPBITS_1) //停止位
.setParity(UsbSerialPort.PARITY_NONE) //校验位
.setMaxReadBytes(20) //接受数据最大长度
.setReadDuration(500) //读数据间隔时间
.setDTR(false) //DTR enable
.setRTS(false) //RTS enable
.build();
//实现监听连接状态和数据收发。
usb.setOnUsbChangeListener(new USB.OnUsbChangeListener() {
@Override
public void onUsbConnect() {
Toast.makeText(MainActivity.this, "conencted", Toast.LENGTH_SHORT).show();
}
@Override
public void onUsbDisconnect() {
Toast.makeText(MainActivity.this, "disconencted", Toast.LENGTH_SHORT).show();
}
@Override
public void onDataReceive(byte[] data) {
tvResult.setText(new String(data, Charset.forName("GB2312")));
}
@Override
public void onUsbConnectFailed() {
Toast.makeText(MainActivity.this, "connect fail", Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionGranted() {
Toast.makeText(MainActivity.this, "permission ok", Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionRefused() {
Toast.makeText(MainActivity.this, "permission fail", Toast.LENGTH_SHORT).show();
}
@Override
public void onDriverNotSupport() {
Toast.makeText(MainActivity.this, "no driver", Toast.LENGTH_SHORT).show();
}
@Override
public void onWriteDataFailed(String error) {
Toast.makeText(MainActivity.this, "write fail" + error, Toast.LENGTH_SHORT).show();
}
@Override
public void onWriteSuccess(int num) {
Toast.makeText(MainActivity.this, "write ok " + num, Toast.LENGTH_SHORT).show();
}
});
4.如果是Activity,可以在onDestroy中调用如下代码关闭串口。
if (usb != null) {
usb.destroy();
}
License
Copyright 2017 zhouzhuo810
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
来源:http://www.jianshu.com/p/123c3cdfb3d3?utm_source=tuicool&utm_medium=referral
标签:Android,串口,通信
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C# HttpClient 如何使用 Consul 发现服务
2021-09-28 01:06:18
![](https://img.aspxhome.com/file/2023/8/111198_0s.png)
在maven中引入本地jar包的步骤
2023-11-25 10:43:44
深入浅析JDK8新特性之Lambda表达式
2023-06-22 08:38:58
详解Spring的两种代理方式:JDK动态代理和CGLIB动态代理
2023-08-08 14:38:37
![](https://img.aspxhome.com/file/2023/8/118808_0s.jpg)
Java背包问题求解实例代码
2023-10-05 06:20:33
C#实现自定义动画鼠标的示例详解
2021-07-31 17:16:59
![](https://img.aspxhome.com/file/2023/4/106424_0s.png)
C#实现希尔排序
2023-11-02 08:15:04
![](https://img.aspxhome.com/file/2023/3/104213_0s.jpg)
Java调用shell命令涉及管道、重定向时不生效问题及解决
2021-07-18 17:02:25
maven的pom文件与打包详解
2023-12-24 00:23:51
![](https://img.aspxhome.com/file/2023/8/64538_0s.png)
Spring源码完美导入IDEA的过程
2023-05-13 14:11:50
![](https://img.aspxhome.com/file/2023/3/80323_0s.jpg)
C#中事件只能在内部调用的原因分析
2023-08-09 05:07:00
spring cloud 阿波罗 apollo 本地开发环境搭建过程
2023-01-25 21:04:50
简单实现Android验证码
2021-12-02 07:09:17
![](https://img.aspxhome.com/file/2023/8/116038_0s.jpg)
MyBatis ofType和javaType的区别说明
2021-09-10 22:56:43
SpringBoot实现Thymeleaf验证码生成
2021-11-25 08:06:38
![](https://img.aspxhome.com/file/2023/0/84170_0s.png)
Java正则多字符串匹配替换
2021-12-16 02:24:48
详解配置spring-boot-actuator时候遇到的一些小问题
2022-03-17 17:04:08
Android 自定义AlertDialog对话框样式
2022-02-06 12:20:07
![](https://img.aspxhome.com/file/2023/3/125533_0s.jpg)
基于获取JAVA路径,包括CLASSPATH外的路径的方法详解
2022-12-20 21:30:03
详解Android 中的文件存储
2023-12-21 17:44:44