I'm having trouble understanding how scanning is performed when an iOS application is in the bacground. I have a very simple test application that simply scans devices and displays the results to the console. I added bluetooth-central to the required background modes in Info.plist, so I have to be fine, and I scan the device with one specified service, i.e.
NSArray *cbuuidArray = [NSArray arrayWithObjects:[CBUUID UUIDWithString:@"UUIDFromUUIDGEN"],nil]; [self.centralManager scanForPeripheralsWithServices:cbuuidArray options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
When the application is in the foreground, I only have one (or none) BLE device connected to a power source, so iOS detects it and shows the results quite often. When I step into the background, there are no longer the results of the first device that is expected, since CBCentralManagerScanOptionAllowDuplicatesKey set to NO.
At this point, I am launching the second BLE device and am looking forward to displaying it in the results. After 10 minutes of waiting nothing appears. The application is not completed, since my last notification comes from applicationDidEnterBackground and applicationWillTerminate never called while working on the task.
In a completely random way, I found that if my application is running and still scanning in the background, and another BLE scanning application (I use excelent BLExplr ) is in the foreground and starts scanning, my application finally receives the results at the same time as foreground application. This makes sense since the ad packages are processed by the system and sent to the applications, but why does my application receive nothing from it?
Did anyone have a similar experience or did he know that this could be caused? I probably read all of Apple's resources regarding background and bluetooth without any hint of this problem. I am working on iOS 4 with iOS 5.1.1. My main ViewController , which is a delegate of CBCentralManagerDelegate , looks like this.
@implementation MainViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; } return self; }
Nothing else happens in the application, except for the initialization of the NavigationController in the deletion of the application.
ios background bluetooth-lowenergy network-scan
mog
source share