小编coo*_*459的帖子

CoreBluetooth状态保存问题:在iOS 7.1中未调用willRestoreState

CoreBluetooth状态保存问题:在iOS 7.1中未调用willRestoreState

大家好.过去几周我一直致力于蓝牙LE项目,并遇到了障碍.我无法在iOS 7/7.1中正常恢复状态.我遵循了(我认为)Apple提出的所有步骤,并在其他堆栈溢出帖子上获得了一些线索.

  1. 我为plist添加了正确的蓝牙权限
  2. 当我创建我的中央管理器时,我给它一个恢复标识符键.
  3. 我总是使用相同的密钥来实例化CM
  4. 我将willRestoreState函数添加到CM委托

我的测试用例:

  1. 连接到外围设备
  2. 确认连接
  3. 模拟内存驱逐(kill(getpid(),SIGKILL);)
  4. 传输数据

结果iOS 7:

应用程序将在AppDelegate didFinishLaunchingWithOptions函数中响应,但是launchOptions [UIApplicationLaunchOptionsBluetoothCentralsKey]内部的NSArray内容始终是一个空数组.

iOS 7.1上的结果:

进展!我可以在100%的时间内在UIApplicationLaunchOptionsBluetoothCentralsKey数组中看到我的CentralManager密钥,但永远不会调用fromRestoreState.

码:

//All of this is in AppDelegate for testing

@import CoreBluetooth;
@interface AppDelegate () <CBCentralManagerDelegate, CBPeripheralDelegate>
@property (readwrite, nonatomic, strong) CBCentralManager *centralManager;
@end

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

    self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:@{CBCentralManagerOptionRestoreIdentifierKey:@“myCentralManager”}];

    //Used to debug CM restore only
    NSArray *centralManagerIdentifiers = launchOptions[UIApplicationLaunchOptionsBluetoothCentralsKey];
    NSString *str = [NSString stringWithFormat: @"%@ %lu", @"Manager Restores: ", (unsigned long)centralManagerIdentifiers.count];
    [self sendNotification:str]; …
Run Code Online (Sandbox Code Playgroud)

iphone bluetooth objective-c ios

9
推荐指数
2
解决办法
8436
查看次数

标签 统计

bluetooth ×1

ios ×1

iphone ×1

objective-c ×1