Pheepster
Pheepster

Reputation: 6367

CLLocationManager delegate methods not called with iBeacon Simulator

I am attempting to create a demo app that monitors iBeacons. I am using a table view and a custom cell that is a location manager delegate.

I am using Estimote's beacon simulator that allows using an iPhone as a beacon. When the app runs, the location manager's didRangeBeacons:method is called but none of the other delegate methods are called. I have used the Estimote app to ensure that my iPhone is definitely broadcasting.

startMonitoringBeacon: is called when the UITableViewCell is created in the parent view controller.

Here is my custom UITableViewCell

@implementation PTBeaconTableViewCell

- (void)awakeFromNib {
    self.locationMgr = [[CLLocationManager alloc] init];
    self.locationMgr.delegate = self;
}

-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
    for(CLBeacon* beacon in beacons) {
        if([_beacon isEqualToBeacon:beacon]) {
            NSString* foundMsg = [NSString stringWithFormat:@"Found: %@", _beacon.uuid.UUIDString];
            [self.detailLabel setText:foundMsg];
        }
    }
}

-(NSString*)getTimeStamp {
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"hh:mm:ss"];
    return [df stringFromDate:[NSDate date]];
}

-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
    NSString* message = [NSString stringWithFormat:@"Exited at %@", [self getTimeStamp]];
    [self.statusLabel setText:message];
}

-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
    NSString* message = [NSString stringWithFormat:@"Region Entered at %@", [self getTimeStamp]];
    [self.statusLabel setText:message];
}

-(CLBeaconRegion*)getRegionForBeacon:(PTBeacon*)beacon {
    return [[CLBeaconRegion alloc] initWithProximityUUID:_beacon.uuid major:_beacon.major minor:_beacon.minor identifier:_beacon.name];
}

-(void)startMonitoringBeacon:(PTBeacon*)beacon {
    CLBeaconRegion* region = [self getRegionForBeacon:beacon];
    [self.locationMgr startMonitoringForRegion:region];
    [self.locationMgr startRangingBeaconsInRegion:region];
    [self.statusLabel setText:[NSString stringWithFormat:@"Monitoring began at %@", [self getTimeStamp]]];
}

-(void)stopMonitoringBeacon:(PTBeacon*)beacon {
    CLBeaconRegion* region = [self getRegionForBeacon:beacon];
    [self.locationMgr stopMonitoringForRegion:region];
    [self.locationMgr stopRangingBeaconsInRegion:region];
    [self.statusLabel setText:[NSString stringWithFormat:@"Monitoring stopped at %@", [self getTimeStamp]]];
}

@end

Any suggestions on what may be missing here? Thanks!

Upvotes: 1

Views: 458

Answers (2)

Abhinav
Abhinav

Reputation: 38162

First of all why are you putting location manager handling in UITableViewCell. It should be in some view controller rather. Cells are used/reused every now and then. Secondly, during region monitoring/ranging, not getting call backs on some of the delegates like locationManager:monitoringDidFailForRegion:withError: & locationManager:didFailWithError: is alright. Please go through this nicely put up tutorial.

Upvotes: 1

r4id4
r4id4

Reputation: 6087

Try to start the broadcasting of Estimote app when your application is already on and monitoring. It would simulate you entering the region that should trigger didEnterRegion. Stopping the broadcasting on the other hand triggers didExitRegion.

Stupid question but try to println() a log message inside those method. I see you just update a label, but is it properly connected in your Storyboard/xib?

Upvotes: 2

Related Questions