Running Bluetooth (BLE) tasks in background, with React Native

Currently working on an emergency app paired with a BLE device, and the desired use case is: When the device is triggered, it sends your location to your emergency contacts an API we've built.

This flow works while the app is open, but we need things to obviously work while in background (while the phone is sleeping as well, of course, for emergency contexts)

From what I've researched and understood, background fetches don't really work, because the intervals are a maximum of about 15 minutes, and iOS will make them less frequent based on app usage, and that window in an emergency situation isn't good enough.

Having read around, I've bumped into a couple resources that suggest background bluetooth processing is possible, if I listen for a particular service being advertised, but I haven't been able to make things work so far. I wanted some help on this.

Replies

The correct way to implement such a solution with a BLE accessory would be to use the Core Bluetooth Background Processing mechanism to wake up or launch your app when there is an activity. Depending on your use case and the delay you are able to accept, you would either keep a constant connection between the phone and the accessory, and the accessory would notify on a characteristic (fast but battery expensive), or the two would stay disconnected, and on a trigger condition, the accessory would start advertising, causing the app to connect and then they could do what needs to be done (will take longer but more power efficient)

How to implement this is explained here: https://developer.apple.com/library/archive/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html#//apple_ref/doc/uid/TP40013257-CH7-SW1

The document references native code. How to implement this in React Native will be left as an exercise.

Yep, useful docs for sure [@Gualtier Malde](https://developer.apple.com/forums/profile/Gualtier Malde) – I came across those already. It seems the RN implementation is the challenge I was particularly looking to solve, and seeing how I can call those methods from React Native, or design the native code correctly and then expose those methods to my React Native via RCT_EXPORT_METHOD (I'm not familiar with native iOS programming, so while I understand the docs in principle, I'm quite confused as to how to go about implementing that, and what methods I need to create & call from my React Native code base).

Any guidance on the specific implementation would be helpful. I know I have to use CBCentralManagerDelegate, and specific methods like peripheral:didDiscoverServices:, but I frankly have little idea on how to use those in context, so I'm trying to figure that out.