Home > other >  CoreLocation Beacon callback functions not triggered when in foreground
CoreLocation Beacon callback functions not triggered when in foreground

Time:01-21

I am new to iOS development and struggling with many of the interactions between my program and the device hardware so please excuse my very minimal knowledge.

I am trying to build into my app the ability to run code when a beacon is detected. Ultimately I need this to happen in the background as well but for now I am just working on getting it to work in the foreground.

With the code that I currently have, the print message located within the callback function is never called even with the beacon about two feet from the phone. I have double checked with an android device that the UUID broadcasted by the beacon and the UUID that iOS is searching for is one and the same.

This is the class that I created to manage the location aspects.

import CoreLocation

class LocationManager: NSObject, CLLocationManagerDelegate {
    var locationManager: CLLocationManager!

    override init() {
        super.init()
        locationManager = CLLocationManager()
        locationManager.delegate = self
    }
    
    func locationPermission() {
        locationManager.requestAlwaysAuthorization()
    }

    func startScanning() {
        let beaconRegion = CLBeaconRegion(uuid: Beacon.beaconUUID!, identifier: "CarBeacon")
        print(Beacon.beaconUUID!)
        locationManager.startMonitoring(for: beaconRegion)
        locationManager.startRangingBeacons(in: beaconRegion)
    }

    func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
        if let beacon = beacons.first {
            print(beacon.uuid)
        }
    }
}

This is called from a SwiftUI View with me creating the object in the beginning:

let locationManager = LocationManager()

with this in the view:

.onAppear() {
    locationManager.locationPermission()
    locationManager.startScanning()
}

Additionally, the UUID is from a separate file

enum Beacon {
    static let beaconUUID = UUID(uuidString: "1810C112-B26E-49EB-8EB0-B8DB2DDF2DFB")
}

I also tried replacing the startScanning() function with startMonitoring():

func startMonitoring() {
    let beaconRegion = CLBeaconRegion(uuid: Beacon.beaconUUID!, identifier: "CarBeacon")
    locationManager.startMonitoring(for: beaconRegion)
}

and adding a new callback function:

func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
    if let beaconRegion = region as? CLBeaconRegion, beaconRegion.uuid == Beacon.beaconUUID {
        print("Hello There")
    }
}

Which led to the same results of having no console output.

Many of the tutorials that I found seemed to be outdated or not written in swift and I know that the startRangingBeacons() function is already depreciated so I struggled to put random bits of information together. I would appreciate any help that you could give me.

CodePudding user response:

A few things to check:

  • Use an off the shelf beacon scanner like Locate Beacon to confirm it can detect your transmitter. Be sure to configure your UUID with the iOS scanner app.

  • Go to settings -> apps -> your app -> permissions and confirm location permission is granted

  • Add debug lines or print statements when you start ranging and make sure you see them.

  • Related