I am developing an application which is acts bluetooth-central and must scan for peripherals in the foreground as well as in background mode. I have enabled “Uses Bluetooth LE accessory” background mode in Xcode capabilities. After much research, I decided to check if scanning for peripherals, based on ServiceUUIDs, was possible in background mode. After some tests, I was able to scan in background mode. But my problem is that after a few minutes (about 10 min approx or sometimes it’s 25 min) my application automatically stops calling didDiscover peripheral method and terminated automatically. But I need to scan for peripherals in background mode as well and fire some local notifications when some values of particular BLE peripheral will get updated. So for implementing this feature, I need to scan and observe continuously for certain BLE devices even application is in background mode or locked screen by user. Please suggest.
Asked
Active
Viewed 106 times
0
-
Have you opted in to core Bluetooth state restoration? This allows iOS to relaunch your app when a peripheral (that you haven't previously discovered) that is advertising the service you are looking for is discovered – Paulw11 Feb 03 '20 at 10:16
-
Actually I am not using connection with peripherals, My app is only uses scanning. – Ashvini Feb 03 '20 at 10:19
-
Yes, state restoration works for pending connections and peripheral discovery – Paulw11 Feb 03 '20 at 10:27
-
I only need to scan and check for updated values of BLE peripheral devices. So please suggest me that how can I use state restoration into my code? – Ashvini Feb 03 '20 at 10:31
-
If you are scanning for peripherals that advertise a specific service and a peripheral is discovered that is advertising that service then your app will be relaunched; you re-establish your `CBCentralManager` and the delegate method will be called with the newly discovered peripheral. Apple's Core Bluetooth Programming Guide describes state restoration – Paulw11 Feb 03 '20 at 10:34
-
When my application gets re-launched then already scheduled local notifications gets missing in this case or triggered even if the values get changed by peripheral, this is what my problem is, so I need to keep my application get calling didDiscover peripheral method and updating and calculating values inside my ViewController and on the basis of some comparisons, local notifications should get fired, so is that possible to implement with Background mode? – Ashvini Feb 03 '20 at 10:44
-
No. You won't get notifications for peripherals you have previously discovered. You really need to maintain a connection to the peripheral and have it issue notifications when characteristics change. Obviously you need to persist any data that you depend upon across launches of your app; values wont be held in memory but there is no reason that scheduled local notifications should be lost – Paulw11 Feb 03 '20 at 10:52