繁体   English   中英

在后台扫描 BLE 外围设备 - iOS

[英]Scan for BLE peripheral in background - iOS

我正在寻找一种解决方案,在应用程序通过 iOS 设备处于后台状态时扫描 BLE 外围设备。 我尝试检查功能,在 info.plist 中为后台进程添加中央和外围设备,创建一个单例 CBCentralManager。 它在前台扫描并连接到 BLE 设备,但一旦进入后台,它就不会调用 didDiscover 方法。 有没有相同的解决方案? 提前致谢。

我的信息.plist

<array>
    <string>bluetooth-central</string>
    <string>bluetooth-peripheral</string>
    <string>fetch</string>
    <string>location</string>
    <string>processing</string>
    <string>remote-notification</string>
</array>

我的能力在此处输入图像描述

正如您所提到的,我还使用服务调用了 scanPeripheral。

centralManager.scanForPeripherals(withServices: [CBUUID(string: "6T5FFJJIL-B5A3-D839-LDKL-KJBLKJ33")])

我还尝试对 scanPeripherals 的选项使用 allow duplicates true/false,并且还尝试使用 retrievePeripherals 委托。

您几乎可以肯定将nil作为 scanForPeripherals 的forServices参数scanForPeripherals 这在后台是不允许的。

您的应用可以通过指定 bluetooth-central 后台模式在后台扫描蓝牙设备。 为此,您的应用必须通过在 serviceUUIDs 参数中指定一项或多项服务来显式扫描它们。 CBCentralManager 扫描选项在后台扫描时无效。

即使您使用 CBCentralManagerScanOptionAllowDuplicatesKey 请求它们,您也不会在后台收到重复的。

您需要确定要扫描的服务 UUID 列表,并将它们传递给scanForPeripherals

通过执行以下操作,我能够使后台连接正常工作:

  1. didDisconnectPeripheral中,我调用了一个函数startScanning startScanning里面我有myCentral.scanForPeripherals(withServices: [SERVICE_UUID], options: [CBCentralManagerScanOptionAllowDuplicatesKey : false])

  2. 在我在上面添加以下scanForPeripherals之前,什么都没有工作:

     if let savedPeripheral = self.myPeripheral { if savedPeripheral.state == .disconnected { self.myCentral.connect(savedPeripheral) } }

现在一切似乎都在正常工作。

暂无
暂无

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

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