The term iBeacon and Beacon are often used interchangeably. iBeacon is the name for Apple’s technology standard, which allows Mobile Apps (running on both iOS and Android devices) to listen for signals from beacons in the physical world and react accordingly. In essence, iBeacon technology allows Mobile Apps to understand their position on a micro-local scale, and deliver hyper-contextual content to users based on location. The underlying communication technology is Bluetooth Low Energy.

Image for post
Image for post

Suppose you entered in any Shopping mall, how would you find the store ? by Using the store map, ask the people around you or visit one by one… what if your device will tells the complete story about the store, exact location, offers and discounts, when you enter in that Shopping mall.

According to me it will save a lot of time. You will achieve this by iBeacon. iBeacon is the future of marketing.

Apple says: From welcoming people as they arrive at a sporting event to providing information about a nearby museum exhibit, iBeacon opens a new world of possibilities for location awareness, and countless opportunities for interactivity between iOS devices and iBeacon hardware.

How does BLE communication work?

BLE communication consists primarily of “Advertisements”, or small packets of data, broadcast at a regular interval by Beacons or other BLE enabled devices via radio waves.

BLE Advertising is a one-way communication method. Beacons that want to be “discovered” can broadcast, or “Advertise” self-contained packets of data in set intervals. These packets are meant to be collected by devices like smartphones, where they can be used for a variety of smartphone applications to trigger things like push messages, app actions, and prompts.

Apple’s iBeacon standard calls for an optimal broadcast interval of 100 ms. Broadcasting more frequently uses more battery life but allows for quicker discovery by smartphones and other listening devices.

Standard BLE has a broadcast range of up to 100 meters, which make Beacons ideal for indoor location tracking and awareness.

Image for post
Image for post

How does iBeacon use BLE communication?

With iBeacon, Apple has standardised the format for BLE Advertising. Under this format, an advertising packet consists of four main pieces of information.

UUID: This is a 16 byte string used to differentiate a large group of related beacons. For example, if Walmart maintained a network of beacons in a chain of retail stores, all Walmart beacons would share the same UUID. This allows Walmart’s dedicated smartphone app to know which beacon advertisements come from Walmart-owned beacons.

Major: This is a 2 byte string used to distinguish a smaller subset of beacons within the larger group. For example, if Walmart had four beacons in a particular retail store, all four would have the same Major. This allows Walmart to know exactly which store its customer is in.

Minor: This is a 2 byte string meant to identify individual beacons. Keeping with the Walmart example, every floor of the store would have its own unique Minor beacons value. This allows Walmart’s dedicated app to know exactly where the customer is in the store.

Example:

A beacon broadcasts the following packet

UUID: 5A4BCFCE-174E-4BAC-A814–092E77F6B7E5

Major: 9

Minor: 1

A device receiving this packet would understand it’s from the Walmart Beacon(UUID) in the store on Los angeles(Major) on the first floor (Minor).

Proximity State Description (Tx Power)

This is used to determine proximity (distance) from the beacon. How does this work? TX power is defined as the strength of the signal exactly 1 meter from the device. This has to be calibrated and hardcoded in advance. Devices can then use this as a baseline to give a rough distance estimate. iBeacon define it’s in 4 sections:

Immediate: This represents a high level of confidence that the device is physically very close to the beacon. Very likely being held directly up to the beacon.

Near: With a clear line of sight from the device to the beacon, this would indicate a proximity of approximately 1–3 meters. As described in the section on accuracy, if there are obstructions between the device and the beacon which cause attenuation of the signal, this Near state may not be reported even though the device is in this range.

Far: This state indicates that a beacon device can be detected but the confidence in the accuracy is too low to determine either Near or Immediate. An important consideration is that the Far state does not necessarily imply “not physically near” the beacon. When Far is indicated, rely on the accuracy property to determine the potential proximity to the beacon.

Unknown: The proximity of the beacon cannot be determined. This may indicate that ranging has just begun, or that there are insufficient measurements to determine the state.

Setup Beacon Receiver

Beacon features consist of two parts, receive beacon notification and show that notification to the user, So we need to import both CoreLocation and UserNotifications, and add privacy permission in Plist.

Open Info.plist, and add the key NSLocationWhenInUseUsageDescription with String value Required to monitor beacons

Create a list of Beacons (later It can come from API ends)

Create a location manager object

Create two methods, first one ask for location permission and another one ask for notification permission and confirm location manager delegate(CLLocationManagerDelegate) to your view controller

Call these method in viewDidLoad

Create another method startScanning and call that method from locationPermission method in place of Ranging Available.

This method binds all the Beacon values from model into Beacon Region and Start ranging that values. Thing to be remembered is that the identifier must be unique for every beacon.

Now when the device enter any Beacon region didRangeBeacons delegate will call

Get the top beacon and check the proximity(distance) and perform action according to that, If proximity is near or immediate then call checkIn method

and when going away from Beacon region i.e proximity is far then call checkOut method

Create a method checkIn that first get model data for the region with the help of identifier then check for recurring notification if it’s not call earlier, then call the fire notification method according to notification setting.

Recurring event is managed by a bool variable it will reset when user exit from region or day change

Create a method fireNotificaion that fire notification with beacon data as userInfo

In checkout method we will update the bool variable for the identifier when user go away from region

Beacon is all set for receiving notification

For perform action on notification you have to confirm notification delegate(UNUserNotificationCenterDelegate) to your view controller

For testing we can download any beacon emitter from the app or play store in another device and run this code in an iOS device.

Download the complete code from here

Happy Coding and New hope year….2021….

iOS App developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store