[英]iOS CoreBluetooth: active connection to the same target peripheral in app and widget simultaneously?
[英]CoreBluetooth establish peripheral connection when app is terminated
我有一個iOS應用程序,它充當通過CoreBluetooth連接到外部peripheral
的central
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
定義了CBCentralManager
, CBCentralManagerOptionRestoreIdentifierKey
所示:
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.