Android串口通信示例
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.idea delete Dec 13, 2017
SerialPortLibrary delete Dec 13, 2017
app 修改README Nov 14, 2017
gradle/wrapper delete Dec 13, 2017
.gitignore Initial commit Oct 31, 2016
README.md README Dec 13, 2017
build.gradle delete Dec 13, 2017
gradle.properties Initial commit Oct 31, 2016
gradlew Initial commit Oct 31, 2016
gradlew.bat Initial commit Oct 31, 2016
settings.gradle Initial commit Oct 31, 2016

README.md

说明

android-serialport-api

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 {
        compile 'com.github.kongqw:AndroidSerialPort:1.0.1'
}

查看串口

SerialPortFinder serialPortFinder = new SerialPortFinder();
ArrayList<Device> devices = serialPortFinder.getDevices();

打开串口

初始化

mSerialPortManager = new SerialPortManager();

添加打开串口监听

mSerialPortManager.setOnOpenSerialPortListener(new OnOpenSerialPortListener() {
    @Override
    public void onSuccess(File device) {
        
    }

    @Override
    public void onFail(File device, Status status) {

    }
});

添加数据通信监听

mSerialPortManager.setOnSerialPortDataListener(new OnSerialPortDataListener() {
    @Override
    public void onDataReceived(byte[] bytes) {
        
    }

    @Override
    public void onDataSent(byte[] bytes) {

    }
});

打开串口

  • 参数1:串口
  • 参数2:波特率
  • 返回:串口打开是否成功
boolean openSerialPort = mSerialPortManager.openSerialPort(device.getFile(), 115200);

发送数据

  • 参数:发送数据 byte[]
  • 返回:发送是否成功
boolean sendBytes = mSerialPortManager.sendBytes(sendContentBytes);

关闭串口

mSerialPortManager.closeSerialPort();

PS:传输协议需自行封装