Wait for successful pairing
See original GitHub issue- I am running the latest version
- I checked the documentation and found no answer
Hi Guys, I posted this on github as well f y i, hope someone can give me some insight!
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);
});
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:5
Top GitHub Comments
I had the same problem of needing to wait until
manager.startDeviceScan()
was done with scanning and connection.The solution is to wrap the
manager.startDeviceScan()
function in a promise and resolve that promise withindevice.connect()
.Here’s an adaption of the official intro code:
This returns
true
once the connection is finished. Of course you can return other things withresolve()
too.You can then call this function in some other part of your application:
Some more info on using Promises.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.