iOS Background Bluetooth: BLE scanning when iPhone is locked

I am looking for someone from the team at Apple to help me with a bluetooth issue we are experiencing.

I am part of a team that is building a product line with a bluetooth device that requires us to use background bluetooth scanning. The device sends BLE packets periodically and the phone receives these packets and sends the data to our DB. We were able to get background bluetooth packets receiving (although sporadically) on the phone. However we cannot receive any packets while the app is backgrounded and the phone is locked.

So my question is two fold:
  1. This is more critical: Is there any way to allow scanning while the app is backgrounded and the phone is locked?

  2. This is secondary: Is there anyway to make background scanning more reliable/predictable so that we can get consistent readings in the background

Answered by edorphy in 618692022
  1. Yes, CoreBluetooth is designed to work while the app is in the background. Be sure to check the appropriate background mode capability in your target's capabilities tab. Watch some of the old WWDC videos on Core Bluetooth. There are limitations in background, i.e. you must be scanning for one or more UUIDs--scans without CBUUID's do not invoke callbacks when the app is in the background state. Based on what you've described sounds like you'd want to check the box "Uses Bluetooth LE accessories".

  2. As far as predictable in background goes, this is an older doc but should still apply. Please refer to the Bluetooth accessory guidelines. Take note of the advertisement intervals that are recommended to aid with background discovery.

https://developer.apple.com/accessories/Accessory-Design-Guidelines.pdf

I will make the assumption that your BLE product advertises frequently and the app would connect and then retrieve data from the device? OR your advertisement payload contains the data you're looking for?
Accepted Answer
  1. Yes, CoreBluetooth is designed to work while the app is in the background. Be sure to check the appropriate background mode capability in your target's capabilities tab. Watch some of the old WWDC videos on Core Bluetooth. There are limitations in background, i.e. you must be scanning for one or more UUIDs--scans without CBUUID's do not invoke callbacks when the app is in the background state. Based on what you've described sounds like you'd want to check the box "Uses Bluetooth LE accessories".

  2. As far as predictable in background goes, this is an older doc but should still apply. Please refer to the Bluetooth accessory guidelines. Take note of the advertisement intervals that are recommended to aid with background discovery.

https://developer.apple.com/accessories/Accessory-Design-Guidelines.pdf

I will make the assumption that your BLE product advertises frequently and the app would connect and then retrieve data from the device? OR your advertisement payload contains the data you're looking for?
Thanks for your response!

For the answer to #1: We have had background bluetooth working for several weeks now using the exact method you mentioned and are able to get the packet when the phone screen is on and unlocked. However, I am more concerned with replicating this behavior while the iPhone is LOCKED. Right now, if the phone is locked, NO packets come through. The discovery does not work the same as when the phone screen is on. Is there something we need to do to get the background bluetooth to operate the same when the phone us locked and unlocked? This is critical as we need the data to flow consistently and many people have their phones locked and in their pockets for a large portion of the day!

Thanks for the article for #2! We will dig into that!
@Lebets_VI
Did you find any solution on this.
And do you know when app is in background or inactive can we just search the near by bluetooth devices.

@Lebets_VI and @VishalWagh
Did you find a solution for communicating when the phone is locked?

@Lebets_VI and @VishalWagh@lazyshankar Did you able to figure out the BLE scanner issue when the screen is locked?

It's not 100% clear from your description but it sounds like you're trying to take readings from a series of advertisements. You may be running into this restriction imposed by iOS:

In particular, when your app is scanning for device while in the background:

  • The CBCentralManagerScanOptionAllowDuplicatesKey scan option key is ignored, and multiple discoveries of an advertising peripheral are coalesced into a single discovery event.

In my testing, actual behaviour seems to vary—sometimes I really do get just one event, sometimes I get an occasional trickle. This may be what you're experiencing with the difference between "backgrounded" and "backgrounded and locked". Either way the rule has been laid down: we can't rely on scanning more than one advertisement when the app is in the background. I am aware of no workarounds.

The solution supported by iOS is to use that single advertisement to connect to the device and then subscribe to the relevant changing characteristic. If you have the relevant background mode configured, iOS is quite willing to allow this. This does require your peripheral to support a different mode of operation of course.

Hello, @Lebets_VI , did you find what you were looking for? I am having the same issue keeping my app runig in the background when I receive a packet.

iOS Background Bluetooth: BLE scanning when iPhone is locked
 
 
Q