Original translation: IOS app Bluetooth background run

Source: Internet
Author: User

By default, most of the normal Corebluetooth tasks, whether central or peripheral, are not available in the background or in the suspended state. That is, you can perform certain Bluetooth related events by announcing that your application supports background processing mode to allow one of your applications to wake you up from a pending state.

Even if your application supports one or both of the corebluetooth background execution modes, he will not run forever. At some point, the system may terminate your application to free memory due to the current foreground application, which will cause any active or pending connections to be lost. In IOS7, Corebluetooth supports storing state information for central and peripheral and recovering these status information when the program starts. You can use this technique to support long-term operation of Bluetooth devices.

      • Foreground-only Apps

Like most iOS apps, if you don't have permission to perform background tasks, the app goes into a pending state immediately after it enters background mode. Your app will not be able to perform Bluetooth-related tasks While it is in the suspended state. He will not be aware of and respond to Bluetooth-related events until it recovers into the foreground mode

In the central side, there is no one that claims to support any kind of Bluetooth backend mode. When he is in the background, or when he is in the suspended state, it cannot scan and discover the task of the peripheral device being broadcast. On the peripheral side, the broadcast will stop and any central attempt to access the dynamic characteristic data of the app's published service will receive an error.

Depending on the use case, these default behaviors will affect your app in every way. For example, imagine that you are interacting with the data on the peripheral you just connected to. Assuming your app is in a pending state (possibly because the user switched to another app), if the connection to peripheral is lost when your app hangs, you won't be aware of any disconnected events when you return to the foreground mode.

      • Take advantage of peripheral Connection Options

All Bluetooth related events that occur when the app is in the suspended state are placed in the queue and delivered to the app when the app returns to the foreground. In other words, when a central event occurs, the Core Bluetooth provides a way to alert the user. Users can use these warnings to decide whether to bring the application back to the foreground.

You can use these alerts to pass Cbcentralmanager the following parameters when calling the Connectionperipheral:options method to connect Cbperipheral:

        • Cbconnectionperipheraloptionnotifyonconnectionkey---When you want your system to be in the suspended state, just one connection is executed successfully and the system throws a connection warning
        • Cbconnectionperipheraloptionnotifyondisconnectkey---When you want your system to throw a disconnect warning when you receive any disconnected events in your app's pending state
        • Cbconnectionperipheraloptionnotifyonnotifycationkey---When you want your system to be in the suspended state of your app, any notification you receive will burst with a warning.
      • Core Bluetooth Background Execution Modes

If your app needs to run some Bluetooth related tasks in the background, then it must announce that he supports a Bluetooth background execution mode in its info.plist file. When your app claims this, the system will wake it up from the suspended state to handle Bluetooth-related events. This support is important for apps that interact with BLE devices at regular intervals, such as a heart rate monitor.

One app can support the Bluetooth background running mode Two, one for the implementation of the central role of the app, the other is to implement the peripheral role of Appl. If both of your apps are implemented, it can also announce that it supports two Bluetooth background execution modes. The Bluetooth background mode can be set to an array containing the following strings by adding Uibackgroundmodes key in Info.plist:

        • Bluetooth-central,
        • Bluetooth-peripheral
      • The bluetooth-central Background execution Mode

This mode allows your app to perform some Bluetooth related work in the background. In the background you can also scan connections to peripheral, discover and interact with peripheral data. In addition, the system will wake up your app when any cbcenralmanagerdelegate and Cbperipheraldelegate methods are called.

However, it is worth noting that scan is not the same in both foreground and background modes. Scan in background mode:

        • Cbcenralmanagerscanoptionallowduplicatskey will be found two times by exchange rate, a advertsing peripheral, so it will only merge into one discovery event.
        • If all the programs that scan peripheral are running in the background, the time interval for discovering peripheral will increase.
      • The bluetooth-peripheral Background execution Mode

This mode will wake up your app to handle the Read,write,subscription event. The background mode also allows your app to broadcast. But background broadcasts are different from the foreground:

        • Cbadvertisementdatalocalnamekey will be ignored, and peripheral's local name will not be broadcast
        • All services UUIDs included in the Cbadvertismentdataserviceuuidskey will be placed in the overflow "area; they are only discovered by those who scan their devices explicitly.
        • If all broadcast apps are in the background, the frequency of sending broadcasts will be reduced
      • Use Background execution Modes wisely

Background mode is very useful, but please use caution, because the background mode to often use the radio, which is very electricity. There are a few specifications to follow:

        • The app should be session-based and provide the interface to the user, allowing them to control the switching of Bluetooth related events
        • The app is awakened with 10 seconds to handle the event, ensuring that it is as fast as possible to switch to the suspended state again. Spending too much time in the background will be rejected or killed by the system.
        • The app does not do anything unrelated to waking up after being awakened

Original translation: IOS app Bluetooth background run

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.