What is Bluetooth Low Energy (BLE)?
Bluetooth low energy (BLE) is also referred to as Bluetooth Smart and it is a lightweight subset of classic Bluetooth.
It was introduced in 2010 to enable the growth of low-power applications under the umbrella of IoT with a specific target being the devices that are run on tiny sources of power like coin size batteries.
It allows you to send short bursts of data with connection intervals spread as far apart as possible to save battery life.
Even though Bluetooth low energy (BLE) is relatively young, it has been rapidly adopted by major mobile industry giants. Hence, a product developed with BLE configuration will only result in wider acceptability in the market.
A protocol is a universally agreed way to communicate between two devices and a protocol stack is nothing but a set of protocols that work together to transmit information from one Bluetooth low energy (BLE) device to another.
Now each layer in the stack has the illusion that it is communicating with the equivalent layer on the other BLE device but in fact, it is communicating with the layers above it and below it.
Bluetooth technology was named after a Danish king, king Herald Blatant or its Bluetooth in English. He was known for unifying the warring factions of what is present-day Denmark, Sweden, and Norway.
Interestingly uniting devices from different manufacturers with different purposes is what Bluetooth technology all about.
In the mid-1990s numerous corporations were developing competing non-compatible standards.
This growing fragmentation was obviously an impediment to the widespread adoption of wireless technology. Thus, Bluetooth an industry-wide standard was developed for low power and short-range radio connectivity.
The logo of Bluetooth is a combination of the letters H and B which are the initials of king herald Bluetooth.
BLE Protocol Stack | BLE Architecture
LE Physical Layer (PHY)
The first layer of the stack is the physical layer. Now, as the name says it contains the analog communications circuitry.
It is concerned with the actual transfer of data over air via radio.
Bluetooth low energy (BLE) operates in 2.4GHz ISM which is an industrial scientific and medical band.
It is a license-free band that is used for short-range applications. All versions of BLE modulate the 2.4GHz carrier using the GFSK modulation technique.
Now, out of the total 40 channels, each with a bandwidth of 2MHz three is used by BLE for advertising, and the rest 37 are used as data channels.
But, what is an advertisement?
Well, it is when one BLE device say peripheral is shouting about its presence on those three channels that we just discussed and if say any other device say central is interested in an exchange of data then it will be exchanged on those 37 channels using something called as frequency hop mechanism and this mechanism helps in avoiding interference with other signals such as wi-fi which are on the same ISM band that is 2.4 GHz.
Link Layer (LL)
The link-layer unlike the physical layer this one is usually implemented as a combination of hardware and software.
It is responsible for advertising scanning and for creating and maintaining connections.
It provides the first level of control and data structure over the raw radio operations so here you will get to see a packet structure.
Now as an application developer you don’t need to understand the details of this layer.
Together the link and physical layer form the controller layer.
The layers that we will discuss now will be host layers and the host is concerned with simply pouring the data in and pulling it out.
But, the controller layer controls the radio and maps the connection requests from the higher layers onto the physical time slots on RF.
In simple word, the host is interested in say throwing a party which is exchanging the data and controller does the dirty job of actually moving it.
Host Controller Interface (HCI)
Then, there is a layer that comes in between our host and controller layers is the HCI or host controller interface. Now, the host doesn’t get to directly change or edit the settings in the controller. Thus, there is a mediation layer known as HCI. HCI allows interoperability between the host and controller.
Logical Link Control And Adaptation Protocol (L2CAP)
In host is the L2CAP, L2CAP stands for logical link control and adaptation protocol. It provides data encapsulation services to the upper layers.
This is the layer that is responsible for data integrity. Thus, in case a packet does not reach the destination then this layer ensures re-transmission.
Now re-transmission is true only for data that is transferred post an establishment of a connection between two BLE devices.
Well, there is no need to re-transmit an advertisement packet. So, this layer deals with connection-oriented data.
Security Manager Protocol (SMP)
The security manager protocol layer provides services to other state layers for two things, a secure connection and a secure exchange of data between two BLE devices. So, it’s got everything to do with security.
Generic Access Profile (GAP)
Generic access profile and as implied by its name it provides access to the link-layer operations.
This includes defining the role of a Bluetooth low energy (BLE) device, managing advertisements, connection establishment, and security.
Basically, everything to do with how Bluetooth low energy (BLE) devices form connections with each other.
Profile: Profile defines the usage of the protocol in a particular way in order to achieve a particular goal and the goal, in this case, would be forming a connection between two BLE devices.
Now, this GAP defines strict roles for the devices and these roles provide the specific requirements for the underlying controller.
Now, roles allow devices to either transmit data or receive data or do both.
Thus, there are four roles possible:
The first one being the broadcaster, the broadcaster keeps on broadcasting its data as simple as that it keeps on sending advertising events.
An example of that would be a BLE thermometer, which does periodic announcements of its identity and keeps on transmitting ambient temperature-related data.
The next is observer, the observer role is complementary or opposite of the broadcaster, a gap observer is a Bluetooth low energy (BLE) device that constantly scans for nearby advertising devices.
Thus, it can read the advertisement and scan the response packets from these devices but it cannot establish further connections that mean it can simply observe the data.
Next comes central, it listens for advertisements but unlike an observer, it can interact with any station it can initiate and start the connection process and thus is known as the master it can connect with multiple peripherals.
The last role is peripheral, peripheral is the one that advertises, and once connected with central it keeps on exchanging the data only with that particular central.
It can still advertise but cannot make connections to any other stations until the session with that particular central is concluded.
Now, let’s have a quick look at how the connections are established between the central and the peripheral.
Peripheral keeps on sending its advertisement say every 20 milliseconds. The developer can decide how many times advertisement messages should be sent along with its content.
If the center wants to get the data from the peripheral it sends something called a connect indication message on the advertisement channel.
Where it gives its frequency hop sequence and the channel that the peripheral needs to tune to. Then, a connection is formed where the peripheral tunes its receiver to the specified channel and starts listening.
So, these are the few things that you should know about GAP.
Generic Attribute Profile (GATT)
Next, we have something called GATT. Now, GATT stands for generic attribute profile just the way GAP is our access layer GATT is concerned with data exchange in BLE thus it is the topmost data layer of BLE.
Now, without a connection, it is not possible to obviously have a bi-directional data transfer between two BLE devices, which means that the devices have already gone through the advertising process, which is governed by GAP.
Attributes Protocol (ATT)
Now, GATT uses ATT protocol for the transfer of data ATT stands for attributes protocol ATT is used to store something called services, characteristics, and related data which are called attributes.
in a simple lookup table using 16-bit ids for each entry we’ll quickly understand what services and characteristics are but right now just hang on so now let’s take a look at the roles within get so the peripheral is known as the get server and it holds the ATT lookup data and definitions of services and characteristics.
The GATT client sends requests to the server to access its data note that a BLE device can act as a server and client at the same time.
Now, let’s look at the three most frequently used terms in Bluetooth low energy (BLE):
So, the profile doesn’t actually exist on the BLE peripheral itself. It is simply a predefined collection of services that allows us to fulfill a particular goal.
Service is a collection of chunks of data called characteristics. Each service has something called a UUID or universally unique identifier which distinguishes itself from other services.
So, UUID is basically like this unique label that allows you to access a particular service. The UUID can be 16-bit for an officially adopted BLE service or 128-bits long for custom services.
Now, what do we mean by predefined or officially adopted profile or services? so, these profiles or services are pre-defined by Bluetooth special interest groups. Thus, we can readily use them.
If what we desire is not on this list then we can create our own custom profile and services as well.
So, that brings us to the last or the lowest level concept of GATT which is characteristics. So, as I said they are the lowest level concept in GATT transactions which encapsulates a single data point or value it can also hold an array of related data such as x y z values of a three-axis accelerometer.
Similar, to services characteristics, have a predefined 16-bit UUID or custom 128-bit.
let’s take a look at a standard heart rate profile to understand the GATT concepts that we just discussed in a better way so there is a GATT client which is accessing the heart rate profile on the GATT server.
The profile consists of two services which are heart rate service and device info service respectively the device and for service further contains five characteristics that give information about the device and the heart rate service contains heart rate measurement characteristic and body sensor location characteristic.
There are six types of operations associated with characteristics:
The first one being commands which are sent by the client to the server and do not require a response example can be a write command.
Then comes requests these are sent by the client to the server and they do require a response.
which brings us to responses that are obviously sent by the server in response to a request.
Next are notifications that are sent by the server to the client to let it know of a change in a particular characteristic value example could be temperature. Temperature can be the characteristic of interest if the data of that change in server.
Then, it will notify the client of the changed value next is an indication sent by the server to the client similar to notification but here the client sends an acknowledgment to the server confirming that the indication was successfully received.
The last operation which is confirmations as just discussed these are sent by the client to the server and are nothing but acknowledgment packets sent back to the server to let it know that the client received an indication successfully.
Finally, comes the application layer the user application sits here and directly interacts with the BLE stack.