There are two switches on my app ,they can turn my app into a peripheral or a central(two role in BLE connection).When the app role as a peripheral,I initialize two CBPeripheralManagers, one for advertising ibeacon and the other for BLE connecttion.What I want to do is that when another device role as central enter the peripheral's range ,it can detect the beacon ,in the mean time,it scans the peripheral ,then connect to the peripheral.When connection establish,the central send some data to the peripheral.
Here is my problem:
I init peripheral like this:
_peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil]; // for BLE connection
_beaconPeripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
_beaconPeripheralData = [self.beaconRegion peripheralDataWithMeasuredPower:nil];
_beaconPeripheralManager startAdvertising:_beaconPeripheralData;
[_peripheralManager startAdvertising:@{ CBAdvertisementDataServiceUUIDsKey : @[[CBUUID UUIDWithString:UUID]] }];
init central like this:
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:UUID];
_beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@"com.nikotung.ibeacon"];
[_locationManager startMonitoringForRegion:self.beaconRegion];
_centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
[_centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:UUID],[CBUUID UUIDWithString:BEACON_UUID]]
options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
If I advertise the two peripherals at the same time,on the central side ,I can never discovery the peripheral(_peripheralManager
) but only the beacon(_beaconPeripheralManager
) .
Delegate
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
never called.
But if I only turn on the _peripheralManager
to advertising ,the central side can discovery it and delegate called.
Does it mean we can't advertising two peripherals at the mean time,or the date size limited.
From Apple's document it says that only two keys CBAdvertisementDataLocalNameKey
and CBAdvertisementDataServiceUUIDsKey
can be advertised.But I found that the _beaconPeripheralData
contain a key kCBAdvDataAppleBeaconKey
which confuse me.
So ,what can I do to make an app can advertise two peripherals and work normally.