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

Each lab below stops at a different observable boundary on that path.

  • The sequence becomes the chapter's running checklist for proving each step.
  • The default BLE MTU is only 23 bytes.

Numbers to remember

23 bytesThe default BLE MTU is only 23 bytes.

Why it matters

BLE can look complicated because the names are precise, but the idea is familiar: one device announces availability, another discovers and connects, and the application exchanges small structured values.

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

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 device that advertises is the peripheral.

Key terms

GATT
GATT is BLE's way to organize connected data.

Why it matters

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

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.
  • 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.
iotclass.org

Major section

Build the BLE Sensor, Step by Step (continued)

But human body blockage can shift RSSI by ±8 dBm, making the same beacon appear 2m to 15m away—why zone-based proximity (near/medium/far) is more reliable than exact distance.

  • 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.
  • The hierarchy matters because a characteristic only has application meaning inside an agreed service, UUID, encoding, properties, and access policy.
iotclass.org

Deck summary

Key takeaways

Seeing the device is only the first step.

  • Each lab below stops at a different observable boundary on that path.
  • 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 not registered in the Bluetooth SIG database
BThe client has not written 0x0001 to the CCCD
CThe MTU is too small to carry the notification packet
DBLE only supports READ and WRITE properties; NOTIFY requires Classic Bluetooth
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.

Q2In an iBeacon deployment for indoor positioning, what does the 'Signal Power' calibration value represent?

AThe maximum transmission power of the beacon in milliwatts
BThe RSSI measured at 1 meter
CThe battery voltage level of the beacon device
DThe antenna gain of the beacon in dBi
Show answer

Answer: B Correct!

Q3Place each BLE implementation layer where it lives so you can trace an application action down to the controller without mixing host and radio responsibilities.

AUser application code
BBLE host API
CGATT and GAP
DHCI driver
EBLE controller hardware
Show answer

Answer: A Separate application intent, host-side BLE services, and controller execution so you can locate an implementation fault at the right boundary.

iotclass.org

Retrieval practice

Recall check 3 of 5

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

Q4A 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 4 of 5

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

Q5How does BLE achieve low power consumption?

AUsing smaller batteries
BReducing transmission power only
CDisabling security features
DBy sleeping between radio events
Show answer

Answer: D BLE achieves ultra-low power through multiple techniques: connection intervals (device sleeps between transmissions), fast connection (6ms vs 6 seconds), simpler modulation (GFSK), fewer channels (40 vs 79), optimized protocol stack, and role separation (Broadcaster, Observer, Peripheral, Central).

iotclass.org

Retrieval practice

Recall check 5 of 5

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

Q6Your BLE temperature sensor advertises every 1 second. After deployment, you notice smartphone battery drains faster than expected. What's the optimization?

AIncrease advertising interval to reduce phone scanning
BUse a GATT connection, not continuous scanning
CSwitch to Classic Bluetooth for more efficient continuous connection
DReduce advertising packet size to save power
Show answer

Answer: B BLE advertising is for discovery, not data transfer.

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 · Correct!
  3. A · Separate application intent, host-side BLE services, and controller execution so you can locate an implementation fault at the right boundary.
  4. 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. D · BLE achieves ultra-low power through multiple techniques: connection intervals (device sleeps between transmissions), fast connection (6ms vs 6 seconds), simpler modulation (GFSK), fewer channels (40 vs 79), optimized protocol stack, and role separation (Broadcaster, Observer, Peripheral, Central).
  2. B · BLE advertising is for discovery, not data transfer.
iotclass.org