Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS BLE background scanning

I'm having some trouble understanding how scanning is performed when an iOS application is in bacground. I have a very simple test application which just scans for devices and outputs the results to the console. I've added bluetooth-central to required background modes in Info.plist so I should be fine and I'm scanning for device with one specified service, that is

NSArray *cbuuidArray = [NSArray arrayWithObjects:[CBUUID UUIDWithString:@"UUIDFromUUIDGEN"],nil];
[self.centralManager scanForPeripheralsWithServices:cbuuidArray options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];

When the application is in foreground I'm having only one (or none) BLE device connected to the power source so iOS detects it and shows the results quite frequently. When I go to background there are no more results of the first device which is expected since CBCentralManagerScanOptionAllowDuplicatesKey is set to NO.

At this point I'm powering the second BLE device and eagerly wait for it to show in the results. After 10 minutes of waiting nothing shows up. The application is not terminated since my last notification comes from applicationDidEnterBackground and applicationWillTerminate was never called while I was working on the task.

In a quite accidental way I discovered that if my app is running and is still scanning in the background and another BLE scanning application (I'm using the excelent BLExplr) is in the foreground and starts scanning, my application is finaly receiving results in the same time as the foreground appliaction. This makes some sense since the advertising packets are handled by the system and dispatched to applications but why doesn't my application receive anything on it's own?

Did anyone have similar experience or knows what can this be caused by? I've read probably all Apple resources regarding backgrounding and bluetooth with no hints regarding this issue. I'm working on iOS 4s with iOS 5.1.1. My main ViewController which is a CBCentralManagerDelegate delegate 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;
}

#pragma mark -
#pragma mark CBCentralManagerDelegate methods

- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
    if( central.state == CBCentralManagerStatePoweredOn ){
        NSArray *cbuuidArray = [NSArray arrayWithObjects:
                                [CBUUID UUIDWithString:@"UUID"],
                                nil];
        [self.centralManager scanForPeripheralsWithServices:cbuuidArray options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
    }
}


- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{

    NSLog(@"%s / peripheral: %@, adData: %@, RSSI: %@" , __PRETTY_FUNCTION__ , peripheral, advertisementData, RSSI);
    NSLog(@"Periphal name: %@", peripheral.name);

}


#pragma mark -
#pragma mark CBPeripheralDelegate methods



@end

There's nothing more going on in the application besides a NavigationController initialization in application delegate.

like image 650
P_B Avatar asked Aug 16 '13 08:08

P_B


1 Answers

In ios side if you trying to connect or scanning ble device in Background mode it's never working. but once your device connected with ble in foreground you can continue fetching data from Ble device in Background mode.

CoreBluetooth apps in the background cannot scan for peripherals like they do in the foreground.

Please check Background scan ble device

like image 128
VijayChaudhay Avatar answered Nov 16 '22 12:11

VijayChaudhay