Skip to content

Latest commit

 

History

History
136 lines (93 loc) · 3.82 KB

README.md

File metadata and controls

136 lines (93 loc) · 3.82 KB

Android-SerialPort

此项目移植于谷歌官方串口库android-serialport-api,但该项目仅支持串口名称及波特率,所以在项目的基础上添加支持数据位、数据位、停止位、流控等配置。

License

下载

Get it on Google Play

酷安:https://www.coolapk.com/apk/251882

引入

Step 1. Add the JitPack repository to your build file

Add it in your root build.gradle at the end of repositories:

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

Step 2. Add the dependency

dependencies {
        implementation 'com.github.F1ReKing:Android-SerialPort:1.5.1'
}

使用

1. 查询串口列表

SerialPortHelper#getAllDevices();
// 查询串口设备地址列表
SerialPortHelper#getAllDeicesPath();

2. 配置串口参数

SerialPortHelper#Builder(String port, int baudRate).build(); //支持配置串口号,波特率(默认值115200)
setStopBits(int stopBits); // 支持设置停止位 默认值为2
setDataBits(int dataBits); // 支持设置数据位 默认值为8
setParity(int parity); // 支持设置检验位 默认值为0
setFlowCon(int flowCon); // 支持设置流控 默认值为0
setFlags(int flags); // 支持设置标志 默认值为0,O_RDWR  读写方式打开

3. 打开串口

SerialPortHelper#open();

4. 关闭串口

SerialPortHelper#close();

4. 发送数据

SerialPortHelper#sendBytes(byte[] bytes); // 支持发送byte[]
SerialPortHelper#sendHex(String hex); // 支持发送Hex
SerialPortHelper#sendTxt(String txt); // 支持发送ASCII码

5. 接收数据

public interface ISerialPortDataListener {
	// 接收数据回调
    void onDataReceived(byte[] bytes);
   	// 发送数据回调
    void onDataSend(byte[] bytes);
}

6. 回调

//  串口打开状态监听
void setIOpenSerialPortListener(IOpenSerialPortListener IOpenSerialPortListener);

// 串口消息监听
void setISerialPortDataListener(ISerialPortDataListener ISerialPortDataListener);

7. proguard-rules

-keep class me.f1reking.serialportlib.** {*;}

版本更新记录

1.1

  • 优化api
  • 支持设置可选参数,并配置默认值

1.0

  • 基础功能、支持设置串口号、波特率、数据位、校验位、停止位、流控等配置
  • 支持发送、接收数据

License

Copyright 2019 F1ReKing. 

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.

Stargazers over time

Stargazers over time