Welcome to visit my blog muhlenXi, this article from my blog, welcome to reprint, reprint please indicate the source: muhlenxi.com/2017/05/02/… .

Guide language:

Imperceptible in iOS low-power bluetooth development has a very long time, haven’t got the time to and put himself about this aspect of the study and write it down, recently finished project iteration update online, a little spare time, take the time to summarize, says this is with phase of communication between friends, is for later friend drop pit experience. After all, time is made! Technical people generally have a very open mind!

We now know that many smart hardware devices already have low-power Bluetooth modules integrated so that we can develop an iOS or Mac APP to interact with them. Since macOS 10.9 and iOS 6, Low-power Bluetooth has been available on Macs and iOS devices. CoreBluetooth is a framework that allows you to interact with various underlying Bluetooth protocol stacks, such as GATT, ATT, and L2CAP.

The interaction with the underlying layer is shown below:

Before we begin, we need to understand a few concepts. For those who are not familiar with Bluetooth, check out wikipedia’s introduction to Bluetooth.

Bluetooth 4.0: Bluetooth 4.0 is a new specification introduced by Bluetooth SIG on July 7, 2010. Its most important features are low power consumption and power saving!

BLE: Bluetooth Low Energy Wireless technology.

A brief introduction to CoreBluetooth

What is BLE?

BLE is a detailed description of Bluetooth 4.0, which defines a set of protocols for communication between low-power devices. CoreBluetooth is an abstraction of the BLE protocol stack. That said, it hides a lot of the underlying implementation details, which makes it easy for us developers to develop an APP that interacts with BLE devices.

Two Central and Peripheral roles

The two most critical roles in CoreBluetooth are Central and Peripheral, where a Peripheral is the one that provides the data, and Central gets the data provided on a Peripheral to perform a specific task. For example, a digital room temperature meter with BLE integration may provide real-time temperature in a room, which can be read, analyzed and displayed through an APP.

So on the Peripheral it broadcasts data into the air so that we can perceive it. Central finds the Peripheral that is broadcasting data around it by scanning and searching. After finding the specified Peripheral, it sends a connection request for connection. After the connection is successful, it interacts with the Peripheral with some data. So the Peripheral will respond to the Central in a proper way.

CoreBluetooth simplifies common Bluetooth tasks

CoreBluetooth simplifies common Bluetooth tasks, making it easier to integrate BLE functionality in your App. If you develop an App that follows Centrals’ development specifications, CoreBluetooth will take care of scanning, connecting, and interacting with Peripheral data, and it’s also easy to set your device to local Peripheral via CoreBluetooth.

The status of the iOS APP affects bluetooth behavior

The status of your iOS APP can also affect Bluetooth behavior. Bluetooth behavior will be affected when your APP is running in the background or in a suspended state. By default, your APP cannot communicate with BLE when it is running in background or suspended. That is, when your APP is running in background, you need to communicate with BLE. You need to declare that your APP supports Bluetooth background mode. Even if you declare support for background mode, Bluetooth will handle data differently in background mode, and you need to be aware of these differences when developing your BLE APP.

Even when the APP is running in the background, it can kill APP background processes when the system memory is low, and for iOS 7, CoreBluetooth supports saving and restoring state information on both Central and Peripheral parts. This feature enables long-term interaction with BLE devices.

Improve the user experience in the right way

The CoreBluetooth framework provides an interface for your APP to interact with many common BLE devices, which will improve the user experience with proper use and practice.

For example, when you implement Central or Peripheral functions, you will use the Radio carried by the device to broadcast signals to the air, which will affect the battery life, so when you design the APP, you need to reduce the frequency of Radio as much as possible.

In-depth CoreBluetooth

Important note: After iOS 10, when communicating data with BLE devices via CoreBluetooth, Must be in the project Info. The file contains about NSBluetoothPerpheralUsageDescription description, otherwise it will lead to APP flash back, see NSBluetoothPerpheralUsageDescription.

Central and Peripheral means of communication

