简体   繁体   中英

iBeacon monitoring not working properly

When there is a beacon in range of iOS device, I get notified( CLRegionStateInside ) and can start ranging. This works properly. However, when ranging is started and the the iOS devices is not in range anymore, I don't get notified(State doesn't change to CLRegionStateOutside ). Not in foreground or background.

Also didEnterRegion and didExitRegion never gets called. I start ranging in didDeterminState when state is CLRegionStateInside .

  • I do have background refresh settings enabled for my app.
  • When I start the app for the first time I do get an alert asking for location permission.

So basically, i'm able to start ranging, but i'm not able to stop ranging, because the state doesn't change to CLRegionStateOutside .

I use Xcode 6.3.1, iOS 8.3 on iPhone 4s.

This is my code:

INIT:

- (id)init{
    self = [super init];
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        [self.locationManager requestAlwaysAuthorization]; //or requestWhenInUseAuthorization
    }

    NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"B75FA2E9-3D02-480A-B05E-0C79DBB922AD"];
    self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
                                                             identifier:@"TESTREGIO"];

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

    return self;
}

DETERMINESTATE:

- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region {
    if (state == CLRegionStateInside)
    {
        [self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];

    }else{
        [self.locationManager stopRangingBeaconsInRegion:self.myBeaconRegion];
    }
}

DIDENTERREGION and DIDEXITREGION:

   - (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion*)region
    {
        NSLog(@"DidenterRegion================================================");
    }

    - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {

        NSLog(@"DidexitRegion================================================");
    }

You are not going to get a didExitRegion notification unless you were already inside a region. I suggest you track the state of the region your are looking for separately.

I don't understand what you are doing in didDetermineState. You started ranging for your beacon in init(). You would not need to start ranging again.

A few tips:

  • You do not need to request any background permissions for this to work.

  • calls to didExitRegion happen 3 seconds after a beacon is no longer detected when the device is ranging in the foreground. In the background, or in the foreground when not ranging, these calls can be delayed for up to 15 minutes. These delays always exist on iPhone 4S models. On newer models, the delays may or may not exist depending on whether there are enough hardware assist slots for detecting beacons. See details here: http://developer.radiusnetworks.com/2014/03/12/ios7-1-background-detection-times.html

I suspect the failure to get exit events is really an issue of them taking longer than expected. Wait 15 minutes and see if the callbacks come.

您还应该调用以下函数来开始测距

[self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM