Ios信标监控仅限区域事件触发”;在ipad上;即使我将desiredAccuracy和distanceFilter字段设置为100米
在下面的代码中,我将字段desiredAccuracy和distanceFilter设置为100。但它并没有发挥应有的作用。正如我在标题中提到的,当我在ipad上安装beacon时,didEnterRegion启动,当我返回时,大约15秒后,didExitRegion启动。我错过什么了吗Ios信标监控仅限区域事件触发”;在ipad上;即使我将desiredAccuracy和distanceFilter字段设置为100米,ios,objective-c,core-location,beacon,Ios,Objective C,Core Location,Beacon,在下面的代码中,我将字段desiredAccuracy和distanceFilter设置为100。但它并没有发挥应有的作用。正如我在标题中提到的,当我在ipad上安装beacon时,didEnterRegion启动,当我返回时,大约15秒后,didExitRegion启动。我错过什么了吗 #import "AppDelegate.h" #import <CoreLocation/CoreLocation.h> #import <CoreBluetooth/CoreBlueto
#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
#导入“AppDelegate.h”
#进口
#进口
@接口AppDelegate()
@属性(强,非原子)CLLocationManager*locationManager;
@属性(赋值,非原子)BOOL-dropEmptyRange;
@结束
@实现AppDelegate
-(instancetype)初始化
{
if(self=[super init]){
self.locationManager=[[CLLocationManager alloc]init];
self.locationManager.delegate=self;
self.locationManager.pausesLocationUpdatesAutomatically=否;
self.locationManager.distanceFilter=100;
self.locationManager.desiredAccuracy=KCallocationAccuracyHundredMeters;
}
回归自我;
}
-(CLBeaconRegion*)createBeaconRegion:(NSString*)标识符
uuid:(NSString*)uuid
少校:(NSInteger)少校
小调:(NSInteger)小调
{
nsuid*beaconUUID=[[nsuid alloc]initWithUUIDString:uuid];
无符号短距离mj=(无符号短距离)主距离;
无符号短mi=(无符号短)次;
CLBeaconRegion*beaconRegion=[[CLBeaconRegion alloc]INITWITHPROXIMITYUID:beaconUUID major:mj
小调:米
标识符:标识符];
NSLog(@“createBeaconRegion with:identifier-uuid-major-minor”);
beaconRegion.notifyOnEntry=是;
beaconRegion.notifyOnExit=是;
beaconRegion.NotifyEntryStateondDisplay=是;
返回信标区域;
}
-(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项
{
if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]){
[self.locationManager请求始终授权];
}
CLBeaconRegion*beaconRegion=[自行创建的beaconRegion:“背景区域”uuid:“fda50693-a4e2-4fb1-afcf-c6eb07647825”大调:10004小调:5178];
[self.locationManager startMonitoringForRegion:beaconRegion];
[self.locationManager startrangingbeaconregion:beaconRegion];
[self.locationManager startUpdatingLocation];
if([UIApplication InstanceRespondtoSelector:@selector(registerUserNotificationSettings:)])){
[[UIApplication sharedApplication]registerUserNotificationSettings:[UIUserNotificationSettings设置类型:UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge类别:nil];
}
返回YES;
}
-(无效)locationManager:(CLLocationManager*)经理
didEnterRegion:(CLBeaconRegion*)区域{
NSLog(@“信标未进入区域”);
UILocalNotification*localNotification=[[UILocalNotification alloc]init];
localNotification.fireDate=[NSDate dateWithTimeIntervalSinceNow:3];
localNotification.alertBody=@“信标未进入区域”;
localNotification.timeZone=[NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
}
-(无效)locationManager:(CLLocationManager*)经理
didExitRegion:(CLBeaconRegion*)区域{
NSLog(@“信标未退出区域”);
UILocalNotification*localNotification=[[UILocalNotification alloc]init];
localNotification.fireDate=[NSDate dateWithTimeIntervalSinceNow:3];
localNotification.alertBody=@“信标已退出区域”;
localNotification.timeZone=[NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
}
-(无效)locationManager:(CLLocationManager*)经理
DidRange信标:(NSArray*)信标
区域:(CLBeaconRegion*)区域{
NSLog(@“信标did范围”);
}
@结束
您当前没有对任何信标进行测距。尝试添加以下行:
-(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];
}
不幸的是,这种技术无法限制信标检测。 字段
locationManager.distanceFilter
和locationManager.desiredAccuracy
不适用于信标检测。当获取基于GPS/Cell/WiFi定位的位置更新时,这些仅对CoreLocation的回调产生任何影响。文件中说:
此属性仅与标准位置服务结合使用,在监视重大位置更改时不使用
虽然这句话听起来有点含糊不清,但“标准位置服务”的意思是当您调用startUpdatingLocation
获取这些非信标位置更新时。你可以阅读更多关于这方面的内容。尽管上面的引文没有明确说明,但如果它还说“此属性仅与标准位置服务结合使用,并且在监控重大位置变化或监控信标区域时不使用,则更清楚。”
如果要根据距离限制信标检测,则不能使用监视API。您必须对信标进行测距,每秒钟进行一次距离估计,并且只有当您发现估计距离小于所需阈值时才触发事件。感谢Allan的回复,在didFinishLaunchingWithOptions方法中,我称之为StartTransgingBeaconsisnRegion和startMonitoringForRegion方法。didEnterRegion和DidextRegion现在已经成功运行。我希望我的应用程序能扫描更大范围内的信标信号。现在,didEnterRegion离ipad太近了。当我稍微改变一下ipad的位置时,didExitRegion事件就会触发。谢谢你的明确解释。我将尝试在区域进入后通过测距信标来实现距离检测。