简体   繁体   English

Swift中的BLE背景扫描

[英]Background Scanning for BLE in Swift

I am attempting to have my app scan for BLE devices in the background and to search for a bit of advertisement data in Swift. 我试图让我的应用程序在后台扫描BLE设备,并在Swift中搜索一些广告数据。 I've been unable to find any tutorials or questions on here that cover this. 我一直无法找到任何有关此内容的教程或问题。

Basically, is there a way of doing this automatically in the background when the app isn't in the foreground and when the user has restarted their phone?: Obtaining Bluetooth LE scan response data with iOS 基本上,当应用程序不在前台并且用户重新启动手机时,有没有办法在后台自动执行此操作?: 使用iOS获取蓝牙LE扫描响应数据

I hope you can point me in the right direction. 我希望你能指出我正确的方向。 Thank you 谢谢

Step 1: Enable bluetooth background mode for your projects capabilities 第1步:为您的项目功能启用蓝牙背景模式

在此输入图像描述

Step 2: Make sure the appropriate stuff was added to your info.plist file 第2步:确保将相应的内容添加到info.plist文件中

在此输入图像描述

Here is the plist code if it didn't add it: 这是plist代码,如果它没有添加它:

<key>UIBackgroundModes</key>
 <array>
  <string>audio</string>
  <string>bluetooth-central</string>
 </array>

Then, when you call "scanForPeripheralsWithServices" on your CBCentralmanager you have to specify an array of services to scan for. 然后,当您在CBCentralmanager上调用“scanForPeripheralsWithServices”时,您必须指定要扫描的服务数组。 You can't pass it an empty array. 你不能传递一个空数组。 It will still scan if you pass nil, just not in the background. 如果你传递的是nil,它仍会扫描,而不是在后台。

So specify an array of service UUID's like this: 所以指定一个服务UUID的数组是这样的:

let arrayOfServices: [CBUUID] = [CBUUID(string: "8888")]
self.myBluetoothManager?.scanForPeripheralsWithServices(arrayOfServices, options: nil)

Now if you care about options you can pass a dictionary of options in place of the nil I passed above. 现在,如果你关心选项,你可以传递一个选项词典来代替我在上面传递的nil。 Mostly, this is used to specify if you want to continuously see a devices RSSI before you connect or if you just want the advertisement packet once. 大多数情况下,这用于指定您是否要在连接之前连续查看设备RSSI,或者只是想要广告数据包一次。 Put a: 放一个:

println(advertisementData["kCBAdvDataLocalName"] as! String) 
println(advertisementData["kCBAdvDataManufacturerData"] as! NSData)

in the "didDiscoverPeripheral" delegate method to observe the different behavior. 在“didDiscoverPeripheral”委托方法中观察不同的行为。

Here is the Dictionary you will pass if you want duplicate keys: 如果您想要重复键,这是您将传递的词典:

let dictionaryOfOptions = [CBCentralManagerScanOptionAllowDuplicatesKey : true]
self.myBluetoothManager?.scanForPeripheralsWithServices(arrayOfServices, options: dictionaryOfOptions)

Now Apple says that when your app goes in the background mode it defaults this scan option to false but as of iOS 8.3 I have see it keep scanning with duplicate keys. 现在Apple说,当你的应用程序进入后台模式时 ,默认此扫描选项为false,但从iOS 8.3开始,我看到它继续扫描重复键。

One final note on background execution. 关于背景执行的最后一点说明。 If the iOS decides to suspend your app the scanning stops. 如果iOS决定暂停您的应用,扫描将停止。 I have seen this happen as soon as 30 seconds if there are a bunch of apps running. 如果有一堆应用正在运行,我已经看到这种情况会在30秒内发生。

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

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