🎉 25% off Pre-Sale! Bluetooth LE course with real hardware included - no SDK required
Bluetooth® Low Energy Protocol · · 13 min read

Bluetooth ATT and GATT Explained (Connection-Oriented Communication)

A deep dive into the ATT protocol and GATT profile layers that enable connection-oriented data transfer between Bluetooth LE devices, covering attributes, services, characteristics, and profiles.

Bluetooth ATT and GATT Explained (Connection-Oriented Communication)

In our previous post, we introduced some of the layers of the Bluetooth Low Energy protocol stack. Today, we will take a closer look at the ATT (Attribute) protocol and GATT (Generic Attribute Profile), two important protocol layers that allow data transfer during a connection-oriented communication mode.

If you are looking to transfer data between connected devices using LE, understanding the technical jargon around ATT & GATT is essential! So let's get started...

Quick Primer: How Devices Connect Using The GAP Layer

For Bluetooth LE-enabled devices to transmit data between each other, they must first form a communication channel. How this channel is formed and maintained is the responsibility of the GAP (Generic Access Profile).

The Generic Access Profile instructs that for two devices to connect and communicate, one must take the role of Central, and one must take the role of Peripheral.

The Central is typically a powerful device like a smartphone, while the Peripheral is often something that requires less power, like a fitness tracker.

And the journey to establishing a connection between the two starts like this:

Step 1:

When the device that wants to assume the peripheral role is ready to connect, it must enter the advertising state and begin announcing its presence by using its LE radio to broadcast advertising packets on the three primary advertising channels: 37, 38, and 39.

Advertising Events
Broadcasting Advertisement packets on the primary channels

Note: Advertisement packets (PDUs) can also be sent on secondary advertisement channels (data channels) in Bluetooth 5 (or later), but they always start with advertising on the primary channels first. For the sake of simplicity, we'll focus on the primary advertising channels for the time being; alternatively, you can read a post we wrote about Bluetooth 5's features.

Step 2:

A device that wants to assume the Central GAP role must transition into a scanning state and listen on these three channels for an advertising packet.

Scanning Event
Listening for packets on the primary channels

When the scanning interval coincides with an advertising event, something magical happens: the central device discovers the peripheral device!

Step 3:

When an advertising packet is detected, the next step is for the Central to initiate a connection to the Peripheral by sending a Connection request packet.

Between the two GAP roles, the device that supports the central GAP role is in charge of managing the connection and has the final say in the connection parameters.

We won't delve much into these connection parameters, but some of them include:

The Central device decides on these parameters and bundles them up into a connection request packet that it sends to the peripheral. After this act, we say the connection is created.

Then, the central device will then send a data packet to the peripheral device after a time interval known as a connection interval. If the peripheral device receives the data packet and responds by sending one back to the central device, the connection is now considered to be established.

A smartwatch is establishing a connection with a
Connection creation and connection establishment

Step 4: Viola! We have a connection.

After establishing a connection, devices can have a persistent and synchronized bi-directional data transfer.

So, how exactly is this data transfer accomplished? How do devices talk to each other after they've established a connection?

This is where ATT & GATT play a vital role.

Attribute Protocol (ATT) - Roles

First, the ATT Protocol defines roles. And, yes, with each role comes specific responsibilities.

The roles defined by ATT are based on a client-server architecture, similar to the one used by the world wide web, where a device that is a provider of a resource or service is called a server, and the client is the consumer or requester of that resource or service.

Now, don't fall into the assumption that a device that supports a GAP Central role will automatically assume the Client ATT role, and the device that supports a GAP Peripheral role will automatically assume the Server ATT role. No, this isn't always the case.

The server and client ATT roles are independent of the GAP roles and can be played by any device at any time.

So, both devices in a connection can act as either a server or a client, depending on the context of the data being transferred.

To further understand this concept, let's take a look at an example from Android's Bluetooth guide: Imagine you have an Android smartphone that is connected to your fitness tracker.

