Scanning in background task.
See original GitHub issuePrerequisites
- I am running the latest version
- I checked the documentation and found no answer
- I checked to make sure that this issue has not already been filed
Expected Behavior
I am using react-native-background-fetch to execute a task when the app is in the background. In that task, I should be able to scan for devices.
Current Behavior
I am able to connect, speak with devices, and do anything except for scan for peripherals in the background task I configured. After searching through these issues, I found a related issue here. To confirm that I have all the correct advertised services, in the foreground process I do this:
const my_service = 'c7fcee6f-e7e0-8c8a-b745-fde27c268efd';
manager.startDeviceScan([my_service], {allowDuplicates: true}, (error, device) => {
if (error) {
console.log("Error in device scan: ", JSON.stringify(error));
}
if (device) {
console.log("DEVICE UUIDS: ", device.serviceUUIDs);
}
});
In the console this log is printed:
'DEVICE UUIDS: ', [ '00001805-0000-1000-8000-00805f9b34fb',
'c7fcee6f-e7e0-8c8a-b745-fde27c268efd' ]
Then in the background task, I perform the scanning process with those exact uuids, but without finding any devices this time.
NOTE: the service ‘00001805…’ is the bluetooth current time service. The service id is really just ‘1805’ or ‘0x1805’ but i’m unsure whats the proper way to scan for a service with an assigned number by the bluetooth sig group.
manager.startDeviceScan(['00001805-0000-1000-8000-00805f9b34fb', 'c7fcee6f-e7e0-8c8a-b745-fde27c268efd'], {allowDuplicates: true}, (error, device) => {
console.log("Anything?");
if (error) {
console.log("Error in background device scan: ", JSON.stringify(error));
}
if (device) {
console.log("DEVICE UUIDS: ", device.serviceUUIDs);
console.log("Found device in background scan: ", device.id);
}
});
I am setting log level to verbose. This is the only relevant log I have found:
'CentralManager(10766645056) scanForPeripherals(
withServices: Optional("[00001805-0000-1000-8000-00805F9B34FB, C7FCEE6F-E7E0-8C8A-B745-FDE27C268EFD]"),
options: Optional(["kCBScanOptionAllowDuplicates": true]))'
Library version: 1.0.3 Platform: IOS and Android
I have tried my best to do my due diligence before submitting a bug report. Please let me know what other relevant information I can provide, or what I am missing.
Thank you for your help and excellent library!
As a sidenote, I am pretty unclear on what happens when the app is registered for background modes, and a device or service is reconnected. For instance, in IOS it is recommended to just call connectToDevice(id) when a device disconnects if we want to reconnect. But, lets say, the app performs a data transfer when a component is mounted. If the app is in the background then that component is not mounted and that data transfer is not performed? Because of this confusion is why I am using react-native-background-fetch so I know exactly the code the runs in the background.
Issue Analytics
- State:
- Created 4 years ago
- Comments:14 (4 by maintainers)
Top GitHub Comments
Hi, thanks for your efforts! Is background mode supported by Android too?
Hello! I added a new entry in the Wiki with an example application to show you all available possibilities around background mode on iOS. Checkout page here: https://github.com/Polidea/react-native-ble-plx/wiki/Background-mode-(iOS).