Old notes on Core Bluetooth

Long story short

Core Bluetooth:

  • Only awakes you from a suspended state (but won’t show any UI).
  • Only 10 seconds for handling the event.
  • Needs background modes.

iBeacon:

  • Can awake a hard-closed app due to a region enter event (but won’t show any UI).
  • Only 10 seconds for handling the event.
  • Needs background modes and location permission.

You may get 30 extra seconds by receiving a silent remote notification. But they are not 100% reliable (http://stackoverflow.com/questions/26959472/silent-push-notifications-only-delivered-if-device-is-charging-and-or-app-is-for)…

For initiating an interaction with the user, you can only use notifications (local or remote). Note that once presented, you cannot modify the notification text.

A Today Extension may provide more chances to run application code. But they can only run when the user opens the Notification Center.

Core Bluetooth

Foreground-Only Apps

Foreground-only apps—apps that have not declared to support either of the Core Bluetooth background execution modes—cannot scan for and discover advertising peripherals while in the background or while suspended.

Core Bluetooth provides a way to alert the user when certain central role events occur. The user can then use these alerts to decide whether a particular event warrants bringing the app back to the foreground.

You can take advantage of these alerts by including one of the following peripheral connection options when calling the connectPeripheral:options: method of the CBCentralManager class to connect to a remote peripheral:

  • CBConnectPeripheralOptionNotifyOnConnectionKey—Include this key if you want the system to display an alert for a given peripheral if the app is suspended when a successful connection is made.
  • CBConnectPeripheralOptionNotifyOnDisconnectionKey—Include this key if you want the system to display a disconnection alert for a given peripheral if the app is suspended at the time of the disconnection.
  • CBConnectPeripheralOptionNotifyOnNotificationKey—Include this key if you want the system to display an alert for all notifications received from a given peripheral if the app is suspended at the time.

Core Bluetooth Background Execution Modes

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.

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

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.

Scanning for peripherals while your app is in the background operates differently than when your app is in the foreground:

  • The CBCentralManagerScanOptionAllowDuplicatesKey scan option key is ignored, and multiple discoveries of an advertising peripheral are coalesced into a single discovery event.
  • It may take longer to discover an advertising peripheral.

The bluetooth-peripheral Background Execution Mode

You must include the UIBackgroundModes key with the bluetooth-peripheral value in your app’s Info.plist file.

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.

Advertising while your app is in the background operates differently than when your app is in the foreground:

  • 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.
  • The frequency at which your peripheral device sends advertising packets may decrease.

Use Background Execution Modes Wisely

  • Apps should be session based (provide an interface that allows the user to start and stop the delivery of Bluetooth-related events).

  • An app has around 10 seconds to complete a task.

  • Apps should not use being woken up as an opportunity to perform extraneous tasks that are unrelated to why the app was woken up.

  • If the Bluetooth device becomes out of range, the app can simply call the connectPeripheral:options: method of the CBCentralManager class, and because connection requests do not time out, the iOS device will reconnect when the device is in range again.

iBeacon

iOS 7.1.1 supports awaking closed apps when the device enters a region: http://beekn.net/2014/03/apple-ios-7-1-launches-major-ibeacon-improvement/.

I couldn’t find the official source, but also stated here: http://stackoverflow.com/questions/19932090/how-to-wake-up-ios-app-with-bluetooth-signal-ble.

See https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/RegionMonitoring/RegionMonitoring.html and http://beekn.net/2013/11/ibeacon-tutorial-dealing-with-errors/.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s