user1068895_CHU
user1068895_CHU

Reputation: 251

CoreBluetooth Background Scan Device

I am currently working on an app that requires me to scan a device using Bluetooth 4.0. At this point I have encounter a problem.

Is it possible to have the app scan the device when the app is running in the background? For some reason, the app cannot scan the device. The app did not call didDiscoverPeripheral while it was running in the background. However, my app can receive data from the device once connection has been establish.

To make things simpler, why can't my app scan the device while it is running in the background?

P.S. I did input the Required background modes 1.App communicates using CoreBluetooth 2.App shares data using CoreBluetooth

Upvotes: 5

Views: 2189

Answers (3)

Big-O Claire
Big-O Claire

Reputation: 956

Another thing that may be an issue is that you must scan for specific services, like so:

    [myCentralManager scanForPeripheralsWithServices:@[serviceUUID] options:nil];

rather than having that "with services" parameter be nil. I think this is a huge gotcha and is barely whispered in Apple's documentation, only mentioned in the description of that method, and nowhere in the Core Bluetooth Backgrounding document.

Upvotes: 0

allprog
allprog

Reputation: 16780

Scanning in the background is a very slow process. Apple says it's 55 times slower than in the foreground. My experience shows that it can be even slower. So in order to scan for a device in background keep in mind the following:

  1. Make sure the other device is advertising at max speed (20ms) and advertises the services that you are scanning for. There is an example in this answer for that: https://stackoverflow.com/a/18113505/768935
  2. Due to the reduced scanning speed, give much more time for the app to find the device.
  3. Make sure you start scanning with the services specified. You cannot scan in the background for nil services.
  4. The allow duplicates option for scanning is ignored in the background.

Upvotes: 6

henrik
henrik

Reputation: 1

Put service or data in advertisement package! Also set advertisement periode fast if you want the iPhone to discover it faster. I use 20 ms for first 30 s. br henrik

Upvotes: 0

Related Questions