When the tracker wants to report data to the phone, then it acts as the server, and the phone acts as the client. In this case, the tracker provides data (such as heart rate or steps taken) to the phone.

An example of when the phone acts as the server is for a SIG-adopted service called the Current Time Service. In this case, the phone will expose the current time information for the peripheral (e.g., tracker) to read and obtain.

Since the phone usually has an internet connection and can synchronize its clock to an accurate reference, it can easily provide this information to other devices that do not have access to the internet; the fitness tracker in this scenario.

Attribute Protocol (ATT) - Server Role and Attributes

As previously stated, the server device functions as a database to store the data that it needs to share with the client device.

For proper functionality, the server, like any other database, requires:

It is the responsibility of the ATT protocol to provide means for the server device to store data in a format that can be read and written by the client, as well as to provide mechanisms for the client to access, write, and read that data.

Attributes

The ATT protocol defines a data structure called an attribute as a standard method for effectively organizing the data stored, accessed, and updated on the server.

The attribute data structure has four fields:

Handle
UUID
Value
Permissions
Attribute data structure

On the server, data is organized as a table of attributes:

Handle
0001
Attribute
0002
Attribute
0005
Attribute
●●●
FFFE
Attribute
FFFF
Attribute
A table of Attributes in a Server's database

All attributes of an LE server are stored in its database by increasing the attribute handle value.

A successive attribute doesn't need to have the next integer handle value, and there can be gaps between handle values as long as they are in increasing order.

Attribute Access Methods

The ATT protocol also defines methods by which attributes can be read or written. The methods depend on whether it is the client or the server who initiates the attribute access procedure.

In the case where the client initiates attribute access, two operations are defined - The read and write operation.

They are pretty self-explanatory, but to recap:

Client device performing a write and read ATT access method
Write and Read ATT-defined access methods

The Read operation is used by the client to read the value of an attribute from the server. The server responds with the value of the attribute.

The Write operation is used by the client to write the value of an attribute on the server. The server responds with a status indicating if the write was successful or not.

On the other hand, in the case where the server initiates access, two operations are defined: Notification and Indication operations.

Notification and Indication ATT-defined access methods
Notification and Indication ATT-defined access methods

The Notification operation is used by the server to send an updated value of an attribute to the client every time it changes. The client does not respond to this operation.

The Indication operation is similar to the notification operation, but the client has to send back a response with an acknowledgment status indicating whether or not it has correctly received the value.

Attribute Access Permissions

Access permissions, like file permissions, determine whether a client can read or write (or both) an attribute value. Each attribute can be assigned one of the following access levels:

Regarding security requirements, the attribute types and handles are public information, but the value field and permissions are not.

As a result, in order for the client to access these two private fields, the server may require the following:

A Hierarchical Database Model

A hierarchical database model is a data model in which data is not organized sequentially but rather in a non-linear format to demonstrate the data's hierarchical relationship. This model uses a tree-like pattern to represent data.

This type of data model organizes data in a way that makes it faster to access and uses less memory.

Scaler Topics' example is arguably the best way to explain a hierarchical database model:

Assume John and Sarah are high school students.

Their teacher assigned them to provide a detailed list of the names of the Faculties in their year, as well as their departments.

John decides to create a table that includes each person's name, department, and designations.

Source: Scaler Topics

Sarah, on the other hand, decides to draw a tree diagram showing all of the faculties and their designations:

Source: Scaler Topics

Q. Who do you believe will receive a higher grade in their assessment?

Sarah! She created a hierarchical database model, which allowed her to more easily visualize the structure of the data and understand the relationships between the different elements (faculty members).

The hierarchical model is an efficient way to organize data. As you can see, it efficiently organizes data into a hierarchy. This makes it easy to visualize the structure and relationship of the data elements.

ATT Server (A.K.A database)

As previously stated, the ATT protocol stores data as attributes in a table in a linear manner by sequentially increasing the handle number of the attribute.

But what if we want to indicate a relationship between the data stored on the server? Just like Sarah did in the previous example.

