在iPhone锁定和睡眠时测距和监控信标

时间:2015-09-03 13:01:17

标签: ios objective-c iphone ibeacon beacon

所以我正在创建一个iOS应用程序,而且我在后台设置了信标。一旦我的iPhone醒来它就能很好地运行,即使iPhone被锁定它也能继续工作......但iPhone仍然必须清醒。一旦iPhone进入睡眠状态,我的应用程序范围大约10次,然后停止。如果你唤醒iPhone,它会再次启动。

我也尝试过监控,但没有运气。

有人能告诉我这是否可能吗?我到处搜索过,无法找到答案!请在下面找到我的信标方法(位于AppDelegate中)

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];

    self.locationManager = [[CLLocationManager alloc] init];

    if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        [self.locationManager requestAlwaysAuthorization];
    }

    self.locationManager.delegate = self;
    self.locationManager.pausesLocationUpdatesAutomatically = NO;

    [self.locationManager startUpdatingLocation];
    return YES;
}

-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
    if(beacons.count > 0) {
        CLBeacon *nearestBeacon = beacons.firstObject;

        if (nearestBeacon.proximity == CLProximityImmediate || nearestBeacon.proximity == CLProximityNear) {
            NSLog("Beacon detected");
        }
    }
}

- (void)startRangingBeacons {
    self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:@"EBEFD083-70A2-47C8-9837-E7B5634DF525" identifier:@"receptionBeacon"];
    [self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
}

感谢任何帮助 谢谢 索尼娅

1 个答案:

答案 0 :(得分:1)

有一种方法可以唤醒手机。如果您的应用程序属于信标区域,那么

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region

上面的方法会自动调用。所以你需要把一些本地通知,比如“你就在灯塔区附近”来唤醒手机。 但你必须重新进入该地区。

一旦您的应用程序进入该区域,请编写以下代码以重新启动信标进程,

[self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
UIBackgroundTaskIdentifier bgTask = [[UIApplication  sharedApplication] 
                                      beginBackgroundTaskWithExpirationHandler:^{
      NSLog(@"End of tolerate time. Application should be suspended now if we do not ask more 'tolerance'");
     [[UIApplication sharedApplication] endBackgroundTask:UIBackgroundTaskInvalid];
}];

if (bgTask == UIBackgroundTaskInvalid)
{
     NSLog(@"This application does not support background mode");
} else {
     //if application supports background mode, we'll see this log.
     NSLog(@"Application will continue to run in background");
}

希望这对你有用。