Auto Connection - WiMetrixDev/rn-usb-serial GitHub Wiki

Should be see for open the connection when the service is started or the usb device is attached

Import Library and Requirements

import { RNSerialport, definitions, actions } from 'rn-usb-serial';
import { DeviceEventEmitter } from 'react-native';

Set Your Callback Methods

componentDidMount() {
  DeviceEventEmitter.addListener(
    actions.ON_SERVICE_STARTED,
    this.onServiceStarted,
    this
  );
  DeviceEventEmitter.addListener(
    actions.ON_SERVICE_STOPPED,
    this.onServiceStopped,
    this
  );
  DeviceEventEmitter.addListener(
    actions.ON_DEVICE_ATTACHED,
    this.onDeviceAttached,
    this
  );
  DeviceEventEmitter.addListener(
    actions.ON_DEVICE_DETACHED,
    this.onDeviceDetached,
    this
  );
  DeviceEventEmitter.addListener(actions.ON_ERROR, this.onError, this);
  DeviceEventEmitter.addListener(actions.ON_CONNECTED, this.onConnected, this);
  DeviceEventEmitter.addListener(
    actions.ON_DISCONNECTED,
    this.onDisconnected,
    this
  );
  DeviceEventEmitter.addListener(actions.ON_READ_DATA, this.onReadData, this);
}

Set Serialport Settings

componentDidMount() {
  //.
  // Set Your Callback Methods in here
  //.
  RNSerialport.setAutoConnect(true);
  RNSerialport.setAutoConnectBaudRate(9600);
  RNSerialport.startUsbService();
  //Started usb listener
}

Some precautions

componentWillUnmount = async () => {
	DeviceEventEmitter.removeAllListeners();
	const isOpen = await RNSerialport.isOpen();
	if (isOpen) {
		Alert.alert('isOpen', isOpen);
		RNSerialport.disconnect();
	}
	RNSerialport.stopUsbService();
};

Installation Successfuly

See for methods

Full Example

import { DeviceEventEmitter } from 'react-native';
import { RNSerialport, definitions, actions } from 'rn-usb-serial';

export default class App extends Component<Props> {
	componentDidMount() {
		DeviceEventEmitter.addListener(
			actions.ON_SERVICE_STARTED,
			this.onServiceStarted,
			this
		);
		DeviceEventEmitter.addListener(
			actions.ON_SERVICE_STOPPED,
			this.onServiceStopped,
			this
		);
		DeviceEventEmitter.addListener(
			actions.ON_DEVICE_ATTACHED,
			this.onDeviceAttached,
			this
		);
		DeviceEventEmitter.addListener(
			actions.ON_DEVICE_DETACHED,
			this.onDeviceDetached,
			this
		);
		DeviceEventEmitter.addListener(actions.ON_ERROR, this.onError, this);
		DeviceEventEmitter.addListener(
			actions.ON_CONNECTED,
			this.onConnected,
			this
		);
		DeviceEventEmitter.addListener(
			actions.ON_DISCONNECTED,
			this.onDisconnected,
			this
		);
		DeviceEventEmitter.addListener(actions.ON_READ_DATA, this.onReadData, this);
		RNSerialport.setInterface(-1); //default -1
		RNSerialport.setAutoConnectBaudRate(9600);
		RNSerialport.setAutoConnect(true); // must be true for auto connect
		RNSerialport.startUsbService(); //start usb listener
	}

	componentWillUnmount = async () => {
		DeviceEventEmitter.removeAllListeners();
		RNSerialport.isOpen((isOpen) => {
			if (isOpen) {
				RNSerialport.disconnect();
				RNSerialport.stopUsbService();
			} else {
				RNSerialport.stopUsbService();
			}
		});
	};

	/* BEGIN Listener Methods */

	onDeviceAttached() {
		console.log('Device Attached');
	}

	onDeviceDetached() {
		console.log('Device Detached');
	}

	onError(error) {
		console.log('Code: ' + error.errorCode + ' Message: ' + error.errorMessage);
	}

	onConnected() {
		console.log('Connected');
	}

	onDisconnected() {
		console.log('Disconnected');
	}

	onServiceStarted(response) {
		//returns usb status when service started
		if (response.deviceAttached) {
			this.onDeviceAttached();
		}
	}

	onServiceStopped() {
		console.log('Service stopped');
	}

	onReadData(data) {
		console.log(data.payload);
	}

	/* END Listener Methods */
}
⚠️ **GitHub.com Fallback** ⚠️