I am scanning for bluetooth devices. For that I am using CBCentralManager
, like so
- (void)startScanning
{
[self.centralManager scanForPeripheralsWithServices:nil options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
}
- (void)centralManager:(CBCentralManager *)central
didDiscoverPeripheral:(CBPeripheral *)peripheral
advertisementData:(NSDictionary *)advertisementData
RSSI:(NSNumber *)RSSI
{
NSLog(@"\n%@, rssi: %@", peripheral.identifier.UUIDString, RSSI);
// do stuff
}
I am using Estimote app to simulate a Beacon on other device. The console output looks like that:
2017-07-26 14:43:56.711830 ExampleApplication[445:55698] peripheral:
6A3E0CB4-74BB-4472-98AA-0C9DEB1D4551, rssi: -28
Everything is fine so far.
Now, I want to scan for bluetooth devices also when app is in background. For that, I added such property to Info.plist
<key>UIBackgroundModes</key>
<string>bluetooth-central</string>
Also, I've read that it's not possible to scan for bluetooth devices in background when passing nil
as serviceUUIDs
, so I take a previously discovered peripheral's UUID and pass it to CBCentralManager
CBUUID *uuid = [CBUUID UUIDWithString:@"6A3E0CB4-74BB-4472-98AA-0C9DEB1D4551"];
[self.centralManager scanForPeripheralsWithServices:@[uuid] options:options];
And there's the problem - didDiscoverPeripheral
is never called anymore, even when app is in foreground. What is the reason? I used the UUID of the device that was discovered before with no issues, but not it's not. Any ideas?