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.

Stuck on retrieveServices (not connected after connection?)

See original GitHub issue

Describtion Hi, I’ve got this annoying bug it appears inconsistently every few times. It’s really similar to this Issue [#402] (https://github.com/innoveit/react-native-ble-manager/issues/402) It looks something like this:

  • Connect do device
  • Retrieve services -> and at this point the code hangs up I never found out this bug after first run of my app (first connect). Only at second, etc. connection.

I can’t find any mistakes because one time it works like a charm and other time it has a problem. I found out that disabling and enabling Bluetooth on the phone and retrying connecting works (But I can’t make my users to disable and enable Bluetooth. Or can I 😉 ha-ha).

My code I’ve tried writing my code in 2 ways both have this bug.

async function internalConnect() {
    await BleManager.connect(item.id);
    console.log('after conn');
    await BleManager.retrieveServices(item.id);
    console.log('after retrive');  // <---if the bug appears I never get to see this log
    await BleManager.write(
      item.id,
      global.GATTS_SECURITY_UUID,
      global.GATTC_ACCESS_CODE_UUID,
      stringToIntArray(code),
    );
    console.log('after write');
    await BleManager.read(
      item.id,
      global.GATTS_SECURITY_UUID,
      global.GATTC_ACCESS_CODE_UUID,
    )
      .then(readData => {
        console.log('access code response: ' + readData);
       //My other stuff here
      })
      .catch(error => {
        // Failure code
        console.log(error);
      });
  }

And this way:

BleManager.connect(item.id)
      .then(() => {
        console.log('connected to device');
        setTimeout(() => {
          BleManager.isPeripheralConnected(item.id, []).then(isConnected => {
            //do not remove
            if (isConnected) {
              console.log('Peripheral is connected!');
            } else {
              console.log('Peripheral is NOT connected!');
              Connect(); //<--- run the same function
              return;
            }
          });
        }, 900);
        setTimeout(() => {
          console.log('timeout');

          BleManager.retrieveServices(item.id)
            .then(PeripheralInfo => {
              console.log(PeripheralInfo);

              BleManager.write(
                item.id,
                global.GATTS_SECURITY_UUID,
                global.GATTC_ACCESS_CODE_UUID,
                stringToIntArray(code),
              )
                .then(() => {
                  console.log('written');
                  BleManager.read(
                    item.id,
                    global.GATTS_SECURITY_UUID,
                    global.GATTC_ACCESS_CODE_UUID,
                  )
                    .then(readData => {
                      console.log('access code response: ' + readData);
                      //My stuff here
                    })
                    .catch(error => {
                      handleConnectionErrors(error);
                      return;
                    });
                })
                .catch(error => {
                  handleConnectionErrors(error);
                  return;
                });
            })
            .catch(error => {
              handleConnectionErrors(error);
              return;
            });
        }, 900);
      })
      .catch(error => {
        handleConnectionErrors(error);
        return;
      });

In this way when the bug appears after running isPeripheralConnected I get Peripheral is NOT connected! So I’ve tried to resolve this by running this function again, but it just loops in this place and doesn’t work.

How to Reproduce Accually I don’t know. It appears so random, I can’t control it. I only know that it appears at second or next connections.

Smartphone:

  • Device: xiaomi redmi note 7
  • OS: android 10 QKQ1.190910.002
  • react-native-cli: 2.0.1
  • react-native: 0.66.4
  • ble-manager: 8.0.1s

Btw, I really like this library. It’s really intuitive 😃

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:5
  • Comments:8

github_iconTop GitHub Comments

3reactions
manxhongcommented, Jun 3, 2022

@Silvesterrr I had the same hanging issue with Android only, iOS works perfectly fine to me. There is a workaround I did that solves the issue. Try to createBond before the connect :

if (Platform.OS === 'android') {
   await BleManager.createBond(item.id);
  // you may want to add some delay after create bond
   await BleManager.connect(item.id);
}

Hope this helps.

2reactions
sarevok89commented, Sep 29, 2022

Is there any update on this issue? I’m currently struggling with the same issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[web-bluetooth] getPrimaryService() not resolving appropriately
Immediately try to connect to the device again. - May need to repeat this a few times, but eventually the connection process hangs....
Read more >
How to use the react-native-ble-manager.connect function in ...
To help you get started, we've selected a few react-native-ble-manager.connect examples, based on popular ways it is used in public projects.
Read more >
BLE Web Disconnecting | Gatt server error - Arduino Forum
connect" information, which is expected, but it gets stuck. After some time (30 seconds ish) it returns an error: Connection failed! DOMExection ...
Read more >
Some processes stuck on executing – Forums - BatchPatch
The client has been installed on the target computer without issue. ... the command prompt and make sure you have it running/working first....
Read more >
Web Bluetooth Failure - Bangle.js - Espruino Forum
Cannot retrieve services. (Re)connect first with `device.gatt.connect`. The app loader logs "NetworkError: Connection failed for unknown ...
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