ios - iBeacon 测距和接近度有问题

标签 ios cllocationmanager nsnotificationcenter 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

最佳答案

您是说 didEnterRegion 和 didExitRegion 回调被延迟了吗?

如果您的应用程序在您测距时在前台运行,您应该会在一秒钟内收到进入区域通知,并在几秒钟内收到退出区域通知。如果您的应用在后台运行,最多可能需要 15 分钟才能收到区域内或区域外通知。

有关此时间的详细信息,请参阅 here .

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

关于ios - iBeacon 测距和接近度有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20547729/

相关文章:

ios - 传递 NSNotifications。好的?坏的? NBD?

ios - 配置 AdMob 后随机弹出麦克风权限

ios - CLLocationManager:allowDeferredLocationUpdates - 不断获取操作无法完成。 (kCLErrorDomain 错误 11。)

ios - 在父 UIViewController 中收到多个 NSNotifications

iphone - CLLocationManager 初始化并调用按钮

带有 CoreLocation 的 iOS 地理围栏

swift - 使用相同的reuseidentifier重新加载各种 TableView

java - 无法使用 NSInputStream 在 iOS 客户端中读取来自 Java 服务器的响应

objective-c - 通用应用程序 : Should I extract shared Views to their own storyboard

iPhone 不会在断点处停止