TheZozoOwner
TheZozoOwner

Reputation: 532

Loop in background mode

I want to search for RSSI in the background. I am trying to do it by calling the function in the same function. Is there anyway of making this loop to live in the background mode. As i see it, it should be living in the background, but i dies after som milliesecounds. How to make it keeping searching?

- (void)applicationWillResignActive:(UIApplication *)application
{
    if([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)])
    {
        NSLog(@"Multitasking Supported");
    }
    else
    {
        NSLog(@"Multitasking Not Supported");
    }

    NSLog(@"Start background task");
    mBeaconDeviceList = [[NSMutableArray alloc] init];
    mBeaconConfigManager = [[JLEBeaconConfigManager alloc] init];
    mBeaconConfigManager.delegate = self;
    [mBeaconConfigManager startJaaleeBeaconsDiscovery];
}

#pragma mark - JLEBeaconConfigManager delegate
-(void)beaconConfigManager:(JLEBeaconConfigManager *)manager didDiscoverBeacon:(JLEBeaconDevice *)beacon RSSI:(NSNumber *)RSSI AdvData:(NSDictionary *)AdvData
{
    NSLog(@"Find RSSI");
    if ([RSSI intValue] > 0 || [RSSI intValue] < -40) {
        return;
    }

    if ([mBeaconDeviceList containsObject:beacon]) {
        [mBeaconDeviceList removeObject:beacon];
    }
    [mBeaconDeviceList addObject:beacon];
    NSLog(@"FOUND: %@", RSSI);

    NSLog(@"Start again");
    mBeaconDeviceList = [[NSMutableArray alloc] init];
    mBeaconConfigManager.delegate = self;
    [mBeaconConfigManager startJaaleeBeaconsDiscovery];
}

Upvotes: 1

Views: 182

Answers (1)

arc4randall
arc4randall

Reputation: 3293

You can extend the background time to 3 minutes, which would allow you more time to search from the RSSIs. You can extend it in the applicationDidEnterBackground like this:

- (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];
        }

    });
}

Upvotes: 3

Related Questions