Skip to content

Stuck on retrieveServices (not connected after connection?) #831

@Silvesterrr

Description

@Silvesterrr

Describtion
Hi, I've got this annoying bug it appears inconsistently every few times. It's really similar to this Issue [#402]
(#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 :)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions