@goroga/react-native-serialport
v1.1.4
Published
TurboModule-compatible USB serial port library for React Native (Expo 54+ / RN 0.76+)
Maintainers
Readme
@goroga/react-native-serialport
TurboModule-compatible USB serial port library for React Native on Android.
This package is based on react-native-serialport and is maintained as the published package @goroga/react-native-serialport, with support targeted at React Native >=0.76.0 and Expo 54+ native projects.
Platform Support
- Android only
- React Native
>=0.76.0 - Expo 54+ after
prebuildor in a custom development build
Installation
npm install @goroga/react-native-serialportyarn add @goroga/react-native-serialportPeer dependency: your app must use react-native >=0.76.0.
If you are using Expo, install the package, then regenerate native projects before building:
npx expo prebuildNative Notes
- USB serial communication is available on Android only.
- The Android implementation uses felHR85/UsbSerial.
- The package exports
RNSerialport,definitions, andactions.
Usage
import { DeviceEventEmitter } from "react-native";
import { RNSerialport, definitions, actions } from "@goroga/react-native-serialport";
const subscriptions = [
DeviceEventEmitter.addListener(actions.ON_CONNECTED, () => {
console.log("Serial device connected");
}),
DeviceEventEmitter.addListener(actions.ON_READ_DATA, (event) => {
console.log("Incoming data:", event.payload);
}),
DeviceEventEmitter.addListener(actions.ON_ERROR, (error) => {
console.error("Serial error:", error);
}),
];
RNSerialport.setReturnedDataType(definitions.RETURNED_DATA_TYPES.INTARRAY);
RNSerialport.setAutoConnectBaudRate(9600);
RNSerialport.setDriver(definitions.DRIVER_TYPES.AUTO);
RNSerialport.startUsbService();
// Example manual connection flow
RNSerialport.getDeviceList().then((devices) => {
if (!devices?.length) {
return;
}
RNSerialport.connectDevice(devices[0].name, 9600);
});
// Example writes
RNSerialport.writeString("HELLO");
RNSerialport.writeBase64("SEVMTE8=");
RNSerialport.writeHexString("48454C4C4F");
// Cleanup
subscriptions.forEach((subscription) => subscription.remove());
RNSerialport.disconnect();
RNSerialport.stopUsbService();Exported Constants
definitions
DATA_BITS:DATA_BITS_5,DATA_BITS_6,DATA_BITS_7,DATA_BITS_8STOP_BITS:STOP_BITS_1,STOP_BITS_15,STOP_BITS_2PARITIES:PARITY_NONE,PARITY_ODD,PARITY_EVEN,PARITY_MARK,PARITY_SPACEFLOW_CONTROLS:FLOW_CONTROL_OFF,FLOW_CONTROL_RTS_CTS,FLOW_CONTROL_DSR_DTR,FLOW_CONTROL_XON_XOFFRETURNED_DATA_TYPES:INTARRAY,HEXSTRINGDRIVER_TYPES:AUTO,CDC,CH34x,CP210x,FTDI,PL2303
actions
ON_SERVICE_STARTEDON_SERVICE_STOPPEDON_DEVICE_ATTACHEDON_DEVICE_DETACHEDON_ERRORON_CONNECTEDON_DISCONNECTEDON_READ_DATA
Main API
startUsbService()stopUsbService()isOpen()isServiceStarted()isSupported(deviceName)setReturnedDataType(type)setInterface(iFace)setDataBit(bit)setStopBit(bit)setParity(parity)setFlowControl(control)setAutoConnectBaudRate(baudRate)setAutoConnect(status)setDriver(driver)loadDefaultConnectionSetting()getDeviceList()connectDevice(deviceName, baudRate)disconnect()writeString(data)writeBase64(data)writeHexString(data)intArrayToUtf16(intArray)hexToUtf16(hex)
Default Connection Settings
| Setting | Default |
| --- | --- |
| Returned data type | INTARRAY |
| Baud rate | 9600 |
| Auto connect baud rate | 9600 |
| Port interface | -1 |
| Data bit | 8 |
| Stop bit | 1 |
| Parity | NONE |
| Flow control | OFF |
| Driver | AUTO |
Android Package Name
com.melihyarikkaya.rnserialport
