在背景中的测距信标

时间:2015-10-14 13:38:52

标签: ios objective-c ibeacon appdelegate

我想在后台敲响信标。有了背景,我的意思是当手机进入锁屏时。我希望该应用程序继续范围信标。我现在的问题是代码永远不会发现信标。我有两个正在工作的信标,但AppDelegate找不到它们。当我在ViewController中运行相同的代码时,它会找到信标并显示它们。我该怎么办?

@interface BDAppDelegate () <AXABeaconManagerDelegate>

@end

@implementation BDAppDelegate {
    NSMutableDictionary *beaconRegions;
    NSMutableDictionary *detectBeacons;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:@"MyUUID"] identifier:@"微信"];

    [AXABeaconManager sharedManager].beaconDelegate = self;
    [[AXABeaconManager sharedManager] requestAlwaysAuthorization];
    [[AXABeaconManager sharedManager] startRangingBeaconsInRegion:beaconRegion];

    self->beaconRegions = [[NSMutableDictionary alloc] init];
    self->detectBeacons = [[NSMutableDictionary alloc] init];

    while (detectBeacons.count < 10) {
        NSLog(@"Rows in detectBeacons %lu", (unsigned long)beaconRegions.count);
    }

    self->beaconRegions[beaconRegion] = [NSArray array];
}

- (void)didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
    self->beaconRegions[region] = beacons;
    NSMutableArray *allBeacons = [NSMutableArray array];

    for (NSArray *regionResult in [self->beaconRegions allValues])
    {
        [allBeacons addObjectsFromArray:regionResult];
    }

    NSPredicate *pre = [NSPredicate predicateWithFormat:@"accuracy != -1"];

    NSArray *rights = [allBeacons filteredArrayUsingPredicate:pre];

    NSString * str = @"accuracy";
    self->detectBeacons[str] = rights;
}

@end

3 个答案:

答案 0 :(得分:3)

在iOS上,应用仅限于在后台播放5秒。每次将应用程序置于后台时,或者当信标监视事件(输入的区域/退出区域)触发时,都会重新启动此计时器。好消息是,你可以在每次事件发生后3分钟将背景范围内的信标延长到3分钟。

我整理了一篇博客文章,向您展示如何操作here.

答案 1 :(得分:0)

对于 CLLocationManager ,有一个方法startMonitoringForRegion(CLBeaconRegion *):beaconRegion

应该在我们开始startRangingBeaconsInRegion之前添加。

因此,如果您的AXABeaconManager类来自CLLocationManager,请添加以下内容:

[[AXABeaconManager sharedManager] startMonitoringForRegion:beaconRegion];

否则:     创建一个CLLocationManager对象locationManager并初始化它,然后添加如下所示的启动监视。

[self.locationManager startMonitoringForRegion:beaconRegion];

之前startRangingBeaconsInRegion

讨论:startMonitoringForRegion 您必须为要监视的每个区域调用此方法一次。如果应用程序已在监视具有相同标识符的现有区域,则旧区域将替换为新区域。您使用此方法添加的区域由应用中的所有位置管理器对象共享,并存储在monitoredRegions属性中。

更多参考here

答案 2 :(得分:0)

信标的范围是一项消耗大量电池的操作,iOS不允许你在BG中无休止地进行操作(大部分时间。有些情况下,它在哪里作品)

你要做的就是调用iOS来做BG工作:

 UIBackgroundTaskIdentifier token = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
     NSLog(@"Ranging for region %@ killed", region.identifier);
 }];
 if(token == UIBackgroundTaskInvalid) {
     NSLog(@"cant start background task");
 }

那么做什么

完成后,请致电endBackgroundTask

相关问题