question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Android, connectToDevice() repeatedly fails after a few successes

See original GitHub issue

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);
    }
  });
};

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:23

github_iconTop GitHub Comments

3reactions
GreanBeetlecommented, Sep 3, 2019

@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.

2reactions
glennecommented, Nov 25, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android, connectToDevice() repeatedly fails after a few ...
After a few success, connect function will execute without any response. I added the timeout option to connect and get the timeout Error:...
Read more >
Android: Catching BLE Connection Fails/Disconnects?
BluetoothGatt.disconnect() Disconnects an established connection, or cancels a connection attempt currently in progress. To free up GATT resources, after ...
Read more >
Android 13: Unable to reconnect reliably to bonded peripheral
After this upgrade, it is not stable and continuously gets disconnected randomly or fails to connect at all. It shows msg saying "Problem...
Read more >
Connect Bluetooth devices - Android Developers
When successful, accept() returns a connected BluetoothSocket . ... If the connection fails, or if the connect() method times out (after ...
Read more >
Fix Bluetooth problems on Android - Google Support
Learn how to pair and connect via Bluetooth. Restart your devices. Learn how to restart your Pixel phone or Nexus device. Step 2:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found