bleManager.startDeviceScan not working on iOS react-native-ble-plx

1.5k Views Asked by At

I am trying to pair a gateway to React native app using the react-native-ble-plx. The below source code works fine in Android wheras in iOS, the bleManager.startDeviceScan() is not triggered. Nothing happens after this step.

Any help is much appreciated!

Source code:

const connectBLE = () => {
    const subscription = bleManager.onStateChange(async (state) => {
      if (state === 'PoweredOn') {
          subscription.remove();
          scanAndConnect();
      } 
  };
}
  const scanAndConnect = () => {
    bleManager.startDeviceScan(null, null, async (error, device) => {
      if (error) {
        showToast(error, 'error');
        console.log('Handle error - scanning will be stopped automatically');
        return;
      }
      console.log('Devices');
      console.log(device.name);
      // Check if it is a device you are looking for based on device name
      if (device.name === "BLE_0006") {
        // Stop scanning as we have found the device.
        bleManager.stopDeviceScan();
        // Establish Device connection.
        device
          .connect()
          .then((deviceData) => {
            /** Show Toast on Device disconnect */
            bleManager.onDeviceDisconnected(
              deviceData.id,
              (connectionError, connectionData) => {
                if (connectionError) {
                  console.log(connectionError);
                }
                console.log('Device is disconnected');
                console.log(connectionData);
              },
            );
            /** Discover All Services and Characteristics */
            return device.discoverAllServicesAndCharacteristics();
          })
          .then(async (deviceObject) => {
            console.log('deviceObject');
            console.log(deviceObject);
            /** Subscribe for the Readable service */
            device.monitorCharacteristicForService(
              Enum.bleConnectionInfo.customServiceUUID,
              Enum.bleConnectionInfo.readCharacteristicUUID,
              (error, characteristic) => {
                if (error) {
                  console.log('Error in monitorCharacteristicForService');
                  console.log(error.message);
                  return;
                }
                console.log(characteristic.uuid, characteristic.value);
                ]);
              },
            );
          })
          .catch((error) => {
            console.warn(error);
            showToast(error, 'error');
          });
      }
    });
  }
1

There are 1 best solutions below

0
On

This might be helpful for someone!

The Issue was resolved by moving the bleManager() initialization outside the functional component.