CoreBluetooth状态保存问题

时间:2015-05-21 14:51:10

标签: 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文件,然后选择I"选择加入状态保存和恢复"通过替换

CBCentralManager *centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

在我的MainViewController.m中

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

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

我这样做是通过添加首先将此属性添加到MainViewController.m

@property (weak,nonatomic) id <CBCentralManagerDelegate> delegate;

然后在appDelegate.m

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

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

下一步是&#34;实施适当的恢复代表方法&#34;

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

我收到了错误

*** Assertion failure in -[CBCentralManager initWithDelegate:queue:options:],               /SourceCache/CoreBluetooth/CoreBluetooth-242.1/CBCentralManager.m:194
2015-05-22 00:42:16.049 HeartMonitor[9694:793663] *** Terminating app due to     uncaught exception 'NSInternalInconsistencyException', reason: '<CBCentralManager:   0x17009e050> has provided a restore identifier but the delegate doesn't implement   the centralManager:willRestoreState: method'

由于

1 个答案:

答案 0 :(得分:0)

您需要实现委托方法,
例如,如果我们在AppDelegate中实现它,它将是这样的。

currencyModule.filter('myCurrency', function () {
    return function (myValue) {
        if (myValue > 999999){
          myValue = 999999;
        }
        else if(myValue < 0){
          myValue = 0;
        }
        return myValue;
    };
});