Android串口通信封装之OkUSB的示例代码

Android串口通信封装之OkUSB的示例代码,第1张

概述本文介绍了Android串口通信封装之OkUSB的示例代码,分享给大家。具体如下:Github传送门:OkUSB

本文介绍了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 zhouzhuo810licensed 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.0Unless required by applicable law or agreed to in writing,softwaredistributed 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 andlimitations under the license.

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android串口通信封装之OkUSB的示例代码全部内容,希望文章能够帮你解决Android串口通信封装之OkUSB的示例代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1143976.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存