-
Notifications
You must be signed in to change notification settings - Fork 1k
background_model
let the app run in background to perform certain Bluetooth-related tasks
if your app needs to run in background to perform certain Bluetooth-related tasks, it must declare that it supports a Core Bluetooth background execution mode in its Information property list (Info.plist) file. When your app declares this, the system wakes it up from a suspended state to allow it to handle Bluetooth-related events. This support is important for apps that interact with Bluetooth low energy devices that deliver data at regular intervals, such as a heart rate monitor.
There are two Core Bluetooth background execution modes that an app may declare—one for apps implementing the central role, and another for apps implementing the peripheral role. If your app implements both roles, it may declare that it supports both background execution modes. The Core Bluetooth background execution modes are declared by adding the UIBackgroundModes key to your Info.plist file and setting the key’s value to an array containing one of the following strings:
bluetooth-central—The app communicates with Bluetooth low energy peripherals using the Core Bluetooth framework.
bluetooth-peripheral—The app shares data using the Core Bluetooth framework.
The bluetooth-central Background Execution Mode When an app that implements the central role includes the UIBackgroundModes key with the bluetooth-central value in its Info.plist file, the Core Bluetooth framework allows your app to run in the background to perform certain Bluetooth-related tasks. While your app is in the background you can still discover and connect to peripherals, and explore and interact with peripheral data. In addition, the system wakes up your app when any of the CBCentralManagerDelegate or CBPeripheralDelegate delegate methods are invoked, allowing your app to handle important central role events, such as when a connection is established or torn down, when a peripheral sends updated characteristic values, and when a central manager’s state changes.
Although you can perform many Bluetooth-related tasks while your app is in the background, keep in mind that scanning for peripherals while your app is in the background operates differently than when your app is in the foreground. In particular, when your app is scanning for device while in the background:
The CBCentralManagerScanOptionAllowDuplicatesKey scan option key is ignored, and multiple discoveries of an advertising peripheral are coalesced into a single discovery event.
If all apps that are scanning for peripherals are in the background, the interval at which your central device scans for advertising packets increases. As a result, it may take longer to discover an advertising peripheral. These changes help minimize radio usage and improve the battery life on your iOS device.
The bluetooth-peripheral Background Execution Mode To perform certain peripheral role tasks while in the background, you must include the UIBackgroundModes key with the bluetooth-peripheral value in your app’s Info.plist file. When this key-value pair is included in the app’s Info.plist file, the system wakes up your app to process read, write, and subscription events.
In addition to allowing your app to be woken up to handle read, write, and subscription requests from connected centrals, the Core Bluetooth framework allows your app to advertise while in the background state. That said, you should be aware that advertising while your app is in the background operates differently than when your app is in the foreground. In particular, when your app is advertising while in the background:
The CBAdvertisementDataLocalNameKey advertisement key is ignored, and the local name of peripheral is not advertised.
All service UUIDs contained in the value of the CBAdvertisementDataServiceUUIDsKey advertisement key are placed in a special “overflow” area; they can be discovered only by an iOS device that is explicitly scanning for them. If all apps that are advertising are in the background, the frequency at which your peripheral device sends advertising packets may decrease.
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
//if Bluetooch not power on,show alert
[NSNumber numberWithBool:YES],CBCentralManagerOptionShowPowerAlertKey,
//centralManager restore IdentifierKey
@"babyBluetoothRestore",CBCentralManagerOptionRestoreIdentifierKey,
nil];
if ([backgroundModes containsObject:@"bluetooth-central"]) {
//background
bleManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil options:options];
}else{
//normal
bleManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil];
}