I'm implementing Core Bluetooth background mode and have gotten to the willRestoreState
method successfully. The system properly handles scanning and waking up my app on service advertisement and incoming data. However, if the device is already connected when the app is terminated (due to memory management), I cannot seem to reestablish functioning ownership of the peripheral. No delegate methods are called. Here is my willRestoreState
method:
-(void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary<NSString *,id> *)state {
NSArray *peripherals = state[CBCentralManagerRestoredStatePeripheralsKey];
NSString* NFCName = @"ACR1255U-J1-001236";
_centralManager = central;
_centralManager.delegate = self;
for(CBPeripheral* peripheral in peripherals) {
if([peripheral.name isEqualToString:NFCName]) {
NSLog(@"Restoring NFC Connection");
_peripheral = peripheral;
_peripheral.delegate = self;
[_centralManager connectPeripheral:_peripheral options:nil];
return;
}
}
[_centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:@"FFF0"]] options:nil];
}
The Apple docs state that the app has to connect locally to the device, but _peripheral.state
reads connected whether -connectPeripheral
is called or not. Neither -centralManager:didConnectPeripheral
nor -centralManager:didFailToConnectPeripheral:
is called. Similarly [_peripheral readRSSI]
does not result in -peripheral:didReadRSSI
delegate method being called.
I can't seem to find full sample code for this functionality. If anyone has anything I can parse through to help troubleshoot, I'd appreciate that as well.
Thank you, SO