我想在后台搜索RSSI。我正在尝试通过在同一函数中调用该函数来完成此操作。有没有办法让这个循环处于后台模式。在我看来,它应该生活在背景中,但我在几个毫秒后就死了。如何让它保持搜索?
- (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];
}发布于 2016-03-01 02:21:01
您可以将后台时间延长到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];
}
});
}https://stackoverflow.com/questions/35706497
复制相似问题