react-native bluetooth device keep disconnecting

Viewed 344

I'm building a react-native application (Android only for now) that needs to communicate with a bluetooth device. I'm using this bluetooth library

I can successfully scan the device, the problem start when I try to connect to it: after few seconds of waiting I get the error

Device MAC_ADDRESS was disconnected

Here's the code:

bluetoothInstance.startDeviceScan(null, null, (error, device) => {
    console.log('start scanning');
if (error) {
  console.log(error);
  return
} else {
  if (device.id = DEVICE_ID) {
    console.log('device found');

    console.log('stop scanning');
    bluetoothInstance.stopDeviceScan();

    console.log('connecting...');
    device.connect()
      .then((device) => {
        console.log("Discovering services and characteristics");
        return device.discoverAllServicesAndCharacteristics()
      })
      .then((device) => {
        console.log("Setting notifications");
        return device;
      })
      .then(() => {
        console.log("Listening...");
      }, (error) => {
        console.log(error.message);
      })
  }
}
});

I can't see any console.log print after connecting to the device. I don't read "Discovering services and characteristics" nor "Setting notifications" nor "Listening...", just "Device MAC_ADDRESS was disconnected" which correspond to the line console.log(error.message).

What am I doing wrong?

0 Answers
Related