James
James

Reputation: 301

CoreBluetooth state preservation issues

The app is a simple heart rate monitor from this link and right now I'm just playing around with Core bluetooth and I'm trying to "perform long-term actions in the background" which involves Adding Support for State Preservation and Restoration

The first thing to do change the info.plist file then I "Opt In to State Preservation and Restoration" by replacing

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

in my MainViewController.m with

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

The second step involves "Reinstantiate any central or peripheral manager objects after your app is relaunched by the system".

I do this by adding first adding this property into MainViewController.m

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

Then in appDelegate.m

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

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

Next step is to "Implement the Appropriate Restoration Delegate Method"

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

I'm getting the error

*** 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'

Thanks

Upvotes: 2

Views: 2536

Answers (1)

l0gg3r
l0gg3r

Reputation: 8954

You need to implement delegate methods,
For example it will be like this, if we implement it in AppDelegate.

@interface AppDelegate () <CBCentralManagerDelegate>

@property (strong, nonatomic) CBCentralManager *centralManager;

@end

@implementation AppDelegate

- (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");
}

@end

Upvotes: 0

Related Questions