How to reboot CoreBluetooth manager instance at fixed interval in background

我的未来我决定 提交于 2019-12-04 18:27:51
Julian Król

You do not need to set that requirement for background modes. You can try on applicationDidEnterBackground: call startBackgroundTask

- (void)startBackgroundTask {
    if(bgTask != UIBackgroundTaskInvalid){
        [[UIApplication sharedApplication] endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }
    bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{

        NSLog(@"your time is over");
        //you can call start once again to get more time
    }];
}

Where the bgTask is UIBackgroundTaskIdentifier bgTask;

My only worry would be that from my observations to get another time for background execution user has to touch the screen to make screen illuminating (screen can be locked). To make sure your code is working in background you can set up a timer and log a message on the console periodically (for example time remaining which system gave you on the background execution [UIApplication sharedApplication].backgroundTimeRemaining or restarting bluetooth). As far as I know it is quite common approach to that kind of problems.

one of solution is this.... But sometimes not going well..

- (void) startProcess
{
    [_manager startMonitoringForRegion:beacon];
}

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
    // some process...
    [_manager startRangingBeaconsInRegion:beacon];
}

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
    // some process...
    [_manager stopRangingBeaconsInRegion:beacon];
}

- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region
{
    // some process...
    [_manager stopRangingBeaconsInRegion:beacon];
    [_manager stopMonitoringForRegion:beacon];
    [_manager startMonitoringForRegion:beacon];
}

i've checked bluetooth scanning run in background always.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!