react-native-ble-plx: Android, connectToDevice() repeatedly fails after a few successes

react-native-ble-plx 1.0.3 react-native 0.59.1

Android-only issue. This occurs across makes, models, and OS versions. For example: Pixel 1, Pixel 2, Galaxy S8, Galaxy S7, Nexus. OS’s range from 6.0 - 9.0. This began happening after upgrading both RN and RNBP.

Our app connects to washers and dryers via bluetooth. Our MachineListScreen lists available machines

machineA machineB machineC

Using connectToDevice(machine), we are successfully able to connect to machineA, machineB, and machineC, transition to ConnectedToMachineScreen and run interactions.

This works only once!

The next time we connectToDevice(machine), it simply fails to connect. The error is a DeviceConnectionFailed. It’s due to a connection attempt timeout that we impose, but this is immaterial. Our timeout for a connection attempt is 10 seconds. But we’ve tried bumping it to 30 seconds, 60 seconds, et cetera, and it still fails.

This doesn’t happen on iOS. iOS connects repeatedly.

Here’s the code.

_handleMachinePress = async machine => { 
  try {  
    this.setState({ loading: true });
    await ble.connectionManager.connectToMachine(machine);
    const passProps = { ...props };
    navigateToScreen(ConnectedToMachineScreen, passProps);
  } catch (error) {
    console.warn(error);
  } finally {
    this.setState({ loading: false });
  }
}
export const connectToMachine = async machine => {  
  return new Promise(async (resolve, reject) => {
    let connectedDevice;
    try {
      connectedDevice = await ble.connectToDevice(machine.id, { timeout: 10000 });
    } catch (error) {
      try {
        await timeout(5000);
        connectedDevice = await ble.connectToDevice(machine.id, { timeout: 10000 });
      } catch (error) {
        reject(error);
        return;
      }
    }
    try {
      const exploredDevice = await connectedDevice.discoverAllServicesAndCharacteristics();
      await exploredDevice.services();
      resolve();
    } catch (error) {
      reject(error);
    }
  });
};

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 2
  • Comments: 23

Most upvoted comments

@folofse So, I found a hacky solution to this. I don’t love it. But it has worked so far Update this code failed. See our solution below

let ble = new BleManager()

const scanDevices =() => {
  let arr = [];
  ble.startDeviceScan( options => {
    arr.push(device)
  })
  return arr;
}

ble.destroy()

ble = new BleManager()

ble.connectToDevice(arr[x]);

Again, I think this is a poor solution, but resetting the BleManager by destroying and re-creating it seems to help. If someone has a better alternative, please advise.

Sad this is being retired when it’s still an issue with no reasonable workaround. If I have more than one device I have to abort all and reconnect just to disconnect one.

@GreanBeetle I get the same problem with pending promises. Have you found a solution?