Level : Intermediate.

Hardware Needed : Beacon, or iOS device configured as a beacon.

Source Code : https://share.sap.com/a:6jg6n0/MyAttachments/20f6b4f2-05e7-4731-b8fe-da40ca149256/

In this blog we will discuss in details the iBeacon technology from Apple and how it can be integrated with your applications for everyday use. This blog will be an introduction to a series of blogs that discuss this technology.

I know you are excited and ready to dive in and start writing your first iBeacon application but before that I would like to present to you some information about the background of this technology.

iBeacon is an indoor positioning system introduced by Apple in 2013.This technology can notify iOS7 devices when entering the beacon region.


Wow! This technology sounds interesting but what can I do with it?


Good question! To answer this question I will list some good examples on using this technology:


1. Advertising: Imagine the super bowl ads coming straight to your phone when you launch the NFL (National Football League)  app. How cool is that? Recently, New jersey and New York was part of a new way of advertising using the iBeacon technology as the New York Times reported.



Apple is using this feature to advertise for their products inside the stores. Users get push notification when they are walking inside the store. Messages like, “Shopping for accessories? Read product reviews and make your purchase right from your iPhone.” Are popped out once you are close to the accessories section in the store.

         

2. Indoor navigation: iBeacon can be used as a GPS like system inside a building, for example the MLB (Major league Baseball) recently announced they will use the iBeacon technology to point the fans to their chairs inside the stadium. Please go to  engadget.com. to read more details.


3. Shopping/ Mobile Payments: iBeacon can be used as a payment method with one click from your cell phone. ebay just announced ebay Beacon project that can be connected to any point of sale and let user make payment from their phones using ebay’s phone application.

Okay,

If (you still around && interested in this technology) {

Go grab a coffee or a refreshment;

else {

I’m sorry to bore you;

}

Now, let’s start with the fun part; we will build a simple application that helps you to find your lost keys.

1.    Fire up Xcode and create a single view project, let’s call it KeysFinder. Next save the app on your desk.

/wp-content/uploads/2014/02/image1_384337.png



2.    In your ViewController.h import these two libraries



@import CoreLocation;
@import CoreBluetooth;








and add this code  to the ViewController.h


@interface ViewController: UIViewController<CLLocationManagerDelegate, CBPeripheralManagerDelegate,
UITableViewDataSource, UITableViewDelegate>
{
    CLLocationManager *_locationManager;
    NSUUID *_uuid;
    NSNumber *_power;
    CLBeaconRegion *region;
}
@property (weak, nonatomic) IBOutlet UILabel *locationLabel;
@end







3-Add a label to the viewController in the Storyboard and connect it to the locationLabel property in your ViewController.h

Screen Shot 2014-02-10 at 1.59.52 PM.png




4- Please import these libraries in your project build phases to get rid of this error

/wp-content/uploads/2014/02/image2_384345.png

/wp-content/uploads/2014/02/image3_384346.png

5- Now in the ViewController.m we will start by creating a method called startRanging. In this method we will instantiate the UUID, the region the location manager and its delegate protocols. We will also make the app start monitoring for beacons.

Ummm, sounds good.. But what are the UUID and RSSI ? 😕 😕

Well, glad that you asked.

UUID: is unique property for each location, area. For example, each company, conference should have a special UUID that distinguish it from other places.


Major: is an iBeacon property with numeric value that can be used to determine which group of beacons you are connected too.

Minor: is an iBeacon property with numeric value that can be used to determine which beacon are you connected too.

RSSI: is an iBeacon property that measure the power signal of that beacon.


Please check Apple’s documentation for more details.


https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLBeacon_class/Reference/Reference.html#//apple_ref/occ/instp/CLBeacon/proximityUUID

Now that you know what do these properties mean, we can move forward and create the method. Please copy and paste this code in your project



-(void)startRanging{
   
    _uuid = [[NSUUID alloc] initWithUUIDString:@"E2C56DB5-DFFB-48D2-B060-D0F5A71096E0"];
   
    _locationManager = [[CLLocationManager alloc] init];
    _locationManager.delegate = self;
    region = [[CLBeaconRegion alloc] initWithProximityUUID:_uuid identifier:@"COM.SAP.KeysFinder"];
    if(region){
        region.notifyOnEntry = YES;
        region.notifyOnExit = YES;
        region.notifyEntryStateOnDisplay = YES;
        [_locationManager startMonitoringForRegion:region];
        [_locationManager startRangingBeaconsInRegion:region];
       
    }
}
   








Done? now call the this method in your viewDidLoad method



-(void) viewdidload {
[super viewDidLoad];
[self startRanging];
}








6- Next step is to create the delegate methods. Let’s start by adding DidEnterRegion and

DidExitRegion. These methods will determine if you are inside the beacon location or outside.

For now we will leave them empty.


-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLBeaconRegion *)region{
}
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLBeaconRegion *)region{
}









