一、当应用进入后台运行,收到订阅的特征值改变通知后如何弹出系统弹窗。值得注意的就是,每次特征值发生改变都会调出弹窗,不够友好。
// 发起连接时,设置 CBConnectPeripheralOptionNotifyOnNotificationKey
let options = [CBConnectPeripheralOptionNotifyOnNotificationKey: true]
centralManager.connect(peripheral, options: options)
二、应用在后台运行,如何在后台接收处理收到的通知。需要在info.plist添加权限
<key>UIBackgroundModes</key>
<array>
<string>bluetooth-central</string>
</array>
三、连接状态的保存和恢复
// 初始化CBCentralManager时通过设置 CBCentralManagerOptionRestoreIdentifierKey,系统会自动保存状态
centeralManager = CBCentralManager.init(delegate: self, queue: nil ,options:[CBCentralManagerOptionRestoreIdentifierKey: "myCentralManagermy"])
// 当APP重新从后台启动,会走代理,然后快速的恢复之前的连接状态(走连接代理,但会直接通过),并且恢复连接状态
func centralManager(_ central: CBCentralManager, willRestoreState dict: [String : Any]) {
appendCenteralInfo(content: "状态重连willRestoreState \(dict)")
}
网友评论