Android串口通信封装之OkUSB的示例代码
程序员文章站
2023-12-12 16:58:58
本文介绍了android串口通信封装之okusb的示例代码,分享给大家。具体如下:
github传送门:okusb
okusb
一个简洁的android串口通信框...
本文介绍了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.
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。