本文介绍了核心蓝牙状态恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作,关于外设断开反应的程序,现在我想采用NE状态preservation和恢​​复的iOS 7中引入的。

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.

我不喜欢的文件说的一切,意思是:

I did everything like the documentation says, means:


  1. 我添加了背景模式中切牙。

  1. I added the background mode for centrals.

我总是实例化我的中央管理器具有相同唯一
    标识符。

I always instantiate my central manager with the same unique identifier.

我实现了 centralManager:willRestoreState:

在我的应用程序移动到后台我杀了它在AppDelegate中的回调与杀号(getpid(),SIGKILL); 。 (<一href=\"http://stackoverflow.com/questions/20529685/core-bluetooth-state-$p$pservation-and-restoration-not-working-cant-relaunch-ap/22244674#22244674\">Core蓝牙状态preservation和恢​​复不灵,无法重新启动应用程序转入后台)

When my App moves to background I kill it in the AppDelegate callback with an kill(getpid(), SIGKILL);. (Core Bluetooth State Preservation and Restoration Not Working, Can't relaunch app into background)

当我现在取出电池我的应用程序正在被唤醒预期和 launchOptions [UIApplicationLaunchOptionsBluetoothCentralsKey] 包含正确的标识符,但 centralManager:willRestoreState:不叫。
只有当我拔下其它外围设备,这种方法被调用。

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.Only if I disconnect another peripheral this method gets called.

推荐答案

这是我怎么有它:

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

                }
            }
        }
    }
}

这篇关于核心蓝牙状态恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 04:24