iBeacon测距和接近是有缺陷的

mrE*_*pty 2 cllocationmanager nsnotificationcenter ios ibeacon

我正在上课来处理我所有的iBeacon测试.它的目的是开始寻找区域,范围信标,识别它们然后发送通知.代码如下.

我遇到的问题是应用程序运行速度非常慢,我知道iBeacons有延迟问题,有时只是停止工作(不会识别关闭信标).我知道,我的代码很混乱,在我清理它之前尝试对逻辑进行排序.我想知道我是否错过了一个逻辑缺陷(我的意思是,我想知道我引入了哪些逻辑漏洞!).

#import "dcBeaconManager.h"

@implementation dcBeaconManager

@synthesize currentBeaconState;

bool testRanging = false;
int firstRegionEntered = 0;
int beaconsRangedCount = 0;

- (void)initBeaconManager {
    NSLog(@"initBeaconManager called");
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    NSUUID *uuid = [[NSUUID alloc]initWithUUIDString:@"B9407F30-F5F8-466E-AFF9-25556B57FE6D"];
    self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@"digiConsRegion"];
    [self.locationManager startMonitoringForRegion:self.beaconRegion];
    [self.locationManager requestStateForRegion:self.beaconRegion];

    currentBeaconState = @"initial";
}

- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
    NSLog(@"Started looking for regions");
    [self.locationManager requestStateForRegion:self.beaconRegion];
}

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
    NSLog(@"Region discovered");
    if (firstRegionEntered == 0) {
        NSLog(@"First time in region");
        UILocalNotification *notification = [[UILocalNotification alloc] init];
        notification.alertBody = @"Welcome to Digial Conversations, we are upstairs.";
        notification.soundName = UILocalNotificationDefaultSoundName;
        [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
        firstRegionEntered = 1;
    }
    [self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
}

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

    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.alertBody = @"We hope you enjoyed the event, thank you for coming.";
    notification.soundName = UILocalNotificationDefaultSoundName;
    [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}

- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
    CLBeacon *beacon = [[CLBeacon alloc] init];
    beacon = [beacons lastObject];
    NSNumber *currentBeaconMajor = beacon.major;  //it's major (group) number
    NSNumber *currentBeaconMinor = beacon.minor;  //it's minor (individual) number

    if (([currentBeaconMinor floatValue] == 59204) && ([currentBeaconMajor floatValue] == 33995) && (beacon.proximity == CLProximityNear)) {
        if (beaconsRangedCount == 0) {
            currentBeaconState = @"Mint";
            beaconsRangedCount ++;
        }
        if ([currentBeaconState isEqualToString:@"Blue"] || [currentBeaconState isEqualToString:@"Purple"]) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"didLocateMint" object:nil];
        }

    } else if (([currentBeaconMinor floatValue] == 7451) && ([currentBeaconMajor floatValue] == 63627) && (beacon.proximity == CLProximityNear)) {
        if (beaconsRangedCount == 0) {
            currentBeaconState = @"Blue";
            beaconsRangedCount ++;
        }
        if ([currentBeaconState isEqualToString:@"Mint"] || [currentBeaconState isEqualToString:@"Purple"]) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"didLocateBlue" object:nil];
        }

    } else if (([currentBeaconMinor floatValue] == 51657) && ([currentBeaconMajor floatValue] == 26976) && (beacon.proximity == CLProximityNear)) {
        if (beaconsRangedCount == 0) {
            currentBeaconState = @"Purple";
            beaconsRangedCount ++;
        }
        if ([currentBeaconState isEqualToString:@"Mint"] || [currentBeaconState isEqualToString:@"Blue"]) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"didLocatePurple" object:nil];
        }
    } else {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"didLeaveNearRegion" object:nil];
    }
}

@end
Run Code Online (Sandbox Code Playgroud)

dav*_*ung 9

你的意思是didEnterRegion和didExitRegion回调被推迟了吗?

如果您的应用程序在测距期间在前台运行,您应该在一秒钟内输入区域通知,并在几秒钟内退出区域通知.如果您的应用位于后台,则最多可能需要15分钟才能获得区域内或区域外通知.

有关此时间的详细信息,请参见此处.

这些延迟问题不是信标特定的.它们与iOS中实现CoreLocation API的方式有关.