Bluetooth & BLE · Study deck

BLE App Development on ESP32

Picture a room sensor that appears in a scan but sends an old temperature after reconnecting.

Radio Remi is your guide for this deck.

implementation
Radio Remi, the module guide, in a scene from this chapter.
iotclass.org

After studying this chapter

Learning objectives

You will be able to:

  • Explain: A phone is usually the central because it has the screen, the app, and the power budget to search for nearby devices, and in connected GATT examples it usually behaves as the client.
  • Explain: This nearby-device scanning is signal-strength proximity analysis in miniature, and it lets you see that BLE is not magic; it is a stream of short radio messages with measurable signal strength.
  • Explain: That ordered discovery connects the scanner exercise to the code below: the ESP32 becomes ESP32-TempSensor, but interoperability comes from the standard service contract rather than the device name.
iotclass.org

Major section

Start With the Story

Seeing the device is only the first step.

  • The app must show which value it read, when it changed, and why it is current.
  • Bluetooth Low Energy, or BLE, is a short-range radio system designed for small exchanges and low power.
  • Firmware means the software stored on the board.
iotclass.org

Major section

The BLE Idea

The peripheral tells nearby devices that it is ready.

  • There, GATT reads, writes, and notices carry small values for the app.
  • The role names are exact, but the flow is simple: announce, find, connect, and exchange.
  • The default BLE MTU is only 23 bytes.
BLE connection flow from peripheral advertising through central scanning and GATT exchange
BLE connection flow from peripheral advertising through central scanning and GATT exchange
iotclass.org

Major section

Phoebe's Field Notes: Where n=2.5 Comes From, And Why It Is Not n=2

Why does the same −74 dBm mean 3.98 m or 5.62 m?

  • The chapter’s d=10^((TxPower−RSSI)/10n) gives 3.98 m for −59/−74 dBm at n=2.5, but 5.62 m at free-space n=2.
  • At the −70 dBm NEAR/FAR rule, the n=2.5 model predicts −70.9 dBm at 3 m—already 0.9 dB beyond the boundary before the chapter’s ±8 dB body swing.
iotclass.org

Major section

Phoebe's Field Notes: Where n=2.5 Comes From, And Why It Is Not n=2 (continued)

The remote sends command messages through a BluetoothSocket.

  • In our build, the ESP32 becomes a peripheral when it behaves like a temperature sensor or an iBeacon, and in connected GATT examples it is also the server.
  • The device that scans or connects is the central.
  • A successful socket connection is therefore not proof that the actuator path is correct.
iotclass.org

Major section

Phoebe's Field Notes: Where n=2.5 Comes From, And Why It Is Not n=2 (continued)

One source-era Classic Bluetooth remote-control design makes the connected roles concrete without pretending that its socket API is GATT.

  • The receiver listens with a BluetoothServerSocket, accepts a BluetoothSocket, parses each message, and dispatches the result to a GpioProcessor that owns the hardware action.
  • GATT is BLE's way to organize connected data.
  • The value in that characteristic is the live reading.
iotclass.org

Major section

Phoebe's Field Notes: Where n=2.5 Comes From, And Why It Is Not n=2 (continued)

A temperature sensor might have an Environmental Sensing service, and inside it a Temperature characteristic.

  • The properties on a characteristic say what a central may do.
  • We start by listening before we transmit, because the fastest way to understand BLE advertising is to watch real advertisements arrive.
  • A phone is usually the central because it has the screen, the app, and the power budget to search for nearby devices, and in connected GATT examples it usually behaves as the client.
iotclass.org

Major section

Build the BLE Sensor, Step by Step

This section is one continuous build.

  • This nearby-device scanning is signal-strength proximity analysis in miniature, and it lets you see that BLE is not magic; it is a stream of short radio messages with measurable signal strength.
  • RSSI-based distance estimation is approximate.
  • The characteristic UUID 2A6E says the value is temperature.

Key terms

TxPower
TxPower is the RSSI measured at 1 meter, typically around -59 to -65 dBm for iBeacon work.

Why it matters

The hierarchy matters because a characteristic only has application meaning inside an agreed service, UUID, encoding, properties, and access policy.

GATT service and characteristic hierarchy for an environmental monitor
GATT service and characteristic hierarchy for an environmental monitor
iotclass.org

Major section

Build the BLE Sensor, Step by Step (continued)

The important path is BLEDevice::init(), then BLEDevice::getScan(), then a callback that runs for each BLEAdvertisedDevice.

  • Checkpoint: you should now see scan results in the Serial Monitor.
  • The service UUID 181A tells generic BLE tools that this is Environmental Sensing data.
  • RSSI is the measured signal strength at the receiver.
iotclass.org

Major section

Build the BLE Sensor, Step by Step (continued)

That ordered discovery connects the scanner exercise to the code below: the ESP32 becomes ESP32-TempSensor, but interoperability comes from the standard service contract rather than the device name.

  • PROPERTY_READ supports polling, while PROPERTY_NOTIFY supports real-time updates after the client enables notifications.
  • The temperature is packed as an int16_t in 0.01 C units, so 22.50 C travels as 2250.
  • Checkpoint: you should now have an ESP32 that broadcasts an iBeacon frame.
iotclass.org

Major section

Build the BLE Sensor, Step by Step (continued)

