在iOS背景中的灯塔测距 [英] Beacon Ranging in Background on iOS

查看:94
本文介绍了在iOS背景中的灯塔测距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解监控和测距之间的区别,我理解iOS中的信标范围的限制只能在进入和退出区域时在前台或后台发生,如此处所述(http://developer.radiusnetworks.com/2013/11/13/ibeacon-monitoring-内式背景和 - foreground.html )。但我正在试图弄清楚如何解决一个常见的情况。

I understand the difference between monitoring and ranging, and I understand the limitation of iOS in that beacon ranging can only happen in the foreground or in the background when entering and exiting regions as explained here (http://developer.radiusnetworks.com/2013/11/13/ibeacon-monitoring-in-the-background-and-foreground.html). But I'm trying to figure out how to solve a common scenario.

如果我在百货商店安装了一堆信标,我该如何检测到什么时候一个人在那些信标的范围内移动?通过它当前的工作方式,当用户进入商店时,应用程序将获得一个事件( didEnterRegion ),因为所有信标的集合都充当一个大区域。但是没有办法知道用户是在商店的不同部分之间移动,除非信标放置得足够远,以便用户退出并再次进入该区域,这可能是不切实际的。

If I had a bunch of beacons installed in a department store, how am I supposed to detect when a person moves within range of those beacons? With the way it currently works, the app will get an event when the user enters the store (didEnterRegion) because the collection of all beacons acts as one big region. But there's no way to know that a user is moving between different sections of the store unless the beacons are placed far enough for the user to exit and enter the region again which is probably not practical.

我想在后台设置信标范围的原因是我可能需要知道用户在商店的特定部门/产品上,以显示该部分的特定优惠/信息(通过通知)无需用户打开应用程序。

The reason I want to range for beacons in the background is that I might need to know that a user is at a specific section/product in the store to display specific offers/information (through a notification) for that section without needing the user to have the app open.

在我看来,这对于商场和博物馆等非常常见的场景......我想知道其他开发者是怎么做的解决了这个或是否有另一种方法来实现我想要的东西。

This seems to me like a very common scenario for malls and museums, etc... I'm wondering how other developers solved this or whether there's another way of achieving what I want.

我这里没有包含代码片段,因为问题不在于代码,它只是一个概念问题。如果需要任何澄清或代码,我也可以添加。

I didn't include code snippets here because the issue is not with the code, it's just a conceptual issue. If any clarification or code is needed I can add that too.

谢谢

推荐答案

答案的最大部分是我的同事@csexton在另一个问题的答案中记录的技术。

The biggest part of the answer is the technique documented by my colleague @csexton in another answer to this question.

为了解决第二个问题在转换后获得10秒的测距时间,您可以请求额外的时间来保持测距。 iOS允许您在后台继续播放长达180秒。这不需要背景模式,也不需要AppStore的特殊许可。

In order to solve the second problem of only getting 10 seconds of ranging time after a transition, you can request additional time to keep ranging. iOS allows you to continue ranging in the background for up to 180 seconds. This requires no background modes and no special permission from the AppStore.

以下是你如何设置:

- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
    if (_inBackground) {
        [self extendBackgroundRunningTime];
    }
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [self logString: [NSString stringWithFormat:@"applicationDidEnterBackground"]];
    [self extendBackgroundRunningTime];
    _inBackground = YES;
}


- (void)extendBackgroundRunningTime {
    if (_backgroundTask != UIBackgroundTaskInvalid) {
        // if we are in here, that means the background task is already running.
        // don't restart it.
        return;
    }
    NSLog(@"Attempting to extend background running time");

    __block Boolean self_terminate = YES;

    _backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"DummyTask" expirationHandler:^{
        NSLog(@"Background task expired by iOS");
        if (self_terminate) {
            [[UIApplication sharedApplication] endBackgroundTask:_backgroundTask];
            _backgroundTask = UIBackgroundTaskInvalid;
        }
    }];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"Background task started");

        while (true) {
            NSLog(@"background time remaining: %8.2f", [UIApplication sharedApplication].backgroundTimeRemaining);
            [NSThread sleepForTimeInterval:1];
        }

    });
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [self logString: [NSString stringWithFormat:@"applicationDidBecomeActive"]];
    _inBackground = NO;
}

在后台获得180秒的射程不是银弹,但它解决了许多用例,10秒没有。

Getting 180 seconds to range in the background is no silver bullet, but it solves many use cases that 10 seconds does not.

您可以阅读有关其工作原理的完整说明,以及此处的测试结果: https://github.com/RadiusNetworks/ibeacon-background-demo/tree/background-task

You can read a full writeup on how this works, along with test results here: https://github.com/RadiusNetworks/ibeacon-background-demo/tree/background-task

这篇关于在iOS背景中的灯塔测距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