简体   繁体   English

iOS蓝牙scanForPeripheralsWithServices后台运行仅在某些版本上有效吗?

[英]iOS Bluetooth scanForPeripheralsWithServices background running works only on certain builds?

I had a working system where I was able to do peripheral scanning in the background. 我有一个工作系统,可以在后台进行外围扫描。 After refactoring my code, this functionality has stopped working. 重构代码后,此功能已停止工作。

I thought I had just missed something, but the strange thing is that the refactored version works if I run the two version of the apps (before/after refactoring) together (with different bundle id's). 我以为我只是错过了一些东西,但是奇怪的是,如果我同时运行两个版本的应用程序(在重构之前/之后)(使用不同的包ID),则重构版本可以工作。 Does anyone know what might cause this? 有谁知道这可能是什么原因?

Relevant Sections: 相关章节:

func centralManagerDidUpdateState(_ central: CBCentralManager) {
    print("did update central manager")
    if central.state == .poweredOn {
        print("powered on central manager")
        let scanOptions = [CBCentralManagerScanOptionAllowDuplicatesKey: true]

        central.scanForPeripherals(withServices: [CBUUID(nsuuid: UUID.init(uuidString: "05D27C93-F0FE-43BB-A8CB-DECFED270F8F")!)], options: scanOptions)
    }
}

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
    print(RSSI)
    DataCollector.logBluetoothData(advertisementData, RSSI)
    let uuid = BluetoothModel.extractUUID(advertisementData)
    bluetoothReading[uuid] = (Date().timeIntervalSince1970, Double(truncating: RSSI))
}

I feel like it might have something to do with the timing between when the user grants permission to listen in the background and when it begins the listening, but I don't think this is the case based on the previous version working the way it does. 我觉得这可能与用户在后台授予监听权限到开始监听之间的时间有关,但是基于以前的版本,我认为情况并非如此。

Issue Resolved. 问题解决了。 It turns out that you can continue to monitor it, if you are also ranging for iBeacon devices. 事实证明,如果您还适用于iBeacon设备,则可以继续对其进行监视。

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

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