Ios信标监控didEnterRegion事件仅在ipad"即使我将desiredAccuracy和distanceFilter字段设置为100米

时间:2017-03-22 06:36:52

标签: ios objective-c core-location beacon

在下面的代码中,我将字段 desiredAccuracy distanceFilter 设置为 100 。但它不能正常运作。正如我在标题上提到的那样,当我把信标放在ipad上时,didEnterRegion开火,当我收回时,大约15秒后, didExitRegion 开火。我错过了什么吗?

#import "AppDelegate.h" 
#import <CoreLocation/CoreLocation.h>
#import <CoreBluetooth/CoreBluetooth.h>

@interface AppDelegate() <CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (assign, nonatomic) BOOL dropEmptyRanges;
@end

@implementation AppDelegate

- (instancetype)init
{
  if (self = [super init]) {
    self.locationManager = [[CLLocationManager alloc] init];

    self.locationManager.delegate = self;
    self.locationManager.pausesLocationUpdatesAutomatically = NO;
    self.locationManager.distanceFilter = 100;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;

  }

  return self;
}

-(CLBeaconRegion *) createBeaconRegion: (NSString *) identifier
                                  uuid: (NSString *) uuid
                                 major: (NSInteger) major
                                 minor:(NSInteger) minor
{
  NSUUID *beaconUUID = [[NSUUID alloc] initWithUUIDString:uuid];

  unsigned short mj = (unsigned short) major;
  unsigned short mi = (unsigned short) minor;

  CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:beaconUUID major:mj
                                                                         minor:mi
                                                                    identifier:identifier];

  NSLog(@"createBeaconRegion with: identifier - uuid - major - minor");
  beaconRegion.notifyOnEntry = YES;
  beaconRegion.notifyOnExit = YES;
  beaconRegion.notifyEntryStateOnDisplay = YES;

  return beaconRegion;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
    [self.locationManager requestAlwaysAuthorization];
  }

  CLBeaconRegion *beaconRegion = [self createBeaconRegion:@"backgroundRegion" uuid:@"fda50693-a4e2-4fb1-afcf-c6eb07647825" major:10004 minor:5178];

  [self.locationManager startMonitoringForRegion:beaconRegion];

  [self.locationManager startRangingBeaconsInRegion:beaconRegion];

  [self.locationManager startUpdatingLocation];

  if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound|UIUserNotificationTypeBadge categories:nil]];
  }

  return YES;
}

-(void)locationManager:(CLLocationManager *)manager
        didEnterRegion:(CLBeaconRegion *)region {
  NSLog(@"Beacon did enter region");
  UILocalNotification* localNotification = [[UILocalNotification alloc] init];
  localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:3];
  localNotification.alertBody = @"Beacon did enter region";
  localNotification.timeZone = [NSTimeZone defaultTimeZone];
  [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

}

-(void)locationManager:(CLLocationManager *)manager
         didExitRegion:(CLBeaconRegion *)region {
  NSLog(@"Beacon did exit region");
  UILocalNotification* localNotification = [[UILocalNotification alloc] init];
  localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:3];
  localNotification.alertBody = @"Beacon did exit region";
  localNotification.timeZone = [NSTimeZone defaultTimeZone];
  [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];


}

-(void)locationManager:(CLLocationManager*)manager
       didRangeBeacons: (NSArray *) beacons
              inRegion:(CLBeaconRegion *)region{
  NSLog(@"Beacon did range");
}

@end

2 个答案:

答案 0 :(得分:0)

你目前没有任何信标。尝试添加以下行:

-(void)locationManager:(CLLocationManager *)manager
        didEnterRegion:(CLBeaconRegion *)region {
  NSLog(@"Beacon did enter region");
  UILocalNotification* localNotification = [[UILocalNotification alloc] init];
  localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:3];
  localNotification.alertBody = @"Beacon did enter region";
  localNotification.timeZone = [NSTimeZone defaultTimeZone];
  [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
  [manager startRangingBeaconsInRegion:region];
}

-(void)locationManager:(CLLocationManager *)manager
         didExitRegion:(CLBeaconRegion *)region {
  NSLog(@"Beacon did exit region");
  UILocalNotification* localNotification = [[UILocalNotification alloc] init];
  localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:3];
  localNotification.alertBody = @"Beacon did exit region";
  localNotification.timeZone = [NSTimeZone defaultTimeZone];
  [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
  [manager stopRangingBeaconsInRegion:region];

}

答案 1 :(得分:0)

不幸的是,此技术不适用于限制信标检测。

字段locationManager.distanceFilterlocationManager.desiredAccuracy不适用于信标检测。当基于GPS / Cell / WiFi定位获取位置更新时,这些仅对CoreLocation的回调有任何影响。文档here说的很多:

  

此属性仅与标准位置服务结合使用,在监控重要位置更改时不会使用。

虽然该声明听起来有点模糊,但“标准位置服务”的含义是当您致电startUpdatingLocation以获取这些非信标位置更新时。您可以阅读有关此here的更多信息。虽然上面引用的内容没有明确说明,但如果它还说“此属性仅与标准位置服务一起使用,并且在监视重要位置更改或监视信标区域时未使用”,则会更清楚。

如果您希望根据距离限制信标检测,则无法使用监控API执行此操作。您必须为信标设置范围,每隔一秒获得一次距离估算,并且只有在您看到估计距离小于所需阈值时才会触发事件。