And, by relationship, we mean the one similar to a file and folder relationship found in a computer disk drive, where we intuitively understand that files in the same folder are related. This would make it easy to navigate through a complex database. Wouldn't you agree?

The GATT layer is in charge of defining a hierarchical data structure that demonstrates the relationship or connection between the data stored in an ATT server.

Let's take a look at how the GATT protocol layer is used to establish this relationship.

GATT (Generic Attribute Profile)

The GATT protocol layer defines a framework in which the resources (data) on a server's database can be organized to show a hierarchical relationship.

The GATT layer defines a 4-level tree-like framework:

Let's illustrate this in the image below:

Hierarchical Relationship of data

1. Characteristic

A characteristic is a basic storage unit where the application data is "written" into and "read" from.

It is analogous to a file on a computer disc drive. We know that files are where all programs and data are written into or read from.

Every Characteristic is defined by :

Characteristic Value - The actual application data stored.

Descriptors- A descriptor is optional information that tells you more about a characteristic's value. This might include a human-readable description, an acceptable range for the characteristic's value, or a unit of measure that is specific to the characteristic's value.

A very popular descriptor is the Client Characteristic Configuration Descriptor (CCCD). CCCD is used to enable or disable indications or notifications from a GATT server. By enabling notifications or indications on a particular characteristic, the client can be notified or receive updates whenever the characteristic value changes on the server.

Let's look at a fitness tracker with a GATT Heart Rate Profile to see how the hierarchical relationship works.

According to the Heart Rate GATT profile, a device implementing it will contain:

The heart rate characteristic is defined by:

The body measurement characteristic is defined by:

The Device information characteristic is defined by:

2. Services

Services are primarily used for organization purposes rather than storage.

They are analogous to folders or drawers. Just like a folder houses related files, a service contains related characteristics.

For our fitness tracker example, its services will be:

3. Profile

On top of the hierarchy are the profiles, which contain services for a particular use case that the server supports.

Some services are optional, while others are required, but every device that conforms to a specific profile must implement all mandatory services (and characteristics). This is done to ensure that devices that support the same GATT profile can communicate with one another (interoperability).

For example, when a fitness tracker from manufacturer X claims to support the Heart Rate GATT profile. Then it's expected that :

This standardized way of organizing data has many benefits:

In our fitness tracker example, its Heart rate profile contains the following services:

Hierarchical relationship of data in a Heart Rate Profile

The GATT Profile Builds On The Functionality Of The ATT Protocol

The GATT layer lies above the ATT (Attribute Protocol) layer in the Bluetooth Low Energy protocol stack. It reuses many of the concepts and functionality of the ATT layer.

ATT and GATT in the Bluetooth protocol stack

1. GATT Reuses The ATT Client-server Architecture:

GATT layer uses the ATT client-server architecture. In this architecture, devices play either the role of a GATT client or a GATT server.

In general, communication between the GATT client and the GATT server begins with the GATT client performing a service and characteristic discovery to find out: what services are offered by the GATT server and what characteristics are associated with each service.

GATT Client and GATT server
Service discovery is always before Attribute access methods

After the discovery process is complete, the GATT client can then read or write the values of certain characteristics that it is interested in or configure notifications to be sent by the GATT server whenever the value of characteristic changes.

2. GATT Reuses The ATT Attribute Data Structure

GATT uses the Attribute data structure to store services and characteristics.

Attribute's table
GATT Attributes stored in the Server's Attribute Table

Each service and characteristic, therefore, have a:

3. GATT Uses ATT PDUs

The GATT layer uses ATT requests and responses to:

Attribute Protocol PDU
Attribute Protocol PDU

Summary

In this post, we took a closer look at the ATT and GATT Protocol layers to help us develop applications that take advantage of connection-oriented communication.

Simply put:

💡
Insider Tip: Want to take your Bluetooth Low Energy knowledge to the next level? The Bluetooth Developer Academy gives you access to comprehensive courses covering ATT, GATT, and much more with hands-on implementation guides.

Read next