7 – Next, we will add the didRangeBeacons method. This method will help us get more information about the beacon or array of beacons we are connected to. Depends on the RSSI value we can determine how close the beacon is.


Please copy and paste this code in to your ViewController.m



-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region
{
   
    for (CLBeacon *beacon in beacons) {
                if ( beacon.proximity == CLProximityNear) {
                          self.locationLabel.text = @"Found It!!";
                          self.view.backgroundColor = [UIColor greenColor];
                    }
                else if (beacon.proximity == CLProximityFar) {
                          self.locationLabel.text = @"Almost There!!";
                          self.view.backgroundColor = [UIColor orangeColor];
                     }
                else {
                          self.locationLabel.text = @"Not Even close!!";
                          self.view.backgroundColor = [UIColor redColor];
                     }
          }
}









In this method, I’m looping through all beacons in the range and once I have found a beacon the app will determine how close that beacon is and show a different message and color. In our case, once I found the key it will show “FOUND IT” Message and change the view color to Green.

We are almost there. Please,  add these methods to your ViewController.m



-(void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLBeaconRegion *)region{
   
}
- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLBeaconRegion *)region{   
}









Excellent job! Now you are an iBeacon expert. Congratulations!!


To see the app running in real life you will need a beacon or an iOS device acting like a beacon. I’m not going to talk about beacons in this post, but depending on your feedback I can create a separate blog just for beacons and how to create an app that make your iOS device act like one.

Running the app


If you have the beacon ready, please hide it somewhere and come back to run the app on your device. The first screen you will see is the Red screen with a message saying, “Not Even Close”, start walking toward the beacon and you will get a different message saying, “Almost there, keep walking”. Finally, you will get the green screen with a message saying, “Found it”.


Here is another example I created few days ago. With this demo app, i’m using two beacons to define two imaginary booths. The HANA booth and the Mobile Platform Booth. As you can see in the video, the application prompted me with a welcome message when i’m the range of that specific booth.

Conclusion

I hope this blog has given you a fair introduction to iBeacon and how it can be used. In my opinion, this technology will open wide doors  for developers who work on indoor maps applications and POS payments applications. If you are an Android developer you can also develop applications with this technology. Jelly Beans 4.3 and up has a full support for the iBeacon technology but we won’t cover it in these blogs.


To report this post you need to login first.

4 Comments

You must be Logged on to comment or reply to a post.

    1. Sultan Sultan Post author

      Hi Denise, thanks for the comment 🙂

      Really cool presentation! You guys did an amazing work. I will be waiting for your live demo from the MWC. I guess it would be really great if you guys can share some of your knowledge with Android and  iBeacon here with the community.

      (0) 
        1. Sultan Sultan Post author

          Hi Danise,

          OK, I understand now 🙂 . Thanks for sharing the library link with us. I’m positively sure it’s really useful for a lot of SAP Android developers.

          BR,

          -Sultan

          (0) 

Leave a Reply