简体   繁体   English

CoreBluetooth后台扫描设备

[英]CoreBluetooth Background Scan Device

I am currently working on an app that requires me to scan a device using Bluetooth 4.0. 我目前正在开发一个需要我使用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. 该应用程序在后台运行时未调用didDiscoverPeripheral。 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 PS我确实输入了必需的背景模式1.App使用CoreBluetooth进行通信2.App使用CoreBluetooth共享数据

Scanning in the background is a very slow process. 在后台扫描是一个非常缓慢的过程。 Apple says it's 55 times slower than in the foreground. 苹果表示,它比前台慢了55倍。 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. 确保另一台设备以最大速度(20ms)投放广告,并投放正在扫描的服务。 There is an example in this answer for that: https://stackoverflow.com/a/18113505/768935 此答案中有一个示例: 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. 您不能在后台扫描nil服务。
  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. 如果您希望iPhone更快地发现广告,请同时设置广告周期。 I use 20 ms for first 30 s. 我在前30秒使用20毫秒。 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. 而不是将“ with services”参数设为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. 我认为这是一个巨大的难题,并且在Apple文档中几乎没有低声说过,仅在该方法的说明中提到过,而在Core Bluetooth Backgrounding文档中却没有提及。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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