Beacon Implementation in iOS

10 / Aug / 2015 by Kailash Narayan 0 comments

What is iBeacon

iBeacon is a new technology that extends location services in iOS. Beacon is a very generic term but Apple has made a wrapper for this and it knows beacon as iBeacon. iBeacons are very small handy devices that works on the principle of BLE.

What is BLE

BLE extends for Bluetooth Low Energy. The small BLE chips are designed in a way to consume very less power so that one BLE device can be used for a couple of  years. BLE works on central peripheral mode.

How to create a beacon region and monitor it: 

As we know that in iOS earlier we were using geo-fencing to monitor the coordinates of a geographical region, for that we need to create a circular region by giving the radius of circle. But using iBeacon technology we can create a beacon region by broadcasting the unique packets and when an iOS device receives the packet the application get to know that device entered into the beacon region.

We get three callbacks when an iOS device comes into the vicinity of beacon region.

  • When a device enters the beacon region
  • When device exits from the beacon region
  • Once the device enters into the beacon region the ranging get started

How to create a packet that we broadcast to create a beacon region: Three parameters play very important role in creating a beacon region.

1. A proximity UUID helps to create a beacon region. We can generate UUID with the help of following command uuidgen. This command will give you a uuid string. This is the first unique identifier that we can use to create region.

2. Major value is the second unique identifier that helps to create more deep down region and its a 16 bit unsigned integer.

3. Minor value is the third unique identifier that helps to create more deep down region and its a 16 bit unsigned integer.

How to implement iBeacon in iOS using Objective c:

There are two applications required to create Beacon experience.

  1. Transmitter
  2. Receiver

Frameworks required:

To integrate beacon we need these frameworks.

  1. CoreLocation.framework

2.   CoreBluetooth.framework

Beacon Transmitter

We can make our iPhone device a Beacon transmitter by the help of coding. Before transmitting it is  necessary to know whether the device  Bluetooth is on or off.

How to check that Bluetooth is on or off in device

if (peripheral.state == CBPeripheralManagerStatePoweredOn)
{
	NSLog(@"Bluetooth is  on");
	//We can advertise the ble packet
}
else if (peripheral.state == CBPeripheralManagerStatePoweredOff)
{
	NSLog(@"Bluetooth is  off");
}

 How to make your iPhone device a beacon broadcaster:

/*How to create your peripheral data*/

self.beaconPeripheralData = [self.beaconRegion peripheralDataWithMeasuredPower:nil];

/*Initializing the PeripheralManager*/

self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:nil];

/*For broadcasting the data we need to start advertising the data*/

[self.peripheralManager startAdvertising:self.beaconPeripheralData];

After executing this code your iPhone device will work as a Beacon hardware.Beacon Receiver

How to create a beacon region to be monitored

To monitor a beacon first we need to create beacon region and register it with the system.This sample code illustrate creation and registration of a beacon region.

//Create the beacon region

CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:proximityUUIDidentifier:identifier];
//Register the beacon region with location manager
[self.locationManager startMonitoringForRegion:beaconRegion];

Start the ranging of the beacon

[self.locationManager startRangingBeaconsInRegion:beaconRegion];

Ranging and Monitoring

  • Region monitoring:

When a user enters or exits the registered Beacon region iOS notifies the app by providing call back. But to enable this service we need to set some properties.

[beaconRegion setNotifyOnEntry:YES];
[beaconRegion setNotifyOnExit:YES];

/*This method gets called when an application enters the Beacon region*/

-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
	// See if we've entered the region.
	//Corresponding operation gets performed.
}

/*This method get called when an application exit an beacon region*/

-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
	// See if we've exit the region.
	//Corresponding operation gets performed.
}

-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
	// See if we've entered the region.
	//Corresponding operation gets performed.
}

/*This method gets called when an application exits the Beacon region*/

-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
	// See if we've exit the region.
	//Corresponding operation gets performed.
}
  • Beacon Ranging:

When a device enters a Beacon region it provides the beacon related data inside this method as a call back.

Note : Beacon ranging would not happen in background

-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region
{
	CLBeacon *beacon = [[CLBeacon alloc] init];
	beacon = [beacons lastObject];
	BeaconInfoModel *modelObj = [[BeaconInfoModel alloc]init];
	modelObj.uuid = beacon.proximityUUID.UUIDString;
	modelObj.major = beacon.major;
	modelObj.minor =  beacon.minor;
	modelObj.accuracy = beacon.accuracy;
	modelObj.proximity = beacon.proximity;
}

 An iOS device receiving an iBeacon transmission can approximate the distance from the iBeacon. The distance (between transmitting iBeacon and receiving device) is categorized into 3 distinct ranges:

  • Immediate: Within a few centimetres
  • Near: Within a couple of meters
  • Far: Greater than 10 meters away

Happy Coding… :)

Beacons - Building Proximity based Solutions for Brands



Tag -

bluetooth
FOUND THIS USEFUL? SHARE IT

Leave a comment -