Skip to content
Alann Maulana edited this page Dec 6, 2016 · 1 revision

We need to provide a beacon region that will define which beacons to monitring for. For that, we can define a beacon region—by proximity UUID-major-minor. Let’s just use the default Cubeacon UUID: CB10023F-A318-3394-4199-A8730C7C1AEC, major: 1 and minor 284.

Let’s go to the AppDelegate implementation file and set up a beacon manager. Also, this time, we’ll create a dedicated property to hold the beacon region. This goes inside the application:didFinishLaunchingWithOptions: method:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // initialize Cubeacon SDK
    [Cubeacon setLogLevel:CBLogLevelDebug];
    [Cubeacon initialize];
    
    // setup scanning
    self.beaconManager = [[CBBeaconManager alloc] init];
    self.beaconManager.delegate = self;
    [self.beaconManager requestAlwaysAuthorization];
    
    self.region = [[CBRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:CUBEACON_UUID]
                                                    major:1
                                                    minor:284
                                               identifier:@"com.eyro.cubeacon.monitoring_region"];
    
    return YES;
}

Now, the code to start monitoring as the app appears. You need to implement CBBeaconManagerDelegate before connecting to service. This too goes inside the AppDelegate class:

@interface AppDelegate () <CBBeaconManagerDelegate>

@property (nonatomic, strong) CBBeaconManager *beaconManager;
@property (nonatomic, strong) CBRegion *region;

@end

Having the list pre-sorted by the Cubeacon SDK, and with all the prep work we’ve performed, the monitoring delegate turns out to be quite simple:

@implementation AppDelegate

- (void)didChangeAuthorizationStatus:(CBAuthorizationStatus)status {
    if (status == CBAuthorizationStatusDenied || status == CBAuthorizationStatusRestricted) {
        NSLog(@"Location Services are disabled for this app, which means it won't be able to detect beacons.");
    } else if (status == CBAuthorizationStatusAuthorizedAlways || status == CBAuthorizationStatusAuthorizedWhenInUse) {
        [self.beaconManager startMonitoringForRegion:self.region];
    }
}

- (void)monitoringDidFailForRegion:(CBRegion *)region withError:(NSError *)error {
    NSLog(@"Monitoring failed for region: %@. Make sure that Bluetooth and Location Services are on, and that Location Services are allowed for this app. Beacons require a Bluetooth Low Energy compatible device: <http://www.bluetooth.com/Pages/Bluetooth-Smart-Devices-List.aspx>. Note that the iOS simulator doesn't support Bluetooth at all. The error was: %@", region.identifier, error);
}

- (void)didEnterRegion:(CBRegion *)region {
    NSLog(@"You're entering beacon region:%@", region);
}

- (void)didExitRegion:(CBRegion *)region {
    NSLog(@"You're exiting beacon region:%@", region);
}

- (void)didDetermineState:(CBRegionState)state forRegion:(CBRegion *)region {
    
}

@end
Clone this wiki locally