Cumulations Logo

Working with iBeacons in iOS is quite easier once you understand the concept. 

First of all, what is iBeacon for those who doesn't know about it? 

     "iBeacons are small wireless sensors that you can attach to any location or object. They broadcast tiny radio signals which your smartphone can receive and interpret, unlocking micro-location and contextual awareness"

How does it work?

  1. Beacons have a powerful ARM processor, memory, Bluetooth Smart module, and temperature and motion sensors. Powered by a coin battery, they broadcast radio signals through built-in antennas.
  2. Devices that came into that range can detect and respond(i.e notification etc).
  3. Beacons can broadcast as little as 2 inches and as far as 230 feet (approx. 70 meters) away.

How to identify Beacons?

  1. We worked with Estimote Beacons which can be uniquely identified as well since each beacon broadcasts its own ID.
  2. Every ID is 20 bytes long and is divided into three sections: proximityUUID (16 bytes) + major number (2 bytes) + minor number (2 bytes)
  3. Normally every Estimote beacon has the same UUID. One differentiates between iBeacons on the basis of Major and Minor values.
  4. You can get Major and Minor values of beacon either through the code or through Estimate App.

Coding in iOS

  • We have used the Estimate iOS SDK to work with Estimote iBeacons.
  • The UUID of Estimote Beacon is "B9407F30-F5F8-466E-AFF9-25556B57FE6D”. And this is same for all the Estimote Beacons.
  • We have done my coding in appDelegate class, it entirely depends upon your requirement.
  • Include the Estimote manager delegate
    • @interface AppDelegate ()

       

  • Write these lines in appDelegate class in didFinishLaunchingWithOptions
    • "NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"B9407F30-F5F8-466E-AFF9-25556B57FE6D"];”
      

       

  • Now set up the beacon Manager.
    • self.beaconManager = [[ESTBeaconManager alloc] init];
      self.beaconManager.delegate = self;
      

       

  • Now set up the beacon Region.
    • self.beaconRegion = [[ESTBeaconRegion alloc] initWithProximityUUID:uuid identifier:@"RegionIdenifier"];
      

       

  • This will let us know if we enter the region.
    • self.beaconRegion.notifyOnEntry = YES;

       

  • This will let us know if we exit the region.
    • self.beaconRegion.notifyOnExit = YES;

       

  • This boolean indicates whether beacon notifications are sent when the device’s display is on.
    • self.beaconRegion.notifyEntryStateOnDisplay = YES;
      

       

  • Here we start Monitoring for the beacon.
    • [self.beaconManager startMonitoringForRegion:self.beaconRegion];
      

       

  • Here we start Ranging for the beacon.
    • [self.beaconManager startRangingBeaconsInRegion:self.beaconRegion];

       

  • Request permission to use location services and Must have in IOS 8.
    • [self.beaconManager requestAlwaysAuthorization];
      

       

  • This will give you the state of region, to do anything here use didDetermineState delegate method
    • [self.beaconManager requestStateForRegion:self.beaconRegion];

       

  • didRangeBeacons is the delegate method where we can get major and minor values.
  • There are delegate methods, That you can use according to your requirement.
    • -(void)beaconManager:(ESTBeaconManager *)manager monitoringDidFailForRegion:(ESTBeaconRegion *)region withError:(NSError *)error
      -(void)beaconManager:(ESTBeaconManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
      - (void)beaconManager:(ESTBeaconManager *)manager didEnterRegion:(ESTBeaconRegion *)region
      - (void)beaconManager:(ESTBeaconManager *)manager didExitRegion:(ESTBeaconRegion *)region
      -(void)beaconManager:(ESTBeaconManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(ESTBeaconRegion *)region
      - (void)beaconManager:(ESTBeaconManager *)manager didDetermineState:(CLRegionState)state forRegion:(ESTBeaconRegion *)region

       

  • For example we have a functionality of showing local notifications when the user Enters the beacon region.
    • - (void)beaconManager:(ESTBeaconManager *)manager didEnterRegion:(ESTBeaconRegion *)region
      {
          [self getMval];
          NSLog(@"%@, %@, %@",titleMsg,bodyMsg,beaconDirection);
          NSString *notificationAlertMsg,*notificationAlertBodyMsg;
          NSNumber *theNumber = [NSNumber numberWithInteger:1];
          if ([beaconDirection containsObject:theNumber])
          {
              NSLog(@"Found");
              int index = [beaconDirection indexOfObject:theNumber];
              notificationAlertMsg = [titleMsg objectAtIndex:index];
              notificationAlertBodyMsg = [bodyMsg objectAtIndex:index];
              [[NSUserDefaults standardUserDefaults] setInteger:[[brandIDBeacon objectAtIndex:index] intValue] forKey:@"beaconIDValue"];
              
          }
          //Adding a custom local notification to be presented
          UILocalNotification *notification = [[UILocalNotification alloc]init];
          notification.alertBody = [NSString stringWithFormat:@"%@n%@",notificationAlertMsg,notificationAlertBodyMsg];
          notification.soundName = UILocalNotificationDefaultSoundName;
          //    notification.soundName = @"Default.mp3";
          NSLog(@"Youve entered");
          [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
      
      }

       

You can see notifications similar to below image when the user enters and leaves the region.

Other than this you can also write your method to check proximity and call them in the didRangeBeacons method. There are 4 types of proximity - CLProximityFar, CLProximityNear, CLProximityImmediate and CLProximityUnknown


IMP: Check beacon battery is not dead using Estimote App(in my case), Every Beacon has their app. For Roximity, etc.