Ios 核心蓝牙状态恢复

Ios 核心蓝牙状态恢复,ios,ios7,bluetooth,core-bluetooth,cbcentralmanager,Ios,Ios7,Bluetooth,Core Bluetooth,Cbcentralmanager,我正在开发一款能够在外围设备断开连接时做出反应的应用程序,现在我正在尝试采用iOS 7中引入的ne状态保存和恢复 我做的一切都像文件上说的,意思是: 我为中心添加了背景模式 我总是用相同的唯一值实例化我的中央管理器 标识符 我实现了centralManager:willRestoreState:方法 当我的应用程序移动到后台时,我在AppDelegate回调中使用kill(getpid(),SIGKILL)将其杀死。() 当我现在通过卸下电池断开外围设备时,我的应用程序将按预期唤醒,launch

我正在开发一款能够在外围设备断开连接时做出反应的应用程序,现在我正在尝试采用iOS 7中引入的ne状态保存和恢复

我做的一切都像文件上说的,意思是:

  • 我为中心添加了背景模式

  • 我总是用相同的唯一值实例化我的中央管理器 标识符

  • 我实现了
    centralManager:willRestoreState:
    方法

  • 当我的应用程序移动到后台时,我在AppDelegate回调中使用
    kill(getpid(),SIGKILL)将其杀死。()

    当我现在通过卸下电池断开外围设备时,我的应用程序将按预期唤醒,
    launchOptions[uiapplicationaunchoptions-bluetoothcentralskey]
    包含正确的标识符,但未调用
    centralManager:willRestoreState:
    。 只有当我断开另一个外围设备时,才会调用此方法。

    我就是这样使用它的:

    - (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;
    }
    
    然后:

    - (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];
    
                    }
                }
            }
        }
    }
    

    蓝牙有时表现得很奇怪。这也发生在我身上,重启设备,突然它工作了。此外,您的应用程序在被终止之前所做的最后一件事可能是扫描或尝试连接外围设备。您确实需要再次实例化central manager,否则将不会调用willRestoreState:。我看到您正在应用程序委托中初始化central manager,但如果central manager在不同的类中,会发生什么?我有一个中央经理的sharedManager。