当进入后台时,BLE扫描会停止吗?

4

我正在扫描硬件设备。

当应用程序在前台时,我可以看到设备。

当我在前台开始扫描,然后转到后台,然后打开硬件-BLE,扫描停止,应用程序无法检测到硬件。

如果我将应用程序再次切换到前台,它将继续扫描并找到设备。

我已经设置了以下键:

  <string>bluetooth-peripheral</string>
        <string>bluetooth-central</string>

还有后台模式:

location updates
external accessory communication
uses BLE accessories
acts as BLE ACCESSORY

但是什么都没有发生。系统不允许我在后台检测设备,只能在前台进行检测。
我错过了什么?
2个回答

0
发现必须在后台设置要查找的服务 ID,否则它将无法工作。无论如何,在后台扫描非常缓慢,可能需要几分钟才能检测到!
 [_centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:DEVICE_INFO_SERVICE_UUID]]

0

您可以仅针对特定的serviceUUID在后台扫描BLE,就像我们在前台扫描时传递nil一样。

此外,请确保您在主线程中启动BLE扫描并设置以下正确选项。

 NSArray *services = @[[CBUUID UUIDWithString:UUID1],[CBUUID UUIDWithString:UUID2],[CBUUID UUIDWithString:UUID3]];
dispatch_async(dispatch_get_main_queue(), ^{
    [self.coreBluetoothManager scanForPeripheralsWithServices:services options:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber  numberWithBool:YES], CBCentralManagerScanOptionAllowDuplicatesKey, nil]];
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接