簡體   English   中英

應用終止時,CoreBluetooth建立外圍設備連接

[英]CoreBluetooth establish peripheral connection when app is terminated

我有一個iOS應用程序,它充當通過CoreBluetooth連接到外部peripheralcentral peripheral

連接到peripheral ,我對其進行讀寫數據,然后斷開連接。 斷開連接后,我嘗試再次建立連接,以便下次peripheral進入advertise模式時,應用程序將以如下方式連接至該連接:

func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
    central.connect(peripheral, options: nil)
    // More actions here...
}

當應用程序位於foreground / background時,它可以很好地工作,但是當terminated ,它根本無法工作。

我已經用CBCentralManagerOptionRestoreIdentifierKey定義了CBCentralManagerCBCentralManagerOptionRestoreIdentifierKey所示:

self.central = CBCentralManager(delegate: self, queue: nil, options: [CBCentralManagerOptionRestoreIdentifierKey: Application.bluetoothRestoreIdentifier])

我還實現了willRestoreState中央委托方法:

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

我究竟做錯了什么?

非常感謝!

嘗試以下代碼

func centralManager(_ central: CBCentralManager, willRestoreState dict: [String : Any]) {
        let connectedperipherals = dict[CBCentralManagerRestoredStatePeripheralsKey] as? [CBPeripheral]
        if let peripheral = connectedperipherals?.first {
            central.connect(peripheral, options: nil)
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM