简体   繁体   English

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

[英]Scan for BLE peripheral in background - iOS

I am looking for a solution to scan for BLE peripherals while the app is in background state through an iOS device.我正在寻找一种解决方案,在应用程序通过 iOS 设备处于后台状态时扫描 BLE 外围设备。 I have tried checking the capabilities, adding central and peripheral in info.plist for background process, creating a singleton CBCentralManager.我尝试检查功能,在 info.plist 中为后台进程添加中央和外围设备,创建一个单例 CBCentralManager。 It scans and connects to the BLE devices while in foreground but once it goes to background, it never calls the didDiscover method.它在前台扫描并连接到 BLE 设备,但一旦进入后台,它就不会调用 didDiscover 方法。 Is there any solution fo the same?有没有相同的解决方案? Thanks in advance.提前致谢。

My info.plist我的信息.plist

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

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

I have also called the scanPeripheral with a service as you have mentioned.正如您所提到的,我还使用服务调用了 scanPeripheral。

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

I also tried using allow duplicates true/false for the options for scanPeripherals and also retrievePeripherals delegates too.我还尝试对 scanPeripherals 的选项使用 allow duplicates true/false,并且还尝试使用 retrievePeripherals 委托。

You are almost certainly passing nil as the forServices parameter of scanForPeripherals .您几乎可以肯定将nil作为 scanForPeripherals 的forServices参数scanForPeripherals This is not allowed in the background. 这在后台是不允许的。

Your app can scan for Bluetooth devices in the background by specifying the bluetooth-central background mode.您的应用可以通过指定 bluetooth-central 后台模式在后台扫描蓝牙设备。 To do this, your app must explicitly scan for one or more services by specifying them in the serviceUUIDs parameter.为此,您的应用必须通过在 serviceUUIDs 参数中指定一项或多项服务来显式扫描它们。 The CBCentralManager scan option has no effect while scanning in the background. CBCentralManager 扫描选项在后台扫描时无效。

You also will not receive duplicates while in the background, even if you request them with CBCentralManagerScanOptionAllowDuplicatesKey.即使您使用 CBCentralManagerScanOptionAllowDuplicatesKey 请求它们,您也不会在后台收到重复的。

You will need to determine the list of service UUIDs you want to scan for, and pass those to scanForPeripherals .您需要确定要扫描的服务 UUID 列表,并将它们传递给scanForPeripherals

I was able to get background connection working by doing the following:通过执行以下操作,我能够使后台连接正常工作:

  1. In the didDisconnectPeripheral , I called a function startScanning .didDisconnectPeripheral中,我调用了一个函数startScanning Inside startScanning is where I have myCentral.scanForPeripherals(withServices: [SERVICE_UUID], options: [CBCentralManagerScanOptionAllowDuplicatesKey : false])startScanning里面我有myCentral.scanForPeripherals(withServices: [SERVICE_UUID], options: [CBCentralManagerScanOptionAllowDuplicatesKey : false])

  2. Nothing was working until I added the following above scanForPeripherals :在我在上面添加以下scanForPeripherals之前,什么都没有工作:

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

Now all seems to be working as it should.现在一切似乎都在正常工作。

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

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