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