Bluez tutorial BLE Data Packet Extensions

 

Bluez tutorial – BLE Data Packet Extensions & Bit Stream Processing
Chapter 8 — Why packet size grew from 27 to 251 bytes and how every bit is processed before going over the air
Old Size
27 bytes (4.0)
New Size
251 bytes (4.2)
TX Steps
Encrypt → CRC → Whiten
RX Steps
Dewhiten → CRC → Decrypt

Keywords:

BLE 4.2 packet length extension BLE 251 bytes data packet BLE OTA firmware update BLE data whitening BLE bit stream processing BLE CRC before decryption BLE header overhead BLE fragmentation

LE Data Packet Extensions — The Big Change in BLE 4.2

When BLE was first released as version 4.0, the maximum data payload in a single packet was 27 bytes. The length field in the data channel PDU header was only 5 bits, which caps the maximum value at 31 — and after subtracting fixed overhead, the usable payload was 27 bytes.

BLE 4.2 changed this by expanding the length field from 5 bits to 8 bits. With 8 bits the maximum value is 255. After subtracting the 4-byte MIC, the maximum payload becomes 251 bytes. That is almost ten times more data per packet. This single change makes a significant practical difference for several important use cases.

Packet Size — Before and After 4.2

Why 27 Bytes Was a Real Bottleneck
BLE 4.0 vs BLE 4.2 — Maximum Data Payload Comparison
BLE 4.0 — max payload 27 bytes
27
← limited by 5-bit length field
BLE 4.2 — max payload 251 bytes
251 bytes — 9.3× larger
Header overhead efficiency:
BLE 4.0
22%
6 bytes header
÷ 27 bytes total
BLE 4.2
2%
6 bytes header
÷ 251 bytes total
Header overhead = bytes for headers in all protocol layers + MIC (approx 6 bytes total)

The 22% overhead in 4.0 means that for every 27 bytes the radio sends, only about 21 bytes is your actual data. In 4.2, the same 6 bytes of overhead is spread across 251 bytes, so 98% of what goes over the air is your real data. This is a big win for battery life and transfer speed.

Use Case 1 — OTA Firmware Updates

Once a BLE device ships and is deployed in the field — inside a building, on a factory floor, in a patient’s home — the manufacturer may need to push a software update. Sending the new firmware over the air (OTA) is far more convenient than physically collecting every device.

OTA Firmware Update — 4.0 vs 4.2 Transfer Time
Sending a 50 KB firmware image
BLE 4.0 — 27-byte packets → needs ~1,900 packets
…1900 packets total → minutes
BLE 4.2 — 251-byte packets → needs ~200 packets
…~200 packets → seconds

With 27-byte packets, the radio must stay on for the entire duration of the transfer — which could be several minutes depending on firmware size. Every extra minute the radio stays powered on costs battery. With 251-byte packets, the same firmware transfers in a fraction of the time, the radio shuts off sooner, and the battery lasts much longer.

A secondary benefit: packets are only sent at connection intervals, and there is dead time (silence) between intervals. The more packets are required to complete a transfer, the more of these dead-time gaps are needed, stretching the total transfer time further. Bigger packets reduce the number of intervals needed, shrinking the total active time.

Use Case 2 — Uploading Sensor Logs

A common BLE use case is a sensor tag that continuously records data and then uploads it in bulk. Consider a wearable health monitor that records body temperature and heart rate every minute throughout a full day — that is 1,440 readings. Even at just 4 bytes per reading, you need to transfer about 5.7 KB of log data.

With 27-byte packets you need over 200 individual packets to upload this. With 251-byte packets you need only about 23. The difference in upload time and energy is dramatic, especially for a device running on a coin cell battery.

BLE 4.0 — 27-byte packets

  • More packets needed
  • More connection intervals required
  • Radio active for longer
  • Higher total energy consumed
  • Risk of more transmission errors
BLE 4.2 — 251-byte packets

  • Far fewer packets needed
  • Fewer connection intervals used
  • Radio shuts off much sooner
  • Significantly less energy consumed
  • Less chance of dropped transfers

The Extra Cost of Fragmentation

When data is too large to fit in a single packet, the transmitter must break it into multiple fragments. The receiver must collect all fragments and reassemble them into the original data. This fragmentation and reassembly has a cost beyond just extra packets:

  • Processing time: The CPU must run fragmentation and reassembly logic, consuming power for each operation.
  • Connection interval gaps: BLE only sends packets at scheduled connection intervals. Between intervals the radio is off. The more fragments you have, the more intervals you need, and the longer the total transfer stretches across time.
  • Radio stays on longer: The device remains powered and active across all those intervals, draining the battery more than a single large packet would.

Larger packets reduce or eliminate fragmentation for most real-world data transfers, making the device both faster and more power-efficient at the same time.

8.10 — Bit Stream Processing

What Happens to Your Data Before It Goes Over the Air

When the link layer sends data, the raw payload does not go straight to the radio antenna. It passes through three processing steps in sequence. On the receiving side the same three steps are applied in reverse order. Understanding this pipeline is important because the order of these steps was deliberately chosen to save power — and BLE’s order is different from classic Bluetooth.

BLE Link Layer Bit Stream Processing Pipeline

TRANSMIT SIDE (TX)
📦
TX Payload
LSB first
① Encryption
AES-128
(optional)
② CRC Gen
24-bit checksum
computed
③ Whitening
XOR with
whitening word
📻 Radio

