2

I'm using local notifications triggered by iBeacons in my app. It works fine both in foreground and background as long as the iPhone is active, but doesn't trigger didEnterRegion after about 15 minutes of inactivity or reboot.

It then only fires again on wake up of the iPhone with the home button or sleep button, but I want didEnterRegion to "fire" when the iPhone is in a pocket for 15 minutes or more while entering the region as well.

Is this possible? If yes, how?

Background modes > Location updates is disabled

Some code:

.h

@property (strong, nonatomic) CLBeaconRegion *beaconRegion;
@property (strong, nonatomic) CLLocationManager *locationManager;

.m

- (void)start {
    self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@"com.bla.bla"];

    self.beaconRegion.notifyOnEntry = YES;
    self.beaconRegion.notifyOnExit = YES;
    self.beaconRegion.notifyEntryStateOnDisplay = YES; 
    self.locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
    [self.locationManager startUpdatingLocation];

    [self.locationManager startMonitoringForRegion:self.beaconRegion];
    [self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
    [self.locationManager requestStateForRegion:self.beaconRegion];
}

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

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

- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
{
    NSLog(@"%@", [error localizedDescription]);
} 

-(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region {
    if (state == CLRegionStateInside) {
        [manager startRangingBeaconsInRegion:self.beaconRegion];
    } else {
        [manager stopRangingBeaconsInRegion:self.beaconRegion];
    }
}
Mathijs
  • 1,258
  • 1
  • 10
  • 27

1 Answers1

1

I'm not sure what is going on here, but the experience described in the question is not consistent with what I have seen testing on multiple devices. Posting the code that sets this up might help figure out some answers.

In several apps, I have been able to get background didEnterRegion callbacks, even after much more than 15 minutes of inactivity without pressing the shoulder button or the home button. And to do this, I have not had to set any background modes. (Apple will actually reject your app if you submit it to the store with background mode Location updates set unnecessarily.)

There is a bug in iOS 7.1 that cam halt iBeacon detections at some point after boot, so perhaps this is what is going on in this case. Details are here. Unfortunately, testing this hypothesis would require you to wake up the screen in order to turn bluetooth off and on to clear the condition, and that would wake up your screen and give you the region exit anyway. Perhaps you could try setting beaconregion.notifyEntryStateOnDisplay=NO, recreating this condition then try cycling bluetooth to see if you then get the notification. You can also use an off the shelf beacon scanning app like Locate for iBeacon to see if your device is able to range for iBeacons at all after it gets into this state, and only cycle to Bluetooth if you are unable to detect iBeacons.

Community
  • 1
  • 1
davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • I've added some code. No result with turning Bluetooth on/off and notifyEntryStateOnDisplay=NO. It's not the 7.1 bug in this case. – Mathijs Apr 30 '14 at 17:35
  • notifyEntryStateOnDisplay=NO seems to do the trick after all. The documentation at https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLBeaconRegion_class/Reference/Reference.html#//apple_ref/occ/instp/CLBeaconRegion/notifyEntryStateOnDisplay is not really clear I think. It should say that when this property is set to YES beacon notifications will ONLY be sent on wake up – Mathijs May 02 '14 at 08:01
  • This is a suprising observation. I am not sure the difference, but it is not consistent with my observations here: http://developer.radiusnetworks.com/2013/11/13/ibeacon-monitoring-in-the-background-and-foreground.html – davidgyoung May 02 '14 at 13:46