I'm simultaneously advertising a BLE peripheral and a beacon combined in one NSDictionary and advertised by the same CBPeripheralManager. In the same app I'm scanning for BLE peripherals and monitoring the beacon.
It doesn't work - Simultaneously advertising the peripheral and the beacon seems to cause a strange conflict. In foreground the beacon will be detected (region entry) but the callbacks for the CBPeripheral don't fire. When device A backgrounds the app, the peripheral callbacks get triggered on the app running on device B and service and characteristic query runs as expected.
The behaviour suggests that the advert for the beacon blocks the advert for the peripheral when in foreground, but the peripheral advert is effective when the app is in the background.
The advertising code is as follows:
NSDictionary *adUUIDs = [[NSDictionary alloc] initWithObjectsAndKeys:@"CNT100A", @"kCBAdvDataLocalName", _serviceUUIDs, @"kCBAdvDataServiceUUIDs", nil];
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"328204DB-5128-40B4-9C73-65DF66899945"];
CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid major:1 minor:1 identifier:@"com.aurl.id"];
NSDictionary *beaconData = [beaconRegion peripheralDataWithMeasuredPower:nil];
NSMutableDictionary *advert = [NSMutableDictionary dictionaryWithDictionary:beaconData];
[advert addEntriesFromDictionary:adUUIDs];
[_adManager startAdvertising:advert];
Which produces this:
(lldb) po advert
{
kCBAdvDataAppleBeaconKey = <328204db 512840b4 9c7365df 66899945 00010001 c8>;
kCBAdvDataLocalName = CNT100A;
kCBAdvDataServiceUUIDs = (
"5351CAD0-B1CE-4E38-8673-76D05124E915",
"E7A48922-5D43-4237-821D-D710E69EE1CD"
);
}
If I remove either the peripheral or the beacon advertisement item from the dictionary before calling startAdvertising, the app behaves normally.
My CBCentralManager scan is called as follows:
[_searchManager scanForPeripheralsWithServices:serviceUUIDs options:@{CBCentralManagerScanOptionSolicitedServiceUUIDsKey: serviceUUIDs, CBCentralManagerScanOptionAllowDuplicatesKey:continuousSearch}];
And my region monitoring is standard:
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"328204DB-5128-40B4-9C73-65DF66899945"];
self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid major:1 minor:1 identifier:@"com.aurl.id"];
self.beaconRegion.notifyEntryStateOnDisplay = YES;
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager requestAlwaysAuthorization];
[self.locationManager startMonitoringForRegion:self.beaconRegion];
I've been trying to get this to work for longer than I care to admit... Have I misunderstood something simple? Does Apple restrict some functionality in this area?