BLE Scan on Background issue

Some problems about BLE scanning on background below:

1. When BLE scan on foreground, it will scan continuously (scan interval about 1-2 second) as below:

strAdvertisementDataLocalName= F_H06LOCKERFFFF

strAdvertisementDataLocalName= F_H06LOCKERFFFF

strAdvertisementDataLocalName= F_H06LOCKERFFFF

But when BLE scan on background, it will scan discontinuously (sometimes max scan interval above 15 second) as below:

strAdvertisementDataLocalName= F_H06LOCKERFFFF

bleScanForBackground

bleScanForBackground

strAdvertisementDataLocalName= F_H06LOCKERFFFF

bleScanForBackground

...

bleScanForBackground

strAdvertisementDataLocalName= F_H06LOCKERFFFF

...

Is this the limitation of iOS on background for BLE scanning? How do I deal with this issue (the scan interval is the same with foreground)?

2. When BLE device is advertising, iOS and android both can get the advertisement string "F_H06LOCKERFFFF"; but when BLE device changes the advertisement string as "F_H127CEC79FEA805LLLL", android can scan the changed advertisement string"F_H127CEC79FEA805LLLL", but iOS scan still the old advertisement string"F_H06LOCKERFFFF".

How do I deal with this issue? This will let iOS BLE scanning the new advertisement string"F_H127CEC79FEA805LLLL" on background?


Thank you for your reply!


// ---------------------------------------------------------------------------------------------------------------------------------------

AppDelegate.m

//

// AppDelegate.m

// Locker_BLE_F1

//


#import "AppDelegate.h"


@interface AppDelegate ()

{

MainViewController *MainVC;

}

@end


@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

NSLog (@"AppDelegate(didFinishLaunchingWithOptions)");

MainVC = (MainViewController *)((AppDelegate *)[[UIApplication sharedApplication] delegate]).window.rootViewController;

return YES;

}


- (void)applicationDidEnterBackground:(UIApplication *)application {

NSLog (@"applicationDidEnterBackground");

[MainVC ExecuteBackgroundAction];

[self backgroundHandler];

}


- (void)backgroundHandler {

UIApplication *application = [UIApplication sharedApplication];

__block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^{

NSLog(@"backgroundHandler1");

dispatch_async(dispatch_get_main_queue(),^{

if( bgTask != UIBackgroundTaskInvalid) {

bgTask = UIBackgroundTaskInvalid;

}

});

}];


// Start the long-running task

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

while ([Common getOpMode] == OP_MODE_BACKGROUND) {

[MainVC bleScanForBackground];

[NSThread sleepForTimeInterval:1.0f];

}

});

}


- (void)applicationWillEnterForeground:(UIApplication *)application {

NSLog (@"applicationWillEnterForeground");

[MainVC ExecuteForegroundAction];

}

@end


MainViewController.m

//

// MainViewController.m

// Locker_BLE_F1

//

#import "MainViewController.h"


@interface MainViewController () {

}

@end


@implementation MainViewController


#pragma mark - View Controller

- (void)viewDidLoad {

[super viewDidLoad];

self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

}


- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}


- (void) viewDidAppear:(BOOL)animated {

[super viewDidAppear:animated];

}


#pragma mark - BlueTooth (BLE, Central)

// method 1

- (void) centralManagerDidUpdateState:(CBCentralManager *)central {

if (central.state != CBManagerStatePoweredOn) // CBCentralManagerStatePoweredOn)

{

NSLog (@"Please open the Bluetooth from System Setting");

return;

}

[self setBLEStatusWithLog:SCANNING];

NSDictionary* scanOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];

[self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:SERVICE_UUID]] options:scanOptions];

}


// method 2

- (void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI

{

[self.centralManager stopScan];

NSDictionary* scanOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];

[self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:SERVICE_UUID]] options:scanOptions];

NSString *strAdvertisementDataLocalName, *strDeviceName ;

strAdvertisementDataLocalName = [advertisementData objectForKey:CBAdvertisementDataLocalNameKey];

if (strAdvertisementDataLocalName == nil) { return; }

strDeviceName = [strAdvertisementDataLocalName substringWithRange:NSMakeRange(0, REMOTE_DEVICE_NAME.length)];

if (![strDeviceName isEqualToString:REMOTE_DEVICE_NAME]) return;

NSLog(@"strAdvertisementDataLocalName= %@", strAdvertisementDataLocalName);


self.connectPeripheral = peripheral;

self.connectPeripheral.delegate = self;

...

}


#pragma mark - General Function

- (void) bleScanForBackground {

[self updateLog:@"bleScanForBackground " LogType:FLOW_LOG];

NSDictionary* scanOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];

[self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:SERVICE_UUID]] options:scanOptions];

}


- (void) ExecuteBackgroundAction {

[self changeOpMode:OP_MODE_BACKGROUND];

if (self.isConnected == true) {

[self bleDisconnect:self.centralManager Peripheral:self.connectPeripheral];

}

}


- (void) ExecuteForegroundAction {

}

@end

Sorry!

Please reply to another post with the same title if you can reply my problem.


Thank you very much for your kindly reply.

BLE Scan on Background issue
 
 
Q