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.)