locationManager:didEnterRegion not called when a beacon is detected

Dhanesh KM picture Dhanesh KM · Oct 8, 2013 · Viewed 19k times · Source

While testing with beacons (iOS devices) I found the listener beacon giving some unexpected behavior. locationManager:didEnterRegion method is not getting called even if a beacon enters a region. But the locationManager:didRangeBeacons:inRegion: is getting called correctly, and detected beacons are shown there. Has anyone experienced anything like this.

Answer

Muhammad Ibrahim picture Muhammad Ibrahim · Dec 27, 2013

Check if your methods are implemented in the following way. In viewDidLoad, start moniotoring at the end

self.beaconRegion.notifyOnEntry=YES;
self.beaconRegion.notifyOnExit=YES;
self.beaconRegion.notifyEntryStateOnDisplay=YES;
[self.locationManager startMonitoringForRegion:self.beaconRegion];

after monitoring start, request state for your defined region

- (void) locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region
{
    [self.locationManager requestStateForRegion:self.beaconRegion];
}

after state is determined, start ranging beacons

-(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
    if (state == CLRegionStateInside)
    {
        //Start Ranging
        [manager startRangingBeaconsInRegion:self.beaconRegion];
    }
    else
    {
        //Stop Ranging here
    }
}

and implement the following methods according to your needs...

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
    self.statusLbl.text=@"Entered region";
}

-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
    self.statusLbl.text=@"Exited region";
}

-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region
{
    if(beacons.count>0)
    {}
}

Hope this will solve your problem.