Learn / Communication / Wi-Fi and Bluetooth

Bluetooth Low Energy: GATT Services

ReviewedAdvanced ProAbout 6 min
NRF24
In short

Bluetooth Low Energy: GATT Services: the short answer

Bluetooth Low Energy (BLE) is built around the Generic Attribute Profile (GATT). A peripheral, such as an ESP32 sensor, exposes services, and each service holds characteristics: small values identified by UUIDs that a central (usually a phone) can read, write or subscribe to.

Level
Advanced
Time
About 6 min
Topic
Communication
You will learn to

Design a BLE peripheral with services and characteristics, use notifications, and balance range, speed and power.

Before this: Wi-Fi, Bluetooth & Radio Constraints, Connecting an ESP32 to Wi-Fi

Bluetooth Low Energy (BLE) is built around the Generic Attribute Profile (GATT). A peripheral, such as an ESP32 sensor, exposes services, and each service holds characteristics: small values identified by UUIDs that a central (usually a phone) can read, write or subscribe to. Standard services have short 16-bit UUIDs, for example Battery Service 0x180F with the Battery Level characteristic 0x2A19; your own services use random 128-bit UUIDs. Design the data model first: one service per function, one characteristic per value or command.

Before connecting, the peripheral advertises: it broadcasts a short packet with its name and service UUIDs every 20 ms to 10 s. Faster advertising is found quicker but uses more power. After connecting, the connection interval (7.5 ms to 4 s) sets how often the two sides exchange packets. Use notifications for changing values: the central subscribes by writing to the characteristic's descriptor (CCCD, 0x2902), and the peripheral pushes updates instead of the phone polling. For larger data, request a bigger MTU (up to 247 or more bytes) and the 2M PHY on supported devices.

Security and power need planning. By default anyone nearby can connect and read; use pairing with bonding and require encryption for characteristics that control things. Data rates in practice are tens to a few hundred kilobytes per second at best, so BLE suits sensor values and commands, not audio or images. Range indoors is typically 10 to 30 m. On the ESP32, the NimBLE stack uses much less memory than the older Bluedroid stack. Test with a generic BLE scanner app such as nRF Connect before writing your own phone app.

Services and characteristics

A BLE device organises its data as services, each containing characteristics, and each characteristic has a value, a type and properties such as read, write, notify and indicate. A standard service, such as the battery service, uses a number defined by the Bluetooth organisation so that any app understands it. A custom service gets a random 128-bit UUID of your own.

Notify lets the device push a new value to the phone without being asked, which saves power compared with the phone reading repeatedly.

Design the data

Pack values into small fixed formats, such as a 16-bit integer in a defined byte order, and document the scaling, for example temperature in hundredths of a degree. The default payload is small, about 20 bytes before negotiation, so send compact data and split large messages into packets. Use one characteristic for readings and another for commands, and keep them simple.

Treat anything a phone can write as input from outside: check the length and the range, and ignore bad values.

Connections and security

Advertising, connection interval and sleep determine power. A longer interval saves energy and adds delay. Pairing with encryption protects the data, and a device that controls something real should require it, not accept writes from anyone nearby. Test with a generic scanner app before writing your own app.

Common mistakes
  • Letting any nearby phone write to a control characteristic without pairing.
  • Not documenting the byte order and scaling of the values.
  • Polling by read when notify would save power.
Key formulas
Interval × (1 + latency)
how long a peripheral may stay silent between connection events
Advanced lesson · Pro plan

Practise this lesson with Pro

The explanation above is free for everyone. The practice for Advanced lessons in every domain are part of the Pro plan at ₹999 a month.

With Pro you also get: a worked example, a hands-on task, a 3-question check.

Common questions

What will I learn in "Bluetooth Low Energy: GATT Services"?

Design a BLE peripheral with services and characteristics, use notifications, and balance range, speed and power.