iBeacon Region Monitoring
23:57
To know the basics read iBeacon Region Basics
The Core Location framework provides two ways to detect a user’s entry and exit into specific regions: geographical region monitoring (iOS 4.0 and later and OS X v10.8 and later) and beacon region monitoring (iOS 7.0 and later).
Apps can use region monitoring to be notified when a user crosses geographic boundaries or when a user enters or exits the vicinity of a beacon. While a beacon is in range of an iOS device, apps can identify the beacon ID (proximityUUID, major, minor) and also can find the relative distance to the beacon using beacon ranging. You can use these capabilities to develop many types of innovative location-based apps.
Beacon region monitoring uses an iOS device’s onboard radio to detect when the user is in the vicinity of Bluetooth low-energy devices that are advertising iBeacon information.
A beacon region is identified with the combination of the following values:
Beacon monitoring works perfectly from iOS 7.1. The phone can detect beacons even if the application is killed (if the application launched at least once in a device), something which in the past (iOS 7.0) was reserved for apps that were at minimum running in the background tray.
- A proximity UUID (universally unique identifier), which is a 128-bit value that uniquely identifies one or more beacons as a certain type or from a certain organization
- A major value, which is a 16-bit unsigned integer that can be used to group related beacons that have the same proximity UUID.
- A minor value, which is a 16-bit unsigned integer that differentiates beacons with the same proximity UUID and major value
Beacon monitoring works perfectly from iOS 7.1. The phone can detect beacons even if the application is killed (if the application launched at least once in a device), something which in the past (iOS 7.0) was reserved for apps that were at minimum running in the background tray.
You can start monitoring a region using the following code,
#define RVProximityUUID @"E2C56DB5-DFFB-48D2-B060-D0F5A71096E0"
NSUUID * uuid = [[NSUUID alloc] initWithUUIDString:RVProximityUUID];
CLBeaconRegion *region = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:RVProximityUUID];
region.notifyOnEntry = YES;
region.notifyOnExit = YES;
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self; [locationManager startMonitoringForRegion:region];
Once a region enter or exit happened the location manager’s didDetermineState: method will be triggered. Please find the code snippet below.
- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
CLBeaconRegion *beaconRegion = (CLBeaconRegion*)region;
NSLog(@"ProximityUUID : %@, Major : %@ Minor : %@", [beaconRegion.proximityUUID UUIDString], beaconRegion.major, beaconRegion.minor);
UILocalNotification *notification = [[UILocalNotification alloc] init];
if(state == CLRegionStateInside)
{
notification.alertBody = @"Welcome.";
}
else if(state == CLRegionStateOutside)
{
notification.alertBody = @"Thank You"; [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
}
We can also use locationManager’s didEnterRegion: and didExitRegion: separately to identify enter and exit region event. But I recommend to use didDetermineState: method, it will be called first and more reliable.
Read about more about Ranging and Monitoring
0 comments