国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android串口通信封裝之OkUSB的示例代碼

2019-10-22 18:24:51
字體:
來源:轉載
供稿:網友

本文介紹了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中添加如下特性:

<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.

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 连云港市| 高州市| 邢台县| 大城县| 蛟河市| 白沙| 平潭县| 景谷| 湖南省| 永胜县| 岑巩县| 建湖县| 延津县| 梧州市| 元氏县| 革吉县| 龙州县| 奉化市| 天祝| 江阴市| 师宗县| 江达县| 博兴县| 余干县| 老河口市| 承德县| 越西县| 万荣县| 金坛市| 苏尼特左旗| 乐业县| 武穴市| 乐昌市| 保亭| 大新县| 江阴市| 阿克陶县| 石门县| 南江县| 河西区| 白城市|