bastengao / react-native-serial-port-api

react-native serial port API
https://bastengao.com/react-native-serial-port-api/
29 stars 11 forks source link

W/unknown:ReactNative: Calling JS function after bridge has been destroyed #18

Open miumiu316 opened 3 years ago

miumiu316 commented 3 years ago

The react-native-serial-port-api works well from start of the react native app, but when restart the app from RNRestart Libiary or DevSettings.reload() method, the react-native-serial-port-api seems have some issues, see below scenario: 1 Start APP--react-native-serial-port-api, send and receive OK, the "dataReceived" event is triggered on the java script side

2 Reload the APP using RNRestart Libiary(https://github.com/avishayil/react-native-restart) or using DevSetting.reload() or just "r" from the console. the first time when react-native-serial-port-api can send out the Data out, and from Physical Layer there is data response back, but the "dataReceived" event is not triggered on the java script side, from the second time send out the data, the react-native-serial-port-api works normal on send and receive.

3 see below logs from Android Studio: 2021-08-29 19:59:25.537 30148-30362/com.inst.hct802h I/serialport: send: 550500000002aa 2021-08-29 19:59:26.045 30148-30326/com.inst.hct802h W/unknown:ReactNative: Calling JS function after bridge has been destroyed: RCTDeviceEventEmitter.emit(["dataReceived",{"data":"FFFFFFFFFFFF0600180042FE174005070108009B0837050401260200101701"}])

4 by Search the google, seems above behavior might be the context is destroyed when RNRestart or DevSetting.reload, and the react-native-serial-port-api is using the old old react context before restart or reload.

5 see reference saying it might be triggered by the the RCTDeviceEventEmitter module trying to send a message to javascript. https://stackoverflow.com/questions/45972220/react-calling-js-function-after-bridge-has-been-destroyed-how-to-find-which

Help Needed: are there any thoughts that we can workaroud of this behavior from the react-native-serial-port-api libiary or the RN side?

bastengao commented 3 years ago

Why do you need to restart app?

Try to close serial port and open a new one.

serialPort.close()
serialPort = await SerialPortAPI.open(...)
miumiu316 commented 3 years ago

Thanks for the Comments. The reason why we restart the app is that there is some critical parameters that allowed for the user to config and save to local storage, not only including the baudrate, parity but also some other User Business logic related paramters that we need to make sure these parameters are saved correctly. Our Scenario is Like this: 1 serialPort.open()(await SerialPortAPI.open(...)) 2 Send Data and Receive Data and Render 3 serialPort.close() 4 RNRestart.restart() and repeat 1-4 so each restart we open a new serial port using await SerialPortAPI.open(...)

bastengao commented 3 years ago

Try reinstall with fix-eventEmitter-cache branch. Let me know if it works. I will merge this branch to master and release new version.

npm install react-native-serial-port-api@fix-eventEmitter-cache --save
miumiu316 commented 3 years ago

Unfortunately, same behavior as before using this fix-eventEmitter-cache branch code. We will investigate this libiary code in the coming days and we will get you back if we find something.