Explain the BLE protocol stack — GAP, GATT, ATT, and L2CAP
The BLE protocol stack is organized into layers that each serve a distinct role, much like the OSI model but tailored for low-energy, short-range communication. At the bottom sits the Physical Layer (2.4 GHz ISM band, 40 channels, 1 Mbps or 2 Mbps PHY) and the Link Layer, which handles advertising, scanning, connection management, and channel hopping. Above the Link Layer is L2CAP (Logical Link Control and Adaptation Protocol), which provides channel multiplexing — it routes data to either the ATT (Attribute Protocol) channel or the SMP (Security Manager Protocol) channel. L2CAP also handles fragmentation and reassembly of larger payloads into the Link Layer's maximum PDU size (typically 27 bytes without Data Length Extension, up to 251 bytes with DLE).
ATT defines a simple client-server model where one device (the GATT server) exposes a table of attributes — each identified by a 16-bit handle, a UUID (type), a value, and permissions (read, write, notify). The client can discover, read, write, and subscribe to these attributes using ATT operations. GATT (Generic Attribute Profile) adds structure on top of ATT by organizing attributes into Services (groupings of related data, identified by standard or custom UUIDs) and Characteristics (individual data points within a service, with properties like Read, Write, Notify, Indicate). For example, a Heart Rate Service contains a Heart Rate Measurement characteristic that supports Notify, and a Body Sensor Location characteristic that supports Read.
GAP (Generic Access Profile) sits at the top and defines how devices discover each other and establish connections. It defines four roles: Broadcaster (advertise only), Observer (scan only), Peripheral (advertise and accept connections), and Central (scan and initiate connections). GAP controls advertising parameters (interval, data payload, scan response), connection parameters (connection interval, slave latency, supervision timeout), and bonding/pairing procedures. A common interview mistake is conflating GAP and GATT — GAP handles the "how do I find and connect to you" question, while GATT handles "what data do you have and how do I access it." Understanding this separation is essential for designing BLE firmware, because advertising data (GAP level) is limited to 31 bytes and is broadcast, while GATT data flows over an established connection with acknowledgment and flow control.
Source: Wireless Technologies Q&A
