Connecting bluetooth devices with React-Native App

ShashwatMDas picture ShashwatMDas · May 1, 2020 · Viewed 8.8k times · Source

I am using react-native-bluetooth-serial. I get this error whenever I try to connect a bluetooth device via my react native app:

Error: read failed, socket might closed or timeout, read ret: -1 Unable to connect to device

Here's what I'm trying to do:

 connectToDevice () {
    if (this.state.discovering) {
      return false
    } else {
      this.setState({ discovering: true })
      console.log("searching");
      BluetoothSerial.list()
      .then((Devices) => {
        this.setState({ Devices, discovering: false })
        for(var i = 0; 1; i++)
        {
          if(Devices[i]["name"] == "JBL Flip 3 SE")
          {
            console.log(Devices[i]["id"]);
            BluetoothSerial.connect(Devices[i]["id"]).then((res) => {
              console.log(res);
            }).catch(err => {
              console.log(err);
            })
            break;
          }
        }
        // console.log(unpairedDevices);
      })
      .catch((err) => console.log(err.message))
    }
}

Same happens even when I clone the example repository.

Any idea why this is happening?

Answer

Dev_IL picture Dev_IL · May 25, 2020

I encourage you to use react-native-ble-plx for bluetooth in React Native and if you need live example you can check out our side project which uses this lib to send colors to Arduino and handle LED lights https://github.com/SparingSoftware/HomeLed