Home:ALL Converter>How to use an iOS device as an iBeacon in iOS?

How to use an iOS device as an iBeacon in iOS?

Ask Time:2017-11-13T18:17:34         Author:Pavan Raja Burra

Json Formatter

I am new to beacon integration. using code to get the UUID of the iPhone beacon(transmitter). initially, I wrote below code to get UUID of iPhone to act as a beacon

let uuidString = UIDevice.current.identifierForVendor?.uuidString  
print("UUID == \(uuidString)")

UUID: F2830580-4E97-4098-B01A-99D49BB69EF1

var locationManager: CLLocationManager?
var myBeaconRegion: CLBeaconRegion?
var peripheral: CBPeripheral?
var manager: CBCentralManager?
var peripheralManager: CBPeripheralManager?

override func viewDidLoad(){

    super.viewDidLoad()
    locationManager = CLLocationManager()
    locationManager?.delegate = self
    locationManager?.distanceFilter = kCLDistanceFilterNone
    locationManager?.desiredAccuracy = kCLLocationAccuracyBest
    locationManager?.requestAlwaysAuthorization()
    locationManager?.requestWhenInUseAuthorization()
    peripheralManager = CBPeripheralManager(delegate: self as 
    CBPeripheralManagerDelegate, queue: nil, options: nil)

    let uuid = UUID(uuidString: "F2830580-4E97-4098-B01A-99D49BB69EF1")

    myBeaconRegion = CLBeaconRegion(proximityUUID: uuid!,identifier: "iBeacon")

    myBeaconRegion?.notifyOnEntry = true
    myBeaconRegion?.notifyOnExit = true
    myBeaconRegion?.notifyEntryStateOnDisplay = true
    manager = CBCentralManager(delegate: self as? CBCentralManagerDelegate, queue: nil)
    locationManager?.startMonitoring(for: myBeaconRegion!)
    locationManager?.startRangingBeacons(in: myBeaconRegion!)
    locationManager?.pausesLocationUpdatesAutomatically = false
    locationManager?.startUpdatingLocation()

}

Using delegate method to get the detected beacon

func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion)
{

  //  let value = region.major  as? NSNumber
   beaconLabel.text = "Beacon not found!"
    let foundBeacon = beacons.first as? CLBeacon
    print("foundBeacon == \(foundBeacon)")
 guard let  discoveredBeacon = beacons.first?.proximity else {
 print("beacon not found")
 return
 }


    let proxyValue = foundBeacon?.accuracy
    beaconDistanceLabel.text = String(describing: proxyValue!)
 let background:UIColor = {

 switch discoveredBeacon {
 case .immediate:
    beaconLabel.text = "Immediate!"
    return  UIColor.green
 case .near:
    beaconLabel.text = "Near!"
    return  UIColor.orange
 case .far:
    beaconLabel.text = "Far!"
    return  UIColor.red
 case .unknown:
    beaconLabel.text = "Unknown!"
    return  UIColor.magenta
 }
 }()
 view.backgroundColor = background

}

delegate methods are calling but CLBeacon not detecting iPhone device.

Even i tried with https://developer.apple.com/documentation/corelocation/turning_an_ios_device_into_an_ibeacon but no results.

Any help will be appreciated..

Author:Pavan Raja Burra,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/47261951/how-to-use-an-ios-device-as-an-ibeacon-in-ios
yy