↑ Air — Radio Waves — ↓

RECEIVE SIDE (RX) — exact reverse
📻 Radio
① Dewhiten
XOR with same
whitening word
② CRC Check
Verify 24-bit
checksum
③ Decryption
AES-128
(if encrypted)
📦
RX Payload
LSB first
Original data recovered

Step 1 — Encryption (Optional)

Encryption is performed first on the transmit side. It is not always applied — only when the host has specifically requested an encrypted connection (for example, after pairing with a key exchange). If encryption is not enabled, this step is simply skipped and the raw payload moves on to CRC generation.

BLE uses AES-128 in CCM mode (Counter with CBC-MAC). The result is an encrypted payload plus the 4-byte MIC (Message Integrity Check) appended to it. On the receive side, decryption is the last step — after the CRC check has already passed.

/* Checking if a BLE connection is encrypted via BlueZ HCI */
/* After pairing, the host gets an Encryption Change event */

/* Listen for HCI Encryption Change event (event code 0x08) */
/* From BlueZ hci.h: */
typedef struct {
    uint8_t  status;    /* 0x00 = success                       */
    uint16_t handle;    /* connection handle                    */
    uint8_t  encrypt;   /* 0x00 = off, 0x01 = AES-128 CCM on   */
} __attribute__((packed)) evt_encrypt_change;

/* Using bluetoothctl to initiate pairing (triggers encryption): */
/* bluetoothctl                                                   */
/* [bluetooth]# pair AA:BB:CC:DD:EE:FF                           */
/* [bluetooth]# Pairing successful                               */
/* Connection is now encrypted with AES-128                      */
Step 2 — CRC Generation / Checking

After encryption, the 24-bit CRC is computed over the encrypted payload. The CRC travels with the packet. On the receive side, after dewhitening, the CRC is recomputed and compared against the transmitted value. If they match, the packet is good. If not, it is dropped immediately without even attempting decryption.

Why does BLE do CRC after encryption (not before)? This is a deliberate reversal from classic BR/EDR. In BR/EDR, CRC is computed before encryption, so on the receive side you must decrypt first and then CRC-check. BLE flips this — CRC is computed on the encrypted data, so you can CRC-check before decrypting. This gives three power-saving advantages explained below.

Step 3 — Data Whitening / Dewhitening

The final step before transmission is whitening. The purpose is to avoid long runs of the same bit — for example 00000000000 or 11111111111 — in the transmitted data. If the radio sends a long run of identical bits, the receiver’s synchronisation circuit (which uses bit transitions to stay locked to the timing) can lose lock and misread subsequent bits.

Whitening works by XOR-ing the data with a whitening word — a pseudo-random sequence generated from the channel index. XOR-ing with a random-looking sequence breaks up any long runs without permanently changing the data. The receiver uses the exact same whitening word to XOR again, which perfectly reverses the process and recovers the original data.

/* BLE data whitening — LFSR-based scrambling */
/* The whitening sequence is generated from the RF channel index */
/* From BlueZ net/bluetooth/ble_core.c concept: */

/* Channel index is loaded as initial value of a 7-bit LFSR */
/* The LFSR polynomial is: x^7 + x^4 + 1                    */
void ble_whiten(uint8_t *data, int len, uint8_t channel_idx)
{
    /* Initialise 7-bit LFSR with channel index (bit 6 always 1) */
    uint8_t sr = (channel_idx & 0x3F) | 0x40;

    for (int i = 0; i < len; i++) {
        uint8_t byte = data[i];
        for (int b = 0; b < 8; b++) {
            int out_bit  = sr & 1;       /* output bit        */
            int feedback = out_bit ^ ((sr >> 3) & 1); /* taps */
            sr = (sr >> 1) | (feedback << 6);
            byte ^= (out_bit << b);      /* XOR with data bit */
        }
        data[i] = byte;
    }
    /* Note: dewhitening uses the SAME function with the SAME key */
    /* XOR is self-inverse: (A XOR K) XOR K = A                  */
}

Why BLE’s Order Is Smarter Than BR/EDR’s

In BR/EDR the order is: CRC → Encrypt → Transmit on the send side, and Receive → Decrypt → CRC check on the receive side. BLE reverses the CRC and encryption steps. This might seem like a small detail, but it produces three concrete power-saving benefits:

1
Acknowledge the packet earlier, switch radio off sooner

CRC checking is far faster than AES decryption. Since BLE does CRC check first (before decryption), the receiver can acknowledge the packet as soon as the CRC passes — without waiting for decryption to finish. The radio can be switched off immediately after sending the ACK, saving power.

2
Decrypt offline after the radio is already off

Since the radio is off (ACK already sent), decryption happens with only the CPU active. No radio power is being drawn during the decryption process. This reduces peak current and spreads the work across more time without costing any additional radio-on time.

3
Skip decryption entirely on corrupt packets

If the CRC check fails, the packet was corrupted during transmission. Since decryption is done after CRC checking, there is no point decrypting a packet that is already known to be garbage. In BLE, corrupt packets are dropped immediately after the CRC check with zero decryption cost. In BR/EDR you would waste CPU time decrypting before discovering the CRC failure.

Next: Link Layer States in Detail

You now understand packet size extensions and the bit stream pipeline. The next file covers the five link layer states — Standby, Advertising, Scanning, Initiating, and Connection — with all four advertising event types explained.

Next: Link Layer States (Advertising Events) →

Leave a Reply

Your email address will not be published. Required fields are marked *