I\'m writing an application for iOS that requires that the application advertise both an iOS iBeacon as well as advertise peripheral service concurrently. It\'s necessary th
I was able to get this going with a separate CLLocationManager and CLBeaconRegion for both the reciever and the beacon separately:
#import "GRBothViewController.h"
@interface GRBothViewController ()
@end
@implementation GRBothViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationScanner = [[CLLocationManager alloc] init];
self.locationScanner.delegate = self;
[self initBeacon];
[self initRegion];
[self locationManager:self.locationManager didStartMonitoringForRegion:self.scannerRegion];
}
- (void)initBeacon {
NSLog(@"Starting beacon");
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString: @"23542266-18D1-4FE4-B4A1-23F8195B9D39"];
self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
major:1
minor:1
identifier:@"com.thisisgrow.Grow"];
[self transmitBeacon:self];
}
- (IBAction)transmitBeacon:(id)sender {
self.beaconPeripheralData = [self.beaconRegion peripheralDataWithMeasuredPower:nil];
self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self
queue:nil
options:nil];
}
-(void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral {
if (peripheral.state == CBPeripheralManagerStatePoweredOn) {
NSLog(@"Powered On");
[self.peripheralManager startAdvertising:self.beaconPeripheralData];
} else if (peripheral.state == CBPeripheralManagerStatePoweredOff) {
NSLog(@"Powered Off");
[self.peripheralManager stopAdvertising];
}
}
- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
[self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
}
- (void)initRegion {
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"23542266-18D1-4FE4-B4A1-23F8195B9D39"];
_scannerRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@"com.thisisgrow.Grow"];
_scannerRegion.notifyEntryStateOnDisplay = YES;
[_locationScanner startMonitoringForRegion:self.scannerRegion];
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
//SCANNER
[self.locationScanner startRangingBeaconsInRegion:self.scannerRegion];
}
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
//SCANNER HAS LEFT THE AREA
[self.locationScanner stopRangingBeaconsInRegion:self.scannerRegion];
}
-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
CLBeacon *beacon = [[CLBeacon alloc] init];
NSLog(@"Beacons: %d", [beacons count]);
if(beacons && [beacons count]>0){
beacon = [beacons firstObject];
}
else{
}
}
The only limitation here is that the device cannot detect itself.
In my practice, iBeacon & BLE service can not advertise at same time.
BLE service can not advertise in foreground if advertise mixed with iBeacon. iBeacon can not advertise in background.
iBeacon & BLE service can advertise one by one, e.g. iBeacon advertise 0.5 seconds, then BLE service advertise 30.0 seconds.
Hope this will helpful