I am using the Xcode 5 and CoreBluetooth Framework.
In plist file
Required background modes -> App communicates using CoreBluetooth
In Xcode 5 is a section for Background Modes in the your app's capabilities. Set Background Modes to ON and check "Uses Bluetooth LE accessories"
Our app and BLE connection works fine while the app is running.
The problem is when the phone goes to sleep or if the back goes to the background, the BLE connection stops working.
I haven't seen anything in the CoreBluetooth Framework about keeping the connection alive in the background.
When app get background than will call method to startBackgroundNotification. like below method
-(void)startBackgroundNotification{
self.counterTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:counterTask];
}];
BackgroundManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil];
nDevices = [[NSMutableArray alloc]init];
sensorTags = [[NSMutableArray alloc]init];
}
After startBackgroundNotification call than will call (delegate) to centralManagerDidUpdateState
-(void)centralManagerDidUpdateState:(CBCentralManager *)central {
if (central.state != CBCentralManagerStatePoweredOn) {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"BLE not supported !"
message:[NSString stringWithFormat:@"CoreBluetooth return state: %d",central.state]
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
}
else
[central scanForPeripheralsWithServices:nil options:nil];
}
But never call below function
-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
}
When the app is in the background and we press a button on the BLE device, the phone gives a notification saying that the device needs access to the app so I have to open the app manually again.