Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Android串口通信示例

NotificationsYou must be signed in to change notification settings

kongqw/AndroidSerialPort

Repository files navigation

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'}

查看串口

SerialPortFinderserialPortFinder =newSerialPortFinder();ArrayList<Device>devices =serialPortFinder.getDevices();

打开串口

初始化

mSerialPortManager =newSerialPortManager();

添加打开串口监听

mSerialPortManager.setOnOpenSerialPortListener(newOnOpenSerialPortListener() {@OverridepublicvoidonSuccess(Filedevice) {            }@OverridepublicvoidonFail(Filedevice,Statusstatus) {    }});

添加数据通信监听

mSerialPortManager.setOnSerialPortDataListener(newOnSerialPortDataListener() {@OverridepublicvoidonDataReceived(byte[]bytes) {            }@OverridepublicvoidonDataSent(byte[]bytes) {    }});

打开串口

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

发送数据

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

关闭串口

mSerialPortManager.closeSerialPort();

PS:传输协议需自行封装

About

Android串口通信示例

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp