AndroidSerialpPort

  Android串口通信

  一.集成AndroidSerialPort库:

    1.allprojects {
      repositories {
        ... maven { url 'https://jitpack.io' }
          }
         }
 
    2.compile 'com.github.kongqw:AndroidSerialPort:1.0.1'

  二、查看串口

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

  三、初始化

    SerialPortManager 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) {

      }
     });

  六、打开串口

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

  七、发送数据

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

  八、关闭串口

    mSerialPortManager.closeSerialPort();

原文地址:https://www.cnblogs.com/leizz/p/11671436.html