I am Working on BLE project, everything works fine when the app in the foreground.It can discover and connect to the peripheral, all the call back method work perfectly.
But the problem is that, when the app in the background mode (I press home button). Only the centralManagerDidUpdateState
delegate method get called.
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
switch (central.state) {
case CBCentralManagerStatePoweredOn:
[self.cbCentralManager scanForPeripheralsWithServices:nil options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
break;
default:
break;
}
}
I use scanForPeripheralsWithServices:nil
option, But when the app in the background, the didDiscoverPeripheral
call back never called. I have edit my plist file with "bluetooth-central" option to support ble central role in background.
Any idea why didDiscoverPeripheral
method not call when app in the background?
Scan for
nil
(scanForPeripheralsWithServices:nil
) services will not work in background. You must search for some specific service in background.You have to set the
UUID
inscanForPeripheralsWithServices:
method which Peripherals/BLE device is advertising.I was working on Estimote Nearable type beacons. After iOS10 SDK update, I encountered exception from
CBCentralManager
stating :<CBCentralManager: 0x17009e050> has provided a restore identifier but the delegate doesn't implement the centralManager:willRestoreState: method
To fix this, Turn-On "Background Mode", in Xcode -> Capabilities -> Background Mode
Paulw11 said are right, If your app find the peripherals in the foreground. It will not call the
didDiscoverPeripheral
for the same peripherals when it enters the background.For more information about the iOS BLE Behavior in the background mode. You can check this answer What exactly can CoreBluetooth applications do whilst in the background?
From Official Apple reference
Here
Apps that have specified the bluetooth-central background mode are allowed to scan while in the background. That said, they must explicitly scan for one or more services by specifying them in the serviceUUIDs parameter.
So
scanForPeripheralsWithServices:nil
with nil it will not work in background , you need to specify list of UUIDS