There are two main roles in BLE communication: Central and Peripheral are based on traditional client-server architecture. Peripheral usually provides data needed by other devices. Central usually uses information obtained through Peripheral to complete a specific task, as shown in the figure. The heart rate monitor feeds data to a Mac or iOS APP that then displays the user’s heart rate.

Central searches for and connects to the Peripheral that is broadcasting data

Peripheral broadcasts the data in the service in the form of a broadcast packet, a broadcast packet is a smaller packet that contains Peripheral useful information, such as Peripheral name and main function data. For example, a digital room temperature meter may broadcast data that includes the current room temperature, and for BLE, broadcast is the primary way to indicate their presence.

As shown in the figure, for a Central, it can search and retrieve the Peripheral broadcast information it wants.

How does Peripheral organize data

The purpose of connecting Peripheral is to interact with the data provided by Peripheral. After you understand this, you can better understand the Peripheral data structure.

Peripheral contains one or more services and useful information about the strength of the connected signal. A Service can be thought of as a collection of data that performs a specified function. For example, a heart rate monitoring service might be able to read heart rate data from a heart rate sensor.

So services are made up of Characteristic, which provides more detailed information on Peripheral services, for example, Heart rate service may include Characteristic to measure heart rate data of different positions and Characteristic to transmit heart rate data. The following figure shows the data composition structure of a heart rate monitoring device.

Central interacts with Peripheral data

After Central establishes a successful connection with Peripheral, Central can find the whole series of services and Characteristic provided by Peripheral, and the data in broadcast packet is only a small part of the available services.

Central can interact with services by reading or writing values of Service Characteristic. Your APP may need to get the current room temperature from the digital room temperature meter or set a temperature value to the digital room temperature meter.

How do I represent Central, Peripheral, and Peripheral data

The main roles and data processing involved in BLE communication are simply integrated into the CoreBluetooth framework.

Objects from the Central aspect

When you interact with a Peripheral via a local Central, all you need to do is call the Central method, unless you set up a local Peripheral and use it to respond to some other Central interaction request, and in practice, Most of your Bluetooth processing will be on the Central side.

Local Central and Remote Peripheral

On the Central side, a Local Central device is represented by a CBCentralManager object that is used to manage Remote Peripheral devices (represented by a CBPeripheral object), This includes searching and connecting Peripheral data that is being broadcast. This is how Local Central and Remote Peripheral are represented in the CoreBluetooth framework.

CBService and CBCharacteristic objects are used to represent service data in Peripheral

When you interact with Remote Peripheral data, you deal with its services and Characteristic, and in the CoreBluetooth framework, the CBService object represents the Peripheral services, and likewise, The CBCharacteristic object is used to represent the characteristics in a Service. The following figure shows the Remote Peripheral service feature structure tree.

Peripheral object

For macOS 10.9 and iOS 6, Mac and iOS devices can implement BLE Peripheral functions, such as providing data to other devices (including Mac, iPhone, and iPad). The Peripheral aspects of BLE communication can be invoked when you follow the Peripheral development specification.

Local Peripheral and Remote Central

On Peripheral, a Local Peripheral can be represented by the CBPeripheralManager object, which is used to manage publishing the included service, including organizing the construction of Peripheral data structure and broadcasting data to the central device. The Peripheral Manager also responds to Remote Central’s read/write interaction requests. What you see here is a Local Peripheral and Remote Central.

CBMutableService and CBMutableCharacteristic objects represent data on LocalPeripheral

When you set up and interact with Local Peripheral data, you’re dealing with its variable services and Characteristic, and in the CoreBluetooth framework, CBMutableService objects are used to represent services in Local Peripheral services, and likewise CBMutableCharacteristic objects are used to represent features in Local Peripheral services. The following figure represents a service feature structure tree in Local Peripheral.

Further information on BLE development will be provided in subsequent chapters.

reference

1, TP40013257 – CH1 – SW1

2, CoreBluetoothOverview

conclusion

Leave a comment below to share your thoughts…