2.0.0 • Published 11 months ago

react-native-native-serialport v2.0.0

Weekly downloads
-
License
Apache
Repository
github
Last release
11 months ago

react-native-native-serialport

npm version npm downloads npm licence Platform

Native serial port for react-native on Android.

The native serial port lib use https://github.com/licheedev/Android-SerialPort-API.

For react-native USB serial port, please ref to https://github.com/flyskywhy/react-native-usb-serialport.

Install

npm install react-native-native-serialport

Add maven { url 'https://jitpack.io' } into build.gradle:

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

Usage

import RNSerialPort from 'react-native-native-serialport';
  • get device path list
RNSerialPort.getAllDevicesPath(result => console.log(result));
  • open serial port
RNSerialPort.openSerialPort('/dev/ttySO', 9600);
  • send data
let byteData = [0x00, 0x01, 0x02, 0x03, 0x05];
RNSerialPort.sendByteData('/dev/ttyS7', byteData);
  • monitor status and data on serial port
const emitter = Platform.OS === 'ios' ? {
    addListener: () => {},
    removeListener: () => {},
    emit: () => {},
} : DeviceEventEmitter;

emitter.addListener('onSerialPortRecevieData', this.onSerialPortRecevieData);
emitter.addListener('onSerialPortOpenStatus', this.onSerialPortOpenStatus);

onSerialPortOpenStatus(status) {
    console.log("onSerialPortOpenStatus");
    console.log(status);
}

onSerialPortRecevieData(data) {
    console.log("onSerialPortRecevieData");
    console.log(data);
}

And can ref to example/RNSerialPortManager.js.