栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

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

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

Github传送门:OkUSB

OkUSB

一个简洁的Android串口通信框架。

功能简介

  1. 支持设置波特率
  2. 支持设置数据位
  3. 支持设置停止位
  4. 支持设置校验位
  5. 支持DTS和RTS
  6. 支持串口连接状态监听

用法简介

Gradle

  allprojects {
    repositories {
      ...
      maven { url 'https://jitpack.io' }
    }
  }

  dependencies {
      compile 'com.github.zhouzhuo810:OkUSB:1.0.0'
  }

具体用法

1.在AndroidManifest.xml中添加如下特性:

在需要连接串口的Activity中添加:

  
    
  
  

2.在res文件夹创建xml文件夹,新建device_filter.xml



  
  

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.

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/157439.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号