CoreBluetooth - centralManagers method didDiscoverPeripheral becomes least frequent to nothing in sleep mode

StackOverflow https://stackoverflow.com/questions/23551600

Question

I am using CoreBluetooth and the callback method didDiscoverPeripheral gets called frequently when the app is foregrounded or backgrounded or the device is locked with device screen still turned on. But as soon as the screen is turned off / blacked out (Sleep mode) its callback frequency is reduced to almost nothing.

What might be causing this? The device battery is full and Bluetooth turned on.

Please help me with this issue.

Thanks.

Was it helpful?

Solution 2

I was unable to find any direct answers as to why the iOS behaves this way. Although I have found an alternative way to resolve this.

I am implementing a PeripheralManager along with the CentralManager. That way even though the iOS device's screen is turned off, i have control of the iOS device.

OTHER TIPS

Two main things you should be aware of regarding CoreBluetooth and background operation:

1.) The CBCentralManagerScanOptionAllowDuplicatesKeyflag is ignored after an app is put into the background. If you saw a peripheral while in the foreground, you will not get another didDiscover callback for that same peripheral in the background (during the same scanning period). You can test this by having your peripheral powered off, and only powering on after your central has been sitting in the background for some time. You should then receive the callback.

2.) The other issue is that callback time for peripheral discovery is throttled down in the when in background. If for example, you would discover a peripheral in 1 second in the foreground. It could take as long as 60 seconds to discover that same peripheral in the background.

tdevoy is correct, however, I would give slightly more optimistic time-to-discovery values. In my experience it requires only 1-2 seconds for discovery when either one or both devices are in the backgrounded/suspended state.

If you are not receiving callbacks when your app is in the background then there is a good chance your code is slightly wrong. In the WWDC 2013 CoreBluetooth video the engineers provide explicit instructions about how to achieve the behavior you desire. Once you are able to receive the callbacks you could schedule and present a UILocalNotification which would give similar behavior as receiving a text message.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top