Bluetooth lowenergy 应用程序被系统终止时未调用WillRestoreState

Bluetooth lowenergy 应用程序被系统终止时未调用WillRestoreState,bluetooth-lowenergy,core-bluetooth,cbcentralmanager,ios-bluetooth,state-restoration,Bluetooth Lowenergy,Core Bluetooth,Cbcentralmanager,Ios Bluetooth,State Restoration,我正在尝试创建一个连接到BP袖口的应用程序。我想在应用程序中启用状态恢复,这样即使应用程序被系统杀死,当BP cuff可用且正在传输时,应用程序仍然可以被调用。以下是我迄今为止尝试过的一些东西 1) 向centralManager init添加资源标识符并为其分配后台串行队列 var ble_dispatchQueue = DispatchQueue(label: "com.xyz.ios.ble") let opts = [CBCentralManagerOptionShowP

我正在尝试创建一个连接到BP袖口的应用程序。我想在应用程序中启用状态恢复,这样即使应用程序被系统杀死,当BP cuff可用且正在传输时,应用程序仍然可以被调用。以下是我迄今为止尝试过的一些东西

1) 向centralManager init添加资源标识符并为其分配后台串行队列

var ble_dispatchQueue = DispatchQueue(label: "com.xyz.ios.ble")
        let opts = [CBCentralManagerOptionShowPowerAlertKey : true,    CBCentralManagerOptionRestoreIdentifierKey:
            "ios.xyz.ble.peripheral.identifier"] as [String : Any]

        self.centralManager = CBCentralManager(delegate : self, queue : ble_dispatchQueue, options : opts)
2) 实施willRestoreState和CentralManagerDipDateState

   func centralManager(_ central: CBCentralManager, willRestoreState dict: [String : Any]) {

    print("will restore state called")
    if let peripheralsObject = dict[CBCentralManagerRestoredStatePeripheralsKey] {
        // 2
        let peripherals = peripheralsObject as! Array<CBPeripheral>
        // 3
        if peripherals.count > 0 {
            // 4
            self.peripheralDevice = peripherals[0]
            // 5
            self.peripheralDevice?.delegate = self
        }
    }
}
模拟系统的进程终止。我不是在摆弄蓝牙状态/飞行模式或手机重启。 我正在初始化我的centralManager内部,并使用选项启动 我在didLaunchWithOptions有断点,每次BP cuff准备好连接时都会调用该断点,而在willRestoreState则从未调用该断点


有人能建议我还能做些什么来调用willRestoreState吗?

您需要向
CBCentralManager
添加一个还原id,否则将永远不会调用委托方法

let options = [CBCentralManagerOptionRestoreIdentifierKey: "my-central"]
self.centralManager = CBCentralManager(delegate: self, queue: nil, options: options)
完成后,当实例化
CBCentralManager
时,将始终调用
willRestoreState


另外,请确保您的Info.plist中有带有“bluetooth central”的
UIBackgroundModes
(阵列)。

您是否正在使用还原ID实例化CBCentralManager?
kill(getpid(), SIGKILL);
let options = [CBCentralManagerOptionRestoreIdentifierKey: "my-central"]
self.centralManager = CBCentralManager(delegate: self, queue: nil, options: options)