核心蓝牙背景处理

时间:2015-05-17 03:56:30

标签: ios objective-c bluetooth core-bluetooth

The app is a simple heart rate monitor from this link现在我正在玩Core bluetooth,我正在尝试“在后台执行长期行动”,涉及Adding Support for State Preservation and Restoration

要做的第一件事是更改info.plist文件,然后通过替换

“选择加入状态保存和恢复”
CBCentralManager *centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

在我的MainviewController.m中

CBCentralManager *centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:@{CBCentralManagerOptionRestoreIdentifierKey:@"myCentralManager"}];

第二步涉及“在系统重新启动应用程序后重新实例化任何中央或外围管理器对象”。

我这样做是通过添加

来实现的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

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

下一步是“实施适当的恢复代表方法”

-(void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary *)dict {
    NSLog(@"willRestoreState called");
    self.myPeripheral = [dict[CBCentralManagerRestoredStatePeripheralsKey] firstItem];
    self.myPeripheral.delegate = self;
}

这些是正确的步骤吗?我有什么遗漏的吗? (我似乎无法找到关于这样做的任何信息)

0 个答案:

没有答案