阅读事件侦听器未触发本机反应

Read Event listener not firing in react native

我正在使用 React Native 开发一个蓝牙项目,我正在尝试从其他设备读取数据。使用以下代码

读取数据
BluetoothSerial.readFromDevice().then((data) => {
      console.log(data)
}

效果很好。 但是我想使用一个事件监听器来收集一些其他设备在任何随机时间发送的数据。 我正在使用在 https://github.com/rusel1989/react-native-bluetooth-serial/issues/16 找到的侦听器,由 @andrescheca 回答。代码如下所示

    BluetoothSerial.withDelimiter('\r').then(() => {
    Promise.all([
      BluetoothSerial.isEnabled(),
      BluetoothSerial.list()
    ])
    .then((values) => {
      const [ isEnabled, devices ] = values
      this.setState({ isEnabled, devices })
    })

    BluetoothSerial.on('bluetoothEnabled', () => console.log('Bluetooth enabled'))
    BluetoothSerial.on('bluetoothDisabled', () => console.log('Bluetooth disabled'))
    BluetoothSerial.on('read', (data) => {

       console.log(`DATA FROM BLUETOOTH: ${data.data}`);
       Toast.show(data.data);
   })
    BluetoothSerial.on('error', (err) => console.log(`Error: ${err.message}`))
    BluetoothSerial.on('connectionLost', () => {
      if (this.state.device) {
        console.log(`Connection to device ${this.state.device.name} has been lost`)
      }
      this.setState({ connected: false })
    })
  });

当我启用和禁用蓝牙时,所有侦听器(即 bluetoothEnabled、bluetoothDisabled)都在触发。但是读取的监听器没有在接收数据时触发。我假设它应该在另一台设备发送一些数据后立即触发。 如果有人能帮助我解决这个问题,我将不胜感激。

我解决了这个问题,只是在我试图从 Linux 服务器发送到我的设备的数据末尾添加 \r 。 感谢@kendavidson 的帮助。