Understanding and Using Of iBeacon Integration in iOS

By | July 27, 2020

iBeacon technology introduced in the apple to connect multiple devices within range. It is a replica of Bluetooth where multiple devices can connect and communicate with hardware by using Bluetooth. If users are in a range of beacon then all messages, information, and data can be broadcast via one app to another app. In some cases GPS will not work out for indoor navigation, indoor product finding then in that case beacon will work absolutely fine without any hindrance. Usually in Bluetooth users can connect one device at a time but using a beacon.

Business Use case and Use of Beacons

  • Data transfer.
  • Message and communication between peoples.
  • Finding the places in flights, office etc.
  • The nearest shop offer can be sent through notification.
  • Indoor navigation for company, supermarket etc.
  • Many apps can use this feature to showcase

Library to implement Beacon features

  • CoreMotion
  • CoreLocation
  • CoreBluetooth
  • Foundation
  • UIKit

Properties for iBeacon :

  • Proximity – CLProximityindicate how the beacon values get changes for different types of distance. CLProximityvalues can be far, near, immediate, unknown based on the different distance length.
  • Accuracy – CLLocationAccuracy mainly indicate the approximate distance captures from beacon
  • RSSI – It indicates the signal strength capture or measured by beacon
  • UUID – Beacon will scan all the UUID and capture which is nearest to beacon for register and to send the data from one device to another device from android application development services.
  • Major – CLLocation manager will calculate the major distance from the beacon
  • Minor – CLLocation manager will calculate the Minor distance from the beacon

iBeacon Integration in iOS

Adding the framework to build settings

  • CoreMotion, CoreLocation and CoreBluetooth

Plist configuration changes in the project

A screenshot of a cell phone

Description automatically generated

Declare all the variable in the controller and assign properly in the controller

    @IBOutlet weak var uuid: UILabel!

    @IBOutlet weak var major: UILabel!

    @IBOutlet weak var minor: UILabel!

    @IBOutlet weak var identity: UILabel!

    @IBOutlet weak var beacon: UILabel!

    @IBOutlet weak var beaconstartButton: UIButton!

    @IBOutlet weak var beaconstopButton: UIButton!

Declare all the objects iBeacon , UDID, Major and Minor value to identify the local region with peripheral manager

    var localBeacon: CLBeaconRegion!

    var beaconPeripheralData: NSDictionary!

    var peripheralManager: CBPeripheralManager!

    let localBeaconUUID = “7D0D9B66-0554-4CCF-A6E4-ADE12325C4F0”

    let localBeaconMajor: CLBeaconMajorValue = 123

    let localBeaconMinor: CLBeaconMinorValue = 789

    let identifier = “Put your identifier here”

Declare all the objects and assign value to text label

    override funcviewDidLoad() {

super.viewDidLoad()

        // Do any additional setup after loading the view.

beaconstopButton.isHidden = true

        // UI setup

uuid.text = localBeaconUUID

major.text = String(localBeaconMajor)

minor.text = String(localBeaconMinor)

identity.text = identifier

beacon.text = “OFF”

    }

Action can be taken for start beacon and initialize the beacon

    @IBActionfuncbeaconstartButton(_ sender: Any) {

initLocalBeacon()

beaconstartButton.isHidden = true

beaconstopButton.isHidden = false

beacon.text = “ON”

    }

Action can be taken for stop  beacon and initialize the beacon

    @IBActionfuncbeaconstopButton(_ sender: Any) {

stopLocalBeacon()

beaconstartButton.isHidden = false

beaconstopButton.isHidden = true

beacon.text = “OFF”

    }

Initialize the beacon, declare the UDID and stop local beacon

funcinitLocalBeacon() {

        if localBeacon != nil {

stopLocalBeacon()

        }

        let uuid = UUID(uuidString: localBeaconUUID)!

localBeacon = CLBeaconRegion(proximityUUID: uuid, major: localBeaconMajor, minor: localBeaconMinor, identifier: identifier)

beaconPeripheralData = localBeacon.peripheralData(withMeasuredPower: nil)

peripheralManager = CBPeripheralManager(delegate: self, queue: nil, options: nil)

    }

Local beacon can be stopped, peripheral manager and initialize the beacon

funcstopLocalBeacon() {

peripheralManager.stopAdvertising()

peripheralManager = nil

beaconPeripheralData = nil

localBeacon = nil

    }

Peripheral manager can be update and state  will be capture by communicating by hardware in the devices.

funcperipheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {

        if peripheral.state == .poweredOn {

peripheralManager.startAdvertising(beaconPeripheralData as? [String: Any])

        }

        else if peripheral.state == .poweredOff {

peripheralManager.stopAdvertising()

        }    

}