4

I'm new to bluetooth and to react native as well and I've encountered a problem that I've spent days on but I haven't gotten anywhere.

I'm developing an app using the react-native-ble-plx library, but I encountered the same issue using react-native-ble-manager. I've been given a requirement that we need to have security level 3 with numeric comparison and in one of the first screens in the app the user should scan a QR code on the printer which gives the bluetooth library scanner the name it should look for so the user shouldn't have to see any other devices.

This works fine but the problem is when the pairing starts, the app should wait until pairing has been completed before going to the next screen but I haven't seen any way to do this. So what happens is that as soon as the device is connected and services are retrieved, the next then-function gets triggered even if the user aborted the pairing because technically I guess the connection is still active because its waiting for the pairing to complete. I've marked out what I mean in a code example.

So summarize, is there a way to wait for a successful pairing before allowing the user to the next stage? They'll be potentially changing to many different devices all on the same day so I can't really count on them just having to successfully pair just once and it'll be fine. Thanks!

bleManager.startDeviceScan(
        null,
        {scanMode: ScanMode.LowLatency},
        (err, device) => {
          if (err) {
            setLoading(false);
            return;
          }

          if (device && device.name === printer.printerName) {
            bleManager.stopDeviceScan();
            device
              .connect({timeout: 10000})
              .then((d) => {
                return d.discoverAllServicesAndCharacteristics();
              })
              .then((d) => {
                //THIS SHOULD ONLY HAPPEN IF PAIRING WAS COMPLETED
                dispatch({
                  type: 'SET_PRINTER',
                  payload: {
                    printerId: d.id,
                    printerName: printer.printerName,
                    isBluetooth: printer.isBluetooth,
                    printerNetworkId: printer.id,
                  },
                });

                setLoading(false);

                navigation.canGoBack()
                  ? navigation.goBack()
                  : navigation.navigate(patientRoute);
              })
              .catch((error) => {
                console.log(error);
                setLoading(false);
              });
Michael Kotzjan
  • 2,093
  • 2
  • 14
  • 23
Xenofono
  • 169
  • 2
  • 9
  • 2
    I don't know about react but on Android the API to use is this one: https://developer.android.com/reference/android/bluetooth/BluetoothDevice#ACTION_BOND_STATE_CHANGED. Set up a BroadcastReceiver that listens to that intent. – Emil Mar 01 '21 at 08:49
  • Thanks, sadly I don't see anything about that in the react native library. – Xenofono Mar 01 '21 at 10:34
  • I'm using the same lib, but I'm not sure the difference between connecting and pairing, is it server/client or just client-side?. Something that might help is testing using the Nordic NRF Connect App, it has many debug capabilities to fully understand your device. – Gustavo Garcia Mar 21 '21 at 21:08

0 Answers0