我正在制作一个关于在背景模式下扫描BLE设备的应用程序,并每分钟将设备列表发送到服务器。
private let uuid = CBUUID(string: "DEC18772-CCC0-462D-92FB-F5C823537895")
self.centralManager?.scanForPeripherals(withServices: [uuid], options: [CBCentralManagerScanOptionAllowDuplicatesKey : true])
在plist文件中
<array>
<string>bluetooth-central</string>
<string>bluetooth-peripheral</string>
</array>我使用计时器在60秒后将设备列表发送到服务器。
var timer = Timer.scheduledTimer(timeInterval: 60, target: self, selector: #selector(runTimedCode), userInfo: nil, repeats: true)
但是,当我使应用程序进入后台模式时,一切似乎都暂停了,计时器和didDiscover回拨不起作用。
有没有什么东西可以让应用程序在后台模式下运行?
发布于 2020-03-15 04:32:06
你试图做的事情面临着几个问题:
Timer在应用程序挂起时不会触发;在iOS上执行特定句点并不是受支持的后台模式。CBCentralManagerScanOptionAllowDuplicatesKey。由于您在Info.plist中选择了蓝牙后台模式,假设用户授予蓝牙后台权限,每次看到一个新设备正在为指定的服务(DEC18772-CCC0-462D-92FB-F5C823537895)做广告时,您都会收到一个didDiscover委托回调。
您可以在此回调中向服务器报告此设备的发现。
当您的应用程序处于后台时,您将不会收到该设备的任何额外委托发现回调。
https://stackoverflow.com/questions/60689171
复制相似问题