[英]Core Bluetooth State Restoration
I am working on an app that reacts on disconnects of peripherals and I am now trying to adopt the ne state preservation and restoration introduced in iOS 7. 我正在开发对外围设备断开连接有反应的应用程序,现在我正在尝试采用iOS 7中引入的ne状态保存和恢复。
I did everything like the documentation says, means: 我按照文档中的说明做了所有事情,这意味着:
I added the background mode for centrals. 我为中心添加了背景模式。
I always instantiate my central manager with the same unique identifier. 我总是使用相同的唯一标识符实例化中央管理器。
I implemented the centralManager:willRestoreState:
method. 我实现了centralManager:willRestoreState:
方法。
When my App moves to background I kill it in the AppDelegate callback with an kill(getpid(), SIGKILL);
当我的应用程序移至后台时,我使用kill(getpid(), SIGKILL);
在AppDelegate回调中将其kill(getpid(), SIGKILL);
. 。 ( Core Bluetooth State Preservation and Restoration Not Working, Can't relaunch app into background ) ( 核心蓝牙状态保存和还原不起作用,无法将应用重新启动到后台 )
When I now disconnect a peripheral by removing the battery my app is being waked up as expected and launchOptions[UIApplicationLaunchOptionsBluetoothCentralsKey]
contains the correct identifier BUT the centralManager:willRestoreState:
was not called. 当我现在通过卸下电池断开外围设备的连接时,我的应用程序将按预期方式唤醒,并且launchOptions[UIApplicationLaunchOptionsBluetoothCentralsKey]
包含正确的标识符,但未调用centralManager:willRestoreState:
Only if I disconnect another peripheral this method gets called. 仅当我断开另一个外围设备的连接时,此方法才会被调用。
This is how I have it: 这是我的方式:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSArray *peripheralManagerIdentifiers = launchOptions[UIApplicationLaunchOptionsBluetoothPeripheralsKey];
if (peripheralManagerIdentifiers) {
// We've restored, so create the _manager on the main queue
_manager = [[CBPeripheralManager alloc] initWithDelegate:self
queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
options:@{CBPeripheralManagerOptionRestoreIdentifierKey:@"YourUniqueIdentifier"}];
} else {
// Not restored so just create as normal
manager = [[CBPeripheralManager alloc] initWithDelegate:self
queue:nil
options:@{CBPeripheralManagerOptionRestoreIdentifierKey:@"YourUniqueIdentifier"}];
}
return YES;
}
And then: 接着:
- (void)peripheralManager:(CBPeripheralManager *)peripheral
willRestoreState:(NSDictionary *)dict
{
// This is the advertisement data that was being advertised when the app was terminated by iOS
_advertisementData = dict[CBPeripheralManagerRestoredStateAdvertisementDataKey];
NSArray *services = dict[CBPeripheralManagerRestoredStateServicesKey];
// Loop through the services, I only have one service but if you have more you'll need to check against the UUID strings of each
for (CBMutableService *service in services) {
_primaryService = service;
// Loop through the characteristics
for (CBMutableCharacteristic *characteristic in _primaryService.characteristics) {
if ([characteristic.UUID.UUIDString isEqualToString:CHARACTERISTIC_UUID]) {
_primaryCharacteristic = characteristic;
NSArray *subscribedCentrals = characteristic.subscribedCentrals;
// Loop through all centrals that were subscribed when the app was terminated by iOS
for (CBCentral *central in subscribedCentrals) {
// Add them to an array
[_centrals addObject:central];
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.