Skip to content

iBeacon Basics and Implementation

Featured Image

iBeacon is a low energy Bluetooth device, introduced by Apple with iOS 7. iBeacon is like GPS navigation for indoor locations. It is a wireless sensor device that you can use for location-aware or context-aware into your store. It can also use to send notification of offers or promotions on a mobile device (Ex. You could be walking past a store and receive a discount coupon on your mobile).

Apple introduced iBeacon, a low energy Bluetooth device. iBeacon acts as a transmitter with low-powered devices, which notifies nearby iOS 7 devices of its existence. It is like GPS navigation for indoor locations and wireless sensor devices that can use as location and context aware into the store or shopping malls.

iBeacon can also be used for promotional activities with its Push Notification and send promotional adverts to nearby iOS devices. Currently iBeacon is used in 245 numbers of retail stores & restaurants and which helps to derive more footfalls to their stores.

Another giant venture PayPal recently announced Beacon, which will allow people to make payment on purchase via PayPal. iBeacon is more affordable, and it’s incredible battery efficient with price range from $5 to $33.

As of now iBeacons are not available in many countries including India. But we as a team curious to experiment & test this new technology and to implement with our BOYD food ordering application for broadcasting and receiving purpose.

To do the same we have converted iOS devices to iBeacon and tested with our application and yes we have an achievement. I know you are curious to know how we did this. Below is detailed explanation and code which can help you to create your iBeacon.

First you need to create a dummy UUID for making communication between broadcaster and receiver. You can get it from terminal, just run uuidgen to generate a UUID.

Let’s get started with Beacon broadcaster. Create one single view application into Xcode; name it “Beacon Broadcaster”.

Add “CoreBluetooth” and “CoreLocation” frameworks into project.

Now into “ViewController.h” file, import “CoreBluetooth” and “CoreLocation” frameworks, declare “CLBeaconRegion” and “CBPeripheralManager” objects, add one UILabel outlet to display status, and one NSDictionary to get beacon data. Your “ViewController.h” file will be look like this:

#import <UIKit/UIKit.h></em>
<em>#import <CoreLocation/CoreLocation.h></em>
<em>#import <CoreBluetooth/CoreBluetooth.h></em>

<em>@interface ViewController : UIViewController<CBPeripheralManagerDelegate></em>

<em>@property (weak, nonatomic) IBOutlet UILabel *statusLabel;</em>
<em>@property (strong, nonatomic) CLBeaconRegion *myBeaconRegion;</em>
<em>@property (strong, nonatomic) NSDictionary *myBeaconData;</em>
<em>@property (strong, nonatomic) CBPeripheralManager *peripheralManager;</em>

<em>@end[php]</em>

<strong>Into “ViewController.m” file, initialize NSUUID and CLBeaconRegion objects into ViewDidLoad method:</strong>

<em>[php](void)viewDidLoad</em>
<em>{</em>
<em> [super viewDidLoad];</em>
<em> // Do any additional setup after loading the view, typically from a nib.</em>

<em> // Create a NSUUID object</em>
<em> NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@”PLACE UUID HERE WHICH YOU GENERATED INTO TERMINAL”];</em>

<em> // Initialize the Beacon Region</em>
<em> self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid major:1 minor:1 identifier:@”com.myapp.beacontest”];</em>
<em>}

Implement delegate method of CBPeripheralManager:

(void)peripheralManagerDidUpdateState:(CBPeripheralManager*)peripheral</em>
<em>{</em>
<em> if (peripheral.state == CBPeripheralManagerStatePoweredOn)</em>
<em> {</em>
<em> // Bluetooth is on</em>

<em> // Update our status label</em>
<em> self.statusLabel.text = @”Broadcasting…”;</em>

<em> // Start broadcasting</em>
<em> [self.peripheralManager startAdvertising:self.myBeaconData];</em>
<em> }</em>
<em> else if (peripheral.state == CBPeripheralManagerStatePoweredOff)</em>
<em> {</em>
<em> // Update our status label</em>
<em> self.statusLabel.text = @”Stopped”;</em>

<em> // Bluetooth isn’t on. Stop broadcasting</em>
<em> [self.peripheralManager stopAdvertising];</em>
<em> }</em>
<em> else if (peripheral.state == CBPeripheralManagerStateUnsupported)</em>
<em> {</em>
<em> self.statusLabel.text = @”Unsupported”;</em>
<em> }</em>
<em>}

