react-nativeexpobluetooth-lowenergyreact-native-ble-plx

IOS react-native-ble-plx background scanning works only after toggling bluetooth on/off in settings


I'm running my app based on Expo SDK 51. Scanning is started to search for devices with a specific serviceUUID. Everything works perfectly and the react-native-ble-plx lib finds and connects to my devices in the foreground. My problem is when the app is transitioned to background while scanning.

The device I want to connect to is broadcasting but the app doesn't connect.

However, if I enter IOS settings and toggle BT on/off while the app is in the background scanning the app connects to the device correctly.

I also noticed that starting the NRF Scanner Utility App scan function also triggers my app, while in the background, to connect to the device the same way as IOS settings BT on/off (the app being in the background all the time).

It's like something low-level is "waking up" all the way to the app when fiddling around with other applications that are using the phone's BT stack. Has anyone seen this behavior?


Solution

  • The solution to the problem.

    1. My device was advertising the serviceUID in the ScanResponse (secondary message of advertising requested by central). However, in IOS background mode the phone doesn't request the ScanResponse message hence the serviceUID was never known to the phone in background mode. Moving the serviceUID over to the primary advertising message in the device solved it.

    2. Background scanning worked for a couple of connects then it started to take very long time between advertising start (in the device) until it appeared in IOS. The solution: Restart scanning for devices shortly after a connected device has disconnected. Seems like this overcomes the IOS background limitation of allowDuplicates.