2016-02-29 41 views
1

我想在後臺搜索RSSI。我正在嘗試通過調用相同函數中的函數來完成此操作。有沒有辦法讓這個循環生活在後臺模式。正如我所看到的那樣,它應該生活在背景中,但是我會在som milliesecounds之後死去。如何使它保持搜索?在後臺模式下循環

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

回答

2

您可以將背景時間延長到3分鐘,這樣可以讓您有更多時間從RSSI進行搜索。你可以像這樣在applicationDidEnterBackground中擴展它:

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

    }); 
} 
+0

我不認爲時間現在是問題。問題在於'beaconConfigManager'方法在'applicationWillResignActive'處被調用,但是當它進入'applicationDidEnterBackground'時,方法'beaconConfigManager'停止發送響應。 – TheZozoOwner