Swift中的BLE背景扫描 [英] Background Scanning for BLE in Swift

查看:352
本文介绍了Swift中的BLE背景扫描的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让我的应用程序在后台扫描BLE设备,并在Swift中搜索一些广告数据。我一直无法在这里找到任何有关此内容的教程或问题。



基本上,当应用程序没有时,有没有办法在后台自动执行此操作t在前台,当用户重新启动手机时?:



第2步:确保将相应的内容添加到您的info.plist文件中





如果没有添加它,这是plist代码:

 < 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)

现在,如果你关心选项,你可以传递一个选项词典来代替我在上面传递的nil。大多数情况下,这用于指定在连接之前是否要连续查看设备RSSI,或者只是想要广告数据包一次。放一个:

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

在didDiscoverPeripheral委托方法中观察不同的行为。



如果您想要重复的密钥,以下是您将传递的词典:

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

现在 Apple说当你的应用去了在后台模式它默认此扫描选项为false但从iOS 8.3开始我看到它继续扫描w ith重复的钥匙。



关于后台执行的最后一点说明。如果iOS决定暂停您的应用,扫描将停止。如果有一堆应用程序正在运行,我已经看到这种情况会在30秒内发生。


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. 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

I hope you can point me in the right direction. Thank you

解决方案

Step 1: Enable bluetooth background mode for your projects capabilities

Step 2: Make sure the appropriate stuff was added to your info.plist file

Here is the plist code if it didn't add it:

<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. You can't pass it an empty array. It will still scan if you pass nil, just not in the background.

So specify an array of service UUID's like this:

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. 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. Put a:

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

in the "didDiscoverPeripheral" delegate method to observe the different behavior.

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.

One final note on background execution. If the iOS decides to suspend your app the scanning stops. I have seen this happen as soon as 30 seconds if there are a bunch of apps running.

这篇关于Swift中的BLE背景扫描的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