Add one button action for starting broadcasting:

(IBAction)buttonClicked:(id)sender {</em>

<em> // Get the beacon data to advertise</em>
<em> self.myBeaconData = [self.myBeaconRegion peripheralDataWithMeasuredPower:nil];</em>

<em> // Start the peripheral manager</em>
<em> self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:nil];</em>
<em>}

Open storyboard, drag one UIButton and one UILabel, and set label outlet and button action to respective control. Your Beacon broadcaster is ready now.

Let’s start implementation of Beacon receiver. Create new single view application into Xcode; name it “Beacon Receiver”. Add “CoreLocation” framework into project.

Into “ViewController.h” file, import “CoreLocation” framework, add one UILbel outlet to display status, add CLBeaconRegion and CLLocationManager objects:

#import <UIKit/UIKit.h></em>
<em>#import <CoreLocation/CoreLocation.h></em>

<em>@interface ViewController : UIViewController<CLLocationManagerDelegate></em>

<em>@property (weak, nonatomic) IBOutlet UILabel *statusLabel;</em>
<em>@property (strong, nonatomic) CLBeaconRegion *myBeaconRegion;</em>
<em>@property (strong, nonatomic) CLLocationManager *locationManager;</em>

<em>@end

Into “ViewController.m” file, initialize CLLocationManager, NSUUID and CLBeaconRegion objects into ViewDidLoad method:

(void)viewDidLoad</em>
<em>{</em>
<em> [super viewDidLoad];</em>
<em> // Do any additional setup after loading the view, typically from a nib.</em>

<em> // Initialize location manager and set ourselves as the delegate</em>
<em> self.locationManager = [[CLLocationManager alloc] init];</em>
<em> self.locationManager.delegate = self;</em>

<em> // Create a NSUUID with the same UUID as the broadcasting beacon</em>
<em> NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@”PLACE UUID HERE WHICH YOU GENERATED INTO TERMINAL”];</em>

<em> // Setup a new region with that UUID and same identifier as the broadcasting beacon</em>
<em> self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@”com.myapp.beacontest”];</em>
<em> self.myBeaconRegion.notifyOnEntry=YES;</em>
<em> self.myBeaconRegion.notifyOnExit=YES;</em>
<em> self.myBeaconRegion.notifyEntryStateOnDisplay=YES;</em>
<em> [self.locationManager startMonitoringForRegion:self.myBeaconRegion];</em>

<em> // Check if beacon monitoring is available for this device</em>
<em> if (![CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]]) {</em>
<em> UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Monitoring not available” message:nil delegate:nil cancelButtonTitle:@”Ok” otherButtonTitles: nil]; [alert show]; return;</em>
<em> }</em>
<em>}

 

Implement CLLocationManager delegates to receive Beacons data:

(void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region</em>
<em>{</em>
<em> [self.locationManager requestStateForRegion:self.myBeaconRegion];</em>
<em>}</em>
<em>-(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region</em>
<em>{</em>
<em> if (state == CLRegionStateInside)</em>
<em> {</em>
<em> //Start Ranging</em>
<em> [manager startRangingBeaconsInRegion:self.myBeaconRegion];</em>
<em> }</em>
<em> else</em>
<em> {</em>
<em> //Stop Ranging here</em>
<em> }</em>
<em>}</em>
<em>- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region</em>
<em>{</em>
<em> self.statusLabel.text=@”Entered region”;</em>
<em>}</em>

<em>-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region</em>
<em>{</em>
<em> self.statusLabel.text=@”Exit region”;</em>
<em>}
(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region</em>
<em>{</em>
<em> if(beacons.count>0)</em>
<em> {</em>
<em> CLBeacon *foundBeacon = [beacons firstObject];</em>
<em> self.statusLabel.text = [NSString stringWithFormat:@"Beacon found = %@",foundBeacon.proximityUUID.UUIDString];</em>
<em> }</em>
<em> else</em>
<em> self.statusLabel.text = @”Beacon not found”;</em>
<em>}

Open storyboard, drag one label control and set UILabel outlet. Your Beacon receiver is ready now.

First run “Beacon Broadcaster” into one iOS device, turn on Bluetooth and start broadcasting. Then run “Beacon Receiver” into another iOS device. Your receiver device will start receiving data when you put broadcaster device near to it.

And yes, that’s work. This is bit easy procedure to convert your iOS devices to iBeacons.

Do share your comments with us.

Related Insights