Bluetooth Low Energy, from the radio up · Puzzle 3 · 4 of 5

The filing cabinet

In puzzle 2, the phone and the strap met on a timetable. Now they need to agree on what the data is: which value is the heart rate, and which is the battery level.

The problem

In puzzle 2, the phone and the strap met on a timetable to exchange packets. But a packet is just bytes. The strap has a heart rate, a battery level, a name, maybe a setting for its light. How does the phone ask for "the heart rate" and know the bytes it gets back are one?

See it

The strap holds a filing cabinet (GATT server); drawers are services (Heart Rate, Battery); folders inside are characteristics with one value each, named by a UUID; the phone (GATT client) asks for one by name and gets the value strap GATT server Heart RateBattery measurement72 UUID 2A37 GATT client read 2A37
  1. The strap keeps its data in a filing cabinet: GATT. The strap is the GATT server, the side that holds the data.

    Step 1 of 4
  2. Each drawer is a service, a group of related values: Heart Rate, Battery.

    Step 2 of 4
  3. In a drawer, each folder is a characteristic: one value, like the latest heart rate, 72. Its name is a UUID: for a standard value like this, the same number on every brand of strap. Predict: how does the phone ask for it?

    Step 3 of 4
  4. By that name. The phone is the GATT client: "read 2A37", and the strap answers with the folder's value.

    Step 4 of 4

The building block

GATT (Generic Attribute Profile) is the way a BLE device organizes its data, so any phone can find its way around it. The side that holds the data is the GATT server, usually the small device. The side that reads and writes it is the GATT client, usually the phone.

The data comes in two levels. A service is a group of related values: Heart Rate, or Battery. Inside each service, a characteristic holds one value: the latest heart rate, or the battery level in percent. A client can read a characteristic, write it, or, as the next puzzle shows, be told when it changes. Every read travels at the connection events: the request at one meeting, and the answer at that meeting or a later one. Each service and each characteristic is named by a UUID (universally unique identifier), a long number. For standard features, the Bluetooth group fixed the UUIDs in advance, so they're the same on every device: unlike the random ones UUID.randomUUID() gives you, these are agreed. They're written in hexadecimal (digits 0–9 and A–F) and, for standard features, shortened to four digits, so the heart-rate measurement is 2A37. That lets an app look for "the Heart Rate service" on any brand of strap.

Right after connecting, the phone asks the strap for its list of services and characteristics: service discovery. Only then does it know which drawers this cabinet has.

GATT's roles are separate from the roles in puzzle 2. Central and peripheral are about who sets the timing; server and client are about who holds the data. A phone can be a GATT server too, for example when a watch reads the phone's messages.

The analogy

The strap carries a filing cabinet. Each drawer is a service, with a label. Inside, each folder is a characteristic holding one sheet with one value. The phone doesn't rummage: it asks for "drawer Heart Rate, folder Measurement" by its label, and gets that sheet.

Check yourself

Your app works with one brand of heart strap. A different brand also offers the standard Heart Rate service. What does your app need to change to read it?

Right. Standard services share UUIDs across brands.

Not quite. Timing has nothing to do with where the value lives; the UUIDs do. Re-read that part ↑

Not quite. The standard service and characteristic have the same UUIDs on every device that offers them.

Why does GATT group values into services instead of one long list?

Not quite. Services are about organizing data, not packet size.

Right. A service is a feature you can find by name.

What's the difference between a GATT server and a peripheral?

Not quite. Usually, but not always: a phone (central) can be a GATT server too. Re-read that part ↑

Right. Timing roles and data roles are separate.

Not quite. The central sets timing; the server holds data. Those are separate roles. Re-read that part ↑

Sources

What you now know

GATT organizes a device's data like a filing cabinet: services are drawers, characteristics are folders holding one value, each named by a UUID. The GATT server holds them; the GATT client reads and writes them.