ibeacon technology

A Beginner’s Guide to iBeacons

Do you frequently forget your belongings and don’t know where you leave them? Consider your comfort level when you leave your laptop behind and get a notification on your iPhone saying, “You are forgetting something!” or if you ever wished to see your location on a map inside a shopping mall? Here comes our beloved iOS which can make your life a lot easier with its latest technology called ‘iBeacons’.

Beacons are a low-priced hardware equipment. They are adequately small to be attached to anything such as laptop bags or anything you don’t want to loose. It uses energy friendly, low-exhaustive Bluetooth connections to send out messages or to prompt straightforwardly to a tablet or to a smartphone. Even an iPhone/iPad can be turned into a Beacon device.

Technically speaking, iBeacons is an extension of location services on iOS. Previously the only way to determine entry/exit to a specific region was via geographical region monitoring but now Core Location has introduced a new phenomenon called “Beacon Region Monitoring”. Rather than being identified by fixed geographical coordinates, a beacon region is identified by the device’s proximity to Bluetooth low-energy beacons.

A beacon device advertises the combination of these values:

  • A proximity UUID (universally unique identifier), a 128-bit value that uniquely identifies one or more beacons as a certain type or from a certain organization
  • A major value, a 16-bit unsigned integer that can be used to group related beacons that have the same proximity UUID
  • A minor value, a 16-bit unsigned integer that differentiates beacons with the same proximity UUID and major value

An iOS device receiving a beacon signal can also approximate the distance from it. The distance between transmitting beacon device and receiving device is categorized into 3 distinct ranges:

  • Immediate Within a few centimeters
  • Near Within a couple of meters
  • Far Greater than 10 meters

Lets start with a simple app idea. If you ever visit an art gallery and move across the paintings and long to see more details about these art works. For that purpose, we can attach a beacon device to each of the paintings and provide an app to our visitors. The app will detect the signals from these beacon devices. When a beacon signal comes in a range of our iPhone/iPad, it will ask the cloud storage to provide information about this specific painting to which is beacon device is attached and display it to the user.

Creating a Beacon Region:

Lets create a beacon region we are looking for. Our app will discover many regions on its way but will notify us when it finds this one.

#define UUID @”ec127f90-d757-11e3-a1e4-1a514932ac01”

– (CLBeaconRegion *) beaconRegionWithUUID:(NSUUID *) UUID name: (NSString *) name

{
CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc]   initWithProximityUUID:UUID

major:item.majorValue

        minor:item.minorValue

        identifier:item.name];

        return beaconRegion;

}

Searching for Beacons:

 – (void ) startSearchingForBeacons

{

NSUUID *UUID = [[NSUUID alloc] initWithUUIDString:UUID]

NSString *identifier = @”Mona Lisa Painting”;

CLBeaconRegion *beaconRegion = [self beaconRegionWithUUID: UUID name: identifier];

[self.locationManager startMonitoringForRegion: beaconRegion];

[self.locationManager startRangingBeaconsInRegion:beaconRegion];

}

Implement these delegate methods inside your view controller:

(void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:

(CLRegion *)region withError:(NSError *)error

{

NSLog(@”Failed monitoring region: %@”, error);

}

(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

{

NSLog(@”Location manager failed: %@”, error);

}

(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region

{

for(CLBeacon *beacon in beacons)

{

if (beacon.proximity == CLProximityNear)

{

[self fetchFromParse:[beacon.proximityUUID UUIDString]];

break;

} } }

didRangeBeacons:inRegion method is called when iOS finds any beacon device within the range. This method checks if the discovered beacon is ours, then fetch its data from Parse (Cloud Storage).

There is a good tutorial available about fetching data from Parse cloud, based on some parameter at: https://parse.com/docs/ios_guide

– (void) fetchFromParse : (NSString *) detectedUUID

{

PFQuery *query = [PFQuery queryWithClassName:@”Art_Information”];

[query whereKey:@”UUID” equalTo:[detectedUUID lowercaseString]];

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)

{

if (!error)

{

if(objects.count > 0)

[self showDetailedInfo:[objects objectAtIndex:0]];

} }]; }

– (void) showDetailedInfo:(PFObject *) object

{

//show information to the user

}