简体   繁体   中英

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?

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

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM