Bluetooth LE · lesson 4 of 8
Make the link private, and remember who's allowed
In B3 anyone nearby could connect and read every byte. B4 adds a Vault characteristic that the board only serves over an encrypted, authenticated link. Touch it from the phone and pairing starts: the passkey appears big on the TFT, or both screens show the same six digits and you confirm with the button. Afterwards the board bonds: it keeps the keys in flash, recognises your phone behind its changing private address, and can refuse everyone else.
Pair your phone with each of the three methods, read the Vault, and lock the board to your phone.
You're done when
- With
pair passkey, reading the Vault on the phone brings up a code prompt, the TFT shows six digits, and after typing them the phone reads the bench's secret: 42. - The ceremony view fills phase by phase up to bonded, and the header shows a violet lock and an amber key.
- With
pair justworks(afterforget alland forgetting the board on the phone) the link is encrypted but the Vault is still refused: you can explain why. - With
pair compareyou accept the matching numbers with a click on the board and a tap on the phone. - After
allow bonded, your phone still connects, and a second phone (or the same phone after forgetting the board) can't.
On the board
You will learn
- pairing vs bonding
- IO capabilities
- Just Works · Passkey · Numeric Comparison
- LE Secure Connections (ECDH)
- MITM protection
- LTK & IRK
- private addresses
- filter accept list
uv run pio run -d labs/ble-lab-b4 -e ble-lab-b4 -t uploadParts required
Parts library- ESP32-S3-DevKitC-1 + USB-C data cable
- 1.9″ ST7789 screen
- Smartphone (Bluetooth)
- Optional: Push button
Which pairing method? It depends on both sides
At the start of pairing each side says what it can do: show a number, type one, press yes or no, or nothing at all. The two answers decide the method. Pick the phone's and the board's capabilities (the board's are what pair sets) and see who shows and who types.
BLE security capability matrix mapping IO capabilities to Just Works vs Passkey authentication. At the start of pairing each side says what it can do: show a number, type one, press yes or no, or nothing at all. The two answers decide the method. Pick the phone's and the board's capabilities (the board's are what pair sets) and see who shows and who types.
- Key concepts: pairing vs bonding, IO capabilities, Just Works · Passkey · Numeric Comparison, LE Secure Connections (ECDH), MITM protection, LTK & IRK, private addresses, filter accept list.
- Signals & pins: 3V3 (VCC), GPIO7 (BLK), GPIO8 (RES), GPIO9 (DC), GPIO10 (CS).
Interactive diagram loads after page initialization.
One pairing, packet by packet
The Security Manager Protocol (SMP) messages that cross the air when the phone reads the Vault, with their real bytes. Each method differs only in the middle: how a person proves nobody sits in between.
SMP pairing and bonding sequence illustrating Just Works, Passkey Entry, MITM protection, and LTK key storage. The Security Manager Protocol (SMP) messages that cross the air when the phone reads the Vault, with their real bytes. Each method differs only in the middle: how a person proves nobody sits in between.
- Key concepts: pairing vs bonding, IO capabilities, Just Works · Passkey · Numeric Comparison, LE Secure Connections (ECDH), MITM protection, LTK & IRK, private addresses, filter accept list.
- Signals & pins: 3V3 (VCC), GPIO7 (BLK), GPIO8 (RES), GPIO9 (DC), GPIO10 (CS).
Interactive diagram loads after page initialization.
What the screen shows
Five views. A click goes to the next one; a double-click runs the action in the footer (secure, bonds, press). While numbers wait for comparison, the button answers instead: click = yes, long press = no.
Gallery of ST7789 screens shown across the ESP32-S3 Bluetooth LE labs. Five views. A click goes to the next one; a double-click runs the action in the footer (secure, bonds, press). While numbers wait for comparison, the button answers instead: click = yes, long press = no.
- Key concepts: pairing vs bonding, IO capabilities, Just Works · Passkey · Numeric Comparison, LE Secure Connections (ECDH), MITM protection, LTK & IRK, private addresses, filter accept list.
- Signals & pins: 3V3 (VCC), GPIO7 (BLK), GPIO8 (RES), GPIO9 (DC), GPIO10 (CS).
Interactive diagram loads after page initialization.
Commands, in order
Type these in the serial monitor (uv run pio device monitor) unless the grey tag says otherwise. The coloured tag says what should be near the board while it runs.
uv run pio run -d labs/ble-lab-b4 -e ble-lab-b4 -t uploadMac terminalno phone needed- Does
- Builds lab B4 and writes it to the board.
- You should see
- The ceremony view: “Waiting for a phone to connect...” and eight grey phases.
- If not
- “Failed to connect”: hold BOOT, tap RST, release BOOT, run it again.
uv run pio device monitorMac terminalno phone needed- Does
- Opens the serial monitor.
- You should see
- pairing: Passkey · lock off · anyone may connect, then the command list. The method is saved in flash: it survives a reflash of B4.
nRF Connect → Bench B4 → Connect → Vault → readon your phonephone connected- Does
- The Vault refuses a plain-text link with “Insufficient Authentication”; the phone reacts by starting pairing.
- You should see
- The phone asks for a code; the TFT shows PASSKEY ENTRY and six digits; the monitor
! passkey 482915: type it on the other device. The ceremony view's phases 1-5 fill, “check” pulses amber. - If not
- No prompt on iPhone: read the Vault again. On Android the prompt may be a notification.
type the six digits on the phoneon your phonephone connected- Does
- Both sides check the passkey bit by bit (20 rounds of confirm and random values), then encrypt and swap keys.
- You should see
+ secured: authenticated, bonded, key 16 B,- identity: 4C:…; the phone reads the bench's secret: 42. Lock and key icons in the header; the ceremony track is complete.- If not
x … confirm failed: wrong passkey: a typo. Read the Vault again for a new code.
disconnect, then connect again and read the Vaulton your phonephone paired- Does
- A bonded phone has the keys: no ceremony, encryption starts straight away.
- You should see
- The ceremony view says stored keys: bonded before, no ceremony and jumps from connect to encrypted.
bondsserial monitorphone paired- Does
- Lists the bonded devices by identity address (the IRK resolves the phone's private address to it).
- You should see
1 bonded device …and1 4C:… random static(iPhones and most Androids use a random static identity). The bonds view marks it “<- here”.
forget all, then forget Bench B4 on the phoneserial monitorphone paired- Does
- Deletes the bonds on the board. The phone still has its half: forget the device in Settings → Bluetooth (iPhone: ⓘ → Forget This Device; Android: the gear → Unpair; macOS: right-click → Forget).
- You should see
! forgot every bond,- now forget "Bench B4" on the phone too. If you skip the phone part:x ENC_CHANGE failed 0x206: key missing: one side forgot the bond.
pair justworks, then read the Vaultserial monitorphone connected- Does
- The board now says “no input, no output”: nobody can check anything. The link gets encrypted, but not authenticated.
- You should see
+ secured: Just Works, bonded, a violet lock, but the Vault read fails again with Insufficient Authentication: the Vault demands MITM protection.- If not
- That's the lesson: Just Works stops eavesdroppers, not a man in the middle.
pair compare (forget first), read the Vault, then clickpush button / BOOTphone connected- Does
- Both screens show the same six digits. A click on the board says yes, a long press no; the phone has its own button.
- You should see
- NUMERIC COMPARISON on the TFT, the same number on the phone; after both yes:
+ secured: authenticated, bonded. - If not
- A long press (no) ends with
numbers didn't match: said no.
allow bondedserial monitorphone paired- Does
- Only bonded identities go on the filter accept list; the controller resolves each private address with the stored IRKs and refuses the rest.
- You should see
+ only bonded phones may connect (saved). Your phone connects; another one (or yours after forgetting) gets no connection at all.- If not
- Locked yourself out:
allow anyoneover serial.
lock onserial monitorno phone needed- Does
- LED and Message also need an encrypted link. Permissions are part of the GATT table, built once at start-up, so the board saves the setting and restarts.
- You should see
lock on saved. … the board restarts now...; afterwards writing the LED from a non-paired phone triggers pairing too.- If not
- iPhone shows old permissions: turn Bluetooth off and on (it caches the table).
Steps
0 of 6 ticked. Ticks are saved in this browser.
Pairing has two halves: whenever you forget on the board, forget the board on the phone too (iPhone: Settings → Bluetooth → ⓘ → Forget This Device; Android: Bluetooth → gear → Unpair; macOS: System Settings → Bluetooth → right-click → Forget), or the next connection fails with “key missing”. If only the phone forgot, NimBLE accepts the new pairing and replaces the old bond by itself (repeat pairing). The method, lock and allow settings are saved in flash (namespace b4) and survive restarts. NimBLE doesn't report the features, method and key-exchange phases one by one: the ceremony view draws them as inferred (a ring) once a later phase proves they happened. The board ships with the secret “the bench's secret: 42”; vault <text> changes it until the next restart.
Preview: what you should see
Interactive BLE scanner and GATT monitor simulating NimBLE advertising discovery and characteristic updates.
- Key concepts: pairing vs bonding, IO capabilities, Just Works · Passkey · Numeric Comparison, LE Secure Connections (ECDH), MITM protection, LTK & IRK, private addresses, filter accept list.
- Signals & pins: 3V3 (VCC), GPIO7 (BLK), GPIO8 (RES), GPIO9 (DC), GPIO10 (CS).
Interactive diagram loads after page initialization.
ESP32-S3 peripherals used in this lab
Open interactive GPIO pinoutExplore the hardware blocks, pin rules, and registers behind this experiment.
- GPIO and IO MUX
45 pins, and a matrix that routes almost any signal to almost any pin
- SPI
Fast four-wire bus for screens, card readers and flash
- Bluetooth LE
Bluetooth 5 Low Energy: beacons, phones, keyboards, MIDI
- Random number generator
True random numbers from thermal noise
- UART
Three serial ports, the one you see in the serial monitor
- CPU
Two Xtensa LX7 cores at up to 240 MHz
Troubleshooting & hardware gotchas
See all 17 ESP32-S3 hardware gotchas- What if it does not work or behaves unexpectedly?
- Pairing has two halves: whenever you
forgeton the board, forget the board on the phone too (iPhone: Settings → Bluetooth → ⓘ → Forget This Device; Android: Bluetooth → gear → Unpair; macOS: System Settings → Bluetooth → right-click → Forget), or the next connection fails with “key missing”. If only the phone forgot, NimBLE accepts the new pairing and replaces the old bond by itself (repeat pairing). The method, lock and allow settings are saved in flash (namespace b4) and survive restarts. NimBLE doesn't report the features, method and key-exchange phases one by one: the ceremony view draws them as inferred (a ring) once a later phase proves they happened. The board ships with the secret “the bench's secret: 42”;vault <text>changes it until the next restart. - If “uv run pio run -d labs/ble-lab-b4 -e ble-lab-b4 -t upload” does not work
- “Failed to connect”: hold BOOT, tap RST, release BOOT, run it again.
- If “nRF Connect → Bench B4 → Connect → Vault → read” does not work
- No prompt on iPhone: read the Vault again. On Android the prompt may be a notification.
- If “type the six digits on the phone” does not work
x … confirm failed: wrong passkey: a typo. Read the Vault again for a new code.- If “pair justworks, then read the Vault” does not work
- That's the lesson: Just Works stops eavesdroppers, not a man in the middle.
- If “pair compare (forget first), read the Vault, then click” does not work
- A long press (no) ends with
numbers didn't match: said no. - If “allow bonded” does not work
- Locked yourself out:
allow anyoneover serial. - If “lock on” does not work
- iPhone shows old permissions: turn Bluetooth off and on (it caches the table).
- Four pins are read at reset
- GPIO0, GPIO3, GPIO45 and GPIO46 are sampled the moment the chip comes out of reset: they pick the boot mode, the flash voltage, the ROM log and the JTAG source. Anything wired to them must not pull them the wrong way at power-up. After reset they are normal pins again.
- GPIO3 has no pull resistor at reset
- GPIO0 has a weak pull-up and GPIO45/46 weak pull-downs, but GPIO3 floats. When the eFuses make it choose the JTAG source, an outside circuit has to hold it at a clear level.
- GPIO47 and 48 run at 1.8 V on R16V modules
- Modules with the ESP32-S3R16V chip set the flash voltage to 1.8 V, and GPIO47/48 share that supply. A 3.3 V part on those pins sees the wrong levels.
- The pins are not 5 V tolerant
- A high input may go up to the pin's supply plus 0.3 V, so about 3.6 V on a 3.3 V board, and the power pins are rated to 3.6 V at most. 5 V from a sensor or an Arduino can damage the chip: use a level shifter or a divider.