Checkpoint: you should now be able to discover ESP32-TempSensor, find service 0x181A, read characteristic 0x2A6E, and receive changing notifications once the client writes the CCCD.

  • Unlike the connected GATT example, receivers must interpret this advertisement without negotiating a service contract.
  • UUID identifies the deployment namespace, Major and Minor subdivide it, and TX Power supports a rough RSSI-based distance estimate.
  • Quick self-tests for what you just built.
iotclass.org

Major section

Build the BLE Sensor, Step by Step (continued)

This layout connects the connected sensor build to a connectionless pattern: the phone can classify nearby beacons, but payload meaning, calibration, privacy, and authenticity must be designed in advance.

  • The worked example below runs two actual measurements through the formula — open it to see why exact distances wobble and why the zone labels are the trustworthy part.
  • The path loss equation shows how RSSI translates to distance, though environmental factors add significant error.
  • An illustrative ±8 dB RSSI change multiplies that estimate by 10±8/2510^{\pm8/25}10±8/25, giving about 4.8 m to 20.9 m under the same assumed path-loss exponent.
iotclass.org

Major section

Build the BLE Sensor, Step by Step (continued)

A scanner should be able to see the UUID, major value, minor value, and calibrated signal power, then estimate distance from RSSI with visible uncertainty.

  • After the scanner, temperature service, and beacon all work, the next useful move is practice.
  • These challenges keep the same code path but ask you to change one design decision at a time.
  • Track how many devices are in each proximity zone and display a summary after each scan.
iotclass.org

Deck summary

Key takeaways

Seeing the device is only the first step.

  • The peripheral tells nearby devices that it is ready.
  • The mathematical gist: The chapter’s d=10^((TxPower−RSSI)/10n) gives 3.98 m for −59/−74 dBm at n=2.5, but 5.62 m at free-space n=2.
  • The remote sends command messages through a BluetoothSocket.
  • One source-era Classic Bluetooth remote-control design makes the connected roles concrete without pretending that its socket API is GATT.
iotclass.org

Retrieval practice

Recall check 1 of 5

Radio Remi says: answer from memory, then check your reasoning.

Q1A GATT characteristic is configured with PROPERTY_NOTIFY, but a connected client never receives updates. What is the most likely cause?

AThe service UUID is absent from the SIG registry, blocking all notifications.
BThe client has not written 0x0001 to enable Notify in the CCCD.
CThe MTU is always too small to carry a normal characteristic update.
DBLE supports read and write only, so Notify would require Classic.
Show answer

Answer: B BLE notifications are opt-in: the client must explicitly write 0x0001 to the CCCD (UUID 0x2902) before the server sends any notify packets.

iotclass.org

Retrieval practice

Recall check 2 of 5

Radio Remi says: answer from memory, then check your reasoning.

Q2What carries commands, events and data between a Bluetooth host and controller?

AUser application code
BHCI driver
CBLE host API
DGATT and GAP
Show answer

Answer: B HCI transports commands, events, and data between the host and controller.

Q3Complete the code to write a value to a BLE GATT characteristic:

Avalue = bytearray([1 if state else 0])
Bvalue = str(state).encode('utf-8')
Cvalue = bytes(state)
Dvalue = int(state).to_bytes(1)
Show answer

Answer: A BLE characteristics accept bytearray values.

iotclass.org

Retrieval practice

Recall check 3 of 5

Radio Remi says: answer from memory, then check your reasoning.

Q4A scanner sees several nearby devices. Which signal should it use for a rough proximity class?

AUse repeated RSSI samples as noisy evidence for rough proximity.
BUse the device name alone as if it encoded physical distance.
CUse one RSSI value as an exact metre-distance measurement.
DUse GATT service count alone as a proxy for nearby location.
Show answer

Answer: A RSSI can support a zone estimate after repeated samples.

iotclass.org

Retrieval practice

Recall check 4 of 5

Radio Remi says: answer from memory, then check your reasoning.

Q5A BLE implementation works once in a demo. What evidence is still needed before accepting the design?

AA statement that Bluetooth is common in consumer devices, without checking this deployment
BA record of the target device role, GATT operation, connection parameters, error handling, logs or traces.
CA successful connection screenshot with no power, security, or failure-behavior evidence
DA decision to defer retest triggers until after deployment problems appear
Show answer

Answer: B BLE implementation review needs observable behavior, not just a one-time demo result.

iotclass.org

Retrieval practice

Recall check 5 of 5

Radio Remi says: answer from memory, then check your reasoning.

Q6A BLE temperature sensor advertises a small periodic reading every second. Its phone app scans continuously to catch each reading, and measured phone battery draw is too high. Which alternative should the team test to reduce the scanning load?

AIncrease the advertising interval while keeping phone scan polling.
BUse a GATT connection for updates instead of scanning continuously.
CSwitch to Classic, assuming its continuous link costs less power.
DReduce packet length alone while retaining every-second phone scans.
Show answer

Answer: B The phone app's continuous scan is the stated cost.

iotclass.org

Print reference

Answers 1 of 2

Answer key.

  1. B · BLE notifications are opt-in: the client must explicitly write 0x0001 to the CCCD (UUID 0x2902) before the server sends any notify packets.
  2. B · HCI transports commands, events, and data between the host and controller.
  3. A · BLE characteristics accept bytearray values.
  4. A · RSSI can support a zone estimate after repeated samples.
  5. B · BLE implementation review needs observable behavior, not just a one-time demo result.
iotclass.org

Print reference

Answers 2 of 2

Answer key.

  1. B · The phone app's continuous scan is the stated cost.
iotclass.org