Food for thoughtMobile

iBeacon Detector for iOS

By 5 June 2020 June 8th, 2020 2 Comments

iBeacon Detector for iOS

Sharing is caring!

What are Beacon and  iBeacon?

Beacon is a small wireless device whose only function is to transmit the signals within specified range. Those signals contain an information which would be listen by the receiving device, may be a smartphone app at another end. To send the information in specific format, there is a Bluetooth Low Energy (BLE) proximity sensing based protocol developed by Apple – iBeacon. It transmits universally unique identifier along with some extra bytes, which then listen by the receiving device to address the transmitting beacon.

Well, there are lot many contents available out there on other sites. So, without going deep about iBeacon we will see the dedicated class developed by us for detecting the iBeacon-compatible devices, typically beacons.

Information Structure

In iOS, we can range and monitor a beacon that emit 3 values:

  1. UUID: Universally Unique Identifier. It is 128-bit shows as hex string, used to represent the top-level identity. That mean more than one beacon could have same UUID. We call it group of beacons.
  2. Major: 16-bit unsigned integer.
  3. Minor: 16-bit unsigned integer, must be different than major value.

So, If the beacons have same UUID, major and minor values will be used to identify the beacon. 

The iBeacon Detector

This is just the generalized solution we developed using iOS native APIs to find out any iBeacon-compatible devices. With CoreLocation framework, iOS provide us with all the required APIs to deal with iBeacons.

In order to keep track a beacons, we first need to start monitoring region represented by CLBeaconRegion corresponds to those beacons. Once monitoring started, we start ranging beacons in the same region. We capture the all the detected beacons in CLLocationManagerDelegate method:

func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion)You can see we get the list of CLBeaconeach of which has UUID, major and minor values.

This is base of our work. You can get list of all the available beacons with just few lines of code. Let’s dive in and look at some useful classes.  

– RegionToMonitor:

This class let us define the region to monitor and range. It has 4 properties: UUID, major, minor and identifier. UUID and identifier are mandatory to pass, whereas major and minor are optional.

let region = RegionToMonitor.init(withUUIDString: “B9407F30-F5F8-466E-AFF9-25556B57FE6D”, major: nil, minor: nil, identifier: "MyBeaconRegion”)– BeaconDetector:

This is the main class which is taking care of everything from monitoring and ranging to firing delegate for tracked and untracked beacons.

BeaconDetector.shared.start(monitoringRegions: [region], delegate: self)– BeaconDetectorDelegate:

Contains the methods for various event of the beacon once monitor on region called. Two required method implementing the delegates are:

func didTrack(beacon:CLBeacon)
func didUntrack(beacon:CLBeacon)
We can combine the result of these delegates to finally list-out all the ranged beacons on screen. That’s it!

Get started with Bloom