0

I am putting together a simple estimote test using swift and coreLocation. However I am not getting didEnterRegion, didExitRegion

I read following answers already but it still didn't end my problem.

startMonitoringForRegion never calls didEnterRegion/didExitRegion

locationManager:didEnterRegion not called when a beacon is detected

I added Background modes (Location updates)

I get the "didStartMonitoringForRegion" log in the output but then don't get any didenter or exit. I tried to walk out of house with beacon and back in but no luck. Ranging however works.

var locManager: CLLocationManager = CLLocationManager()

let iceRegion: CLBeaconRegion = CLBeaconRegion(proximityUUID: BEACON_PROXIMITY_UUID,  major: BEACON_ICE_MAJOR,  identifier: "ice")

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    if locManager.respondsToSelector("requestAlwaysAuthorization") {
        locManager.requestAlwaysAuthorization()
    }

    if (self.deviceSettingsAreCorrect())
    {
        iceRegion.notifyOnEntry = true
        iceRegion.notifyOnExit = true
        iceRegion.notifyEntryStateOnDisplay = true

        locManager.delegate = self

        locManager.startMonitoringForRegion(iceRegion)
        locManager.startRangingBeaconsInRegion(iceRegion)
    }
}

func locationManager(manager: CLLocationManager!, didStartMonitoringForRegion region: CLRegion!) {
    println("didStartMonitoringForRegion");
    locManager.requestStateForRegion(region);
}

func locationManager(manager: CLLocationManager!, didEnterRegion region: CLRegion!) {
    println("did Enter Region")
}

func locationManager(manager: CLLocationManager!, didExitRegion region: CLRegion!) {
    println("did Exit Region")
}

func locationManager(manager: CLLocationManager!, didDetermineState state: CLRegionState, forRegion region: CLRegion!) {
    println("didDetermineState \(state)");

    switch state {
    case .Inside:
        println("BeaconManager:didDetermineState CLRegionState.Inside");
    case .Outside:
        println("BeaconManager:didDetermineState CLRegionState.Outside");
    case .Unknown:
        println("BeaconManager:didDetermineState CLRegionState.Unknown");
    default:
        println("BeaconManager:didDetermineState default");
    }
}

func locationManager(manager: CLLocationManager!, didRangeBeacons beacons: [CLBeacon]!, inRegion region: CLBeaconRegion!) {
    println("BM didRangeBeacons");

    for beacon: CLBeacon in beacons {
        // TODO: better way to unwrap optionals?
        if let major: String = beacon.major?.stringValue {
            if let minor: String = beacon.minor?.stringValue {
                println(major)
            }
        }
    }
}

func deviceSettingsAreCorrect() -> Bool {
    var errorMessage = ""

    if !CLLocationManager.locationServicesEnabled()
        || (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Denied) {

            errorMessage += "Location services are turned off! Please turn them on!\n"
    }

    if !CLLocationManager.isRangingAvailable() {
        errorMessage += "Ranging not available!\n"
    }

    if !CLLocationManager.isMonitoringAvailableForClass(CLBeaconRegion) {
        errorMessage += "Beacon monitoring not supported!\n"
    }

    let errorLen = countElements(errorMessage)

    if errorLen > 0 {
        println(errorMessage)
    }

    return errorLen == 0
}
Community
  • 1
  • 1
judopro
  • 35
  • 7
  • What does didDetermineState say? You can use [requestStateForRegion](https://developer.apple.com/library/prerelease/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/occ/instm/CLLocationManager/requestStateForRegion:) to force a callback to this delegate. How many beacons does the ranging pick up? Finally, what iOS device do you use? I've had some problems with monitoring on iPhone 4S in iOS 8, it just couldn't converge on a state. – heypiotr Mar 02 '15 at 18:17
  • some more debugging reveals... I get State Inside and Outside for the region appropriately, or when i range the beacons and got the closest one, it gives a meaningful accuracy (distance in m.) so I don't see a problem there but I never receive enterRegion exitRegion and that's what I actually need. I checked Estimote Notification Demo and that didn't give me any exter exit notifications eiter strangely. I am running on iphone 6 ios 8.1.3 – judopro Mar 02 '15 at 23:51

0 Answers0