CoreBluetooth 状态保存和恢复
CoreBluetooth State Preservation and Restoration
我有以下场景:iOS app(外设)X OSX app(中央)
- 我用 CBPeripheralManagerOptionRestoreIdentifierKey 实例化我的外设管理器。
- 在我的外围设备的 didFinishLaunchingWithOptions 中,我在使用 UIApplicationLaunchOptionsBluetoothPeripheralsKey 获取外围设备后发送本地通知(不要对其进行任何操作)
- 在我的外围设备的 willRestoreState 中,我也触发了一个通知(除此之外不要做任何事情)
如果我的外围应用程序在由于内存压力而被杀死之前仍然 运行 在后台,我从 OSX 中心接收消息就好了。
在 iOS 应用程序被杀死后,当 OSX 中央发送一条消息时,上面提到的两个通知都会在 iOS 上通过,但我实际上期待的消息并没有.
我一直都没有重新实例化我的 peripheralManager,我应该在哪里以及如何重新实例化?我的应用程序的整个周期只有一个 peripheralManager。
欢迎提出任何建议。
更新:
如果
let options: Dictionary = [CBPeripheralManagerOptionRestoreIdentifierKey: "myId"]
peripheralManager = CBPeripheralManager(delegate: self, queue: nil, options: options)
在 willRestoreState 中,我的应用程序刚刚失去连接
是的,在重新访问所有主题 100 次之后,我终于想通了,具体应该如何实施:
在 AppDelegate 的 didFinishLaunchingWithOptions 中:
if let options = launchOptions {
if let peripheralManagerIdentifiers: NSArray = options[UIApplicationLaunchOptionsBluetoothPeripheralsKey] as? NSArray {
//Loop through peripheralManagerIdentifiers reinstantiating each of your peripheralManagers
//CBPeripheralManager(delegate: corebluetooth, queue: nil, options: [CBPeripheralManagerOptionRestoreIdentifierKey: "identifierInArray"])
}
else {
//There are no peripheralManagers to be reinstantiated, instantiate them as you normally would
}
}
else {
//There is nothing in launchOptions, instantiate them as you normally would
}
此时,willRestoreState 应该开始被调用,但是,如果您有一个管理所有订阅了您的特性的中心,那么您的中心阵列中将没有中心。由于我所有的中心总是在我拥有的一项服务中订阅我的所有特征,我简单地遍历任何特征中的所有订阅的中心,将它们重新添加到我的中心数组中。
请根据需要修改。
在willRestoreState中:
var services = dict[CBPeripheralManagerRestoredStateServicesKey] as! [CBMutableService]
if let service = services.first {
if let characteristic = service.characteristics?.first as? CBMutableCharacteristic {
for subscribedCentral in characteristic.subscribedCentrals! {
self.cbCentrals.append(subscribedCentral as! CBCentral)
}
}
}
在此之后你应该可以调用你准备好的任何方法来与任何中心对话,即使你同时处理其中的几个。
祝你好运!
我有以下场景:iOS app(外设)X OSX app(中央)
- 我用 CBPeripheralManagerOptionRestoreIdentifierKey 实例化我的外设管理器。
- 在我的外围设备的 didFinishLaunchingWithOptions 中,我在使用 UIApplicationLaunchOptionsBluetoothPeripheralsKey 获取外围设备后发送本地通知(不要对其进行任何操作)
- 在我的外围设备的 willRestoreState 中,我也触发了一个通知(除此之外不要做任何事情)
如果我的外围应用程序在由于内存压力而被杀死之前仍然 运行 在后台,我从 OSX 中心接收消息就好了。
在 iOS 应用程序被杀死后,当 OSX 中央发送一条消息时,上面提到的两个通知都会在 iOS 上通过,但我实际上期待的消息并没有.
我一直都没有重新实例化我的 peripheralManager,我应该在哪里以及如何重新实例化?我的应用程序的整个周期只有一个 peripheralManager。
欢迎提出任何建议。
更新:
如果
let options: Dictionary = [CBPeripheralManagerOptionRestoreIdentifierKey: "myId"]
peripheralManager = CBPeripheralManager(delegate: self, queue: nil, options: options)
在 willRestoreState 中,我的应用程序刚刚失去连接
是的,在重新访问所有主题 100 次之后,我终于想通了,具体应该如何实施:
在 AppDelegate 的 didFinishLaunchingWithOptions 中:
if let options = launchOptions {
if let peripheralManagerIdentifiers: NSArray = options[UIApplicationLaunchOptionsBluetoothPeripheralsKey] as? NSArray {
//Loop through peripheralManagerIdentifiers reinstantiating each of your peripheralManagers
//CBPeripheralManager(delegate: corebluetooth, queue: nil, options: [CBPeripheralManagerOptionRestoreIdentifierKey: "identifierInArray"])
}
else {
//There are no peripheralManagers to be reinstantiated, instantiate them as you normally would
}
}
else {
//There is nothing in launchOptions, instantiate them as you normally would
}
此时,willRestoreState 应该开始被调用,但是,如果您有一个管理所有订阅了您的特性的中心,那么您的中心阵列中将没有中心。由于我所有的中心总是在我拥有的一项服务中订阅我的所有特征,我简单地遍历任何特征中的所有订阅的中心,将它们重新添加到我的中心数组中。
请根据需要修改。
在willRestoreState中:
var services = dict[CBPeripheralManagerRestoredStateServicesKey] as! [CBMutableService]
if let service = services.first {
if let characteristic = service.characteristics?.first as? CBMutableCharacteristic {
for subscribedCentral in characteristic.subscribedCentrals! {
self.cbCentrals.append(subscribedCentral as! CBCentral)
}
}
}
在此之后你应该可以调用你准备好的任何方法来与任何中心对话,即使你同时处理其中的几个。
祝你好运!