UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Lost! Trying to make a basic IBeacon app

Forums > Swift

Hi All

I am an expierneced Java developer trying my hand at Swift and i am nothing short of stuck!

I am trying to create a basic app that when a button is clicked on a UI it initiates my iBeacon class to start monitoring for beacons in my proximity. Unfortunately I keep getting an error. Please can someone let me know if they can see any issues with the following code

UI

import SwiftUI

struct ContentView: View {
    var body: some View {
        Button(action: {
            BeaconDetector()
        }) {
            Text("Press me")
        }

    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

IBeacon Class

import Foundation
import CoreLocation

class BeaconDetector : NSObject, CLLocationManagerDelegate {

    var locationManager: CLLocationManager!

    override init() {
        super.init()
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.pausesLocationUpdatesAutomatically = false
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()
        startScanning()
    }

    func startScanning() {

        let uuid = UUID(uuidString: "2f234454-cf6d-4a0f-adf2-f4911ba9ffa6")!

        //find all beacons matching the uuid major and minor
        let constraint = CLBeaconIdentityConstraint(uuid: uuid)
        let beaconRegion = CLBeaconRegion(uuid: uuid, identifier: "test")

        locationManager.startMonitoring(for: beaconRegion)
        locationManager.startRangingBeacons(satisfying: constraint)
    }

    func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
        if beacons.count > 0 {
            NSLog("Found beacon")

            for beacon in beacons {

                NSLog("Found becaon with uuid " + beacon.uuid.uuidString + " and major:" + beacon.major.stringValue + " and minor" + beacon.minor.stringValue)

                if(beacon.proximity == CLProximity.near) {
                    NSLog("Beacon distance was near")
                } else if (beacon.proximity == CLProximity.far) {
                    NSLog("Beacon distance was far")
                } else if(beacon.proximity == CLProximity.immediate) {
                    NSLog("Beacon distance was immediate")
                } else if (beacon.proximity == CLProximity.unknown) {
                    NSLog("Beacon distance is unknown")
                }
            }

        } else {
            NSLog("Beacon not found")
        }
    }

    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
        let beaconRegion = region as! CLBeaconRegion
        print("Did enter region: " + (beaconRegion.major?.stringValue)!)
    }

    func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
        let beaconRegion = region as! CLBeaconRegion
        print("Did exit region: " + (beaconRegion.major?.stringValue)!)
    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        if status == .authorizedAlways {
            if CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self) {
                if CLLocationManager.isRangingAvailable() {
                    startScanning()
                }
            }
        }
    }

    func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) {
        NSLog(error.localizedDescription)
    }
}

The error i keep getting is as follows

IBeacon[14239:487198] The operation couldn’t be completed. (kCLErrorDomain error 5.)

2      

I think what happens is that you create BeaconDetector on button tap, but don't store it anywhere so it gets deallocated a short while later. You need either a local variable or shared instance.

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.