• Stars
    star
    102
  • Rank 335,584 (Top 7 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created about 7 years ago
  • Updated about 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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.发送数据

        usb.writeData(byte[], 500);

5.如果是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.

More Repositories

1

ZzHorizontalProgressBar

水平进度条,支持渐变色和二级进度
Java
220
star
2

ZzExcelCreator

Excel表格生成工具
Java
197
star
3

ZzBeeLayout

A nice Image ViewGroup like honeycomb.
Java
75
star
4

CameraCardCropDemo

一个卡片(证件)拍照裁剪框架。
Java
65
star
5

ZzWeatherView

仿墨迹天气最近15天天气视图
Java
59
star
6

ZzImageBox

🔥 Android图片显示容器,支持本地图片和网络图片;支持添加和显示模式;
Java
42
star
7

StringKiller

一个简洁的Android Studio插件。用于一键将layout文件夹中的android:text或android:hint字符串资源提取到strings.xml中。
Java
31
star
8

ZzLettersSideBar

A SideBar of letters for Contacts like WeChat(ZzLettersSideBar)
Java
25
star
9

ZzHorizontalCalenderView

A horizontal and scrollable CalenderView.
Java
14
star
10

ZzSecondaryLinkage

A Secondary Linkage ListView with refreshing and loading more.
Java
14
star
11

ZzRatingBar

Java
7
star
12

ZzPagerIndicator

A powerful custom indicator for Android ViewPager.
Java
6
star
13

ZzHttp

A http framework for simply GET and POST.
Java
4
star
14

BLEHelper

蓝牙BLE调试助手
Java
3
star
15

ZzFragmentTabHost

A powerful FragmentTabhost.
Java
3
star
16

ZzAndFrame

🔥 一个多功能Andriod开发框架
Java
2
star
17

ColorfulRatioBar

you can cutomize 3 - 5 kind of color or 3 - 5 kind of ratio with for a bar
Java
2
star
18

MagpieX

A Android Develop Framwork
Java
2
star
19

BlogBackup

博客备份
HTML
2
star
20

Magpie

🔥 A powerful Android Develop Framework for Mobile, Pad And TV.
Java
1
star
21

ZzNoteDesktop

小周便签桌面客户端
Java
1
star
22

ZzSQLHelper

A powerful sqlite framework that supports ORM and Cursor mode.
Java
1
star
23

ZzNoteQt

ZzNote for Desktop with QT
QML
1
star
24

ApiCreator

Android-强大的接口文档转代码工具
Java
1
star