我想让我的应用程序在后台扫描BLE设备,并使用Swift搜索一些广告数据。我找不到任何涵盖这方面内容的教程或问题解答。基本上,当应用程序不在前台且用户已重新启动他们的手机时,是否有自动执行此操作的方法?希望你能指点我正确的方向。谢谢。参考链接:Obtaining Bluetooth LE scan response data with iOS
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
<string>bluetooth-central</string>
</array>
当你在 CBCentralmanager 上调用 "scanForPeripheralsWithServices" 时,你需要指定一个服务数组来扫描。你不能传递一个空数组。如果你传递 nil,它仍然会扫描,只是不会在后台扫描。
因此,请像这样指定一个服务 UUID 的数组:
let arrayOfServices: [CBUUID] = [CBUUID(string: "8888")]
self.myBluetoothManager?.scanForPeripheralsWithServices(arrayOfServices, options: nil)
println(advertisementData["kCBAdvDataLocalName"] as! String)
println(advertisementData["kCBAdvDataManufacturerData"] as! NSData)
在“didDiscoverPeripheral”委托方法中观察不同的行为。
如果您希望具有重复键,则将传递以下字典:
let dictionaryOfOptions = [CBCentralManagerScanOptionAllowDuplicatesKey : true]
self.myBluetoothManager?.scanForPeripheralsWithServices(arrayOfServices, options: dictionaryOfOptions)