Bluetooth & BLE · Study deck

Python BLE: Runtime Boundaries and Positioning

A Bleak client can scan, connect, or notify, but each role has a runtime limit.

Radio Remi is your guide for this deck.

implpythonindoor
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: With n=2, an 8 dB weaker reading multiplies 2.00 m to 5.02 m, while an 8 dB stronger reading gives 0.796 m—not the chapter quiz’s 1.4 m.
  • Explain: A modern BLE controller can time-slice more than one role, but Bleak exposes the Python host as the scanner, initiator, and GATT client.
  • Explain: The error:: A Python script using bleak connects to a BLE temperature sensor, reads data in a loop, but doesn't handle disconnections.
  • Explain: File writes, MQTT publishes, and database retries belong outside the BLE callback path.
iotclass.org

Major section

Deep Dive: Bleak Role Boundaries and Runtime Limits

Bleak is a Central/client API.

  • A modern BLE controller can time-slice more than one role, but Bleak exposes the Python host as the scanner, initiator, and GATT client.
  • The scaling constraint is usually scheduling, not Python byte throughput.
  • File writes, MQTT publishes, and database retries belong outside the BLE callback path.

Key terms

Disconnects
Disconnects are normal events.

Why it matters

Otherwise a clean connection log can still hide truncated data.

iotclass.org

Major section

Deep Dive: Bleak Role Boundaries and Runtime Limits (continued)

A 38 byte sensor report needs MTU negotiation or application-level chunking with an order field and a missing-chunk timeout.

  • ATT framing also changes what a successful trace proves.
  • With the default ATT MTU, a notification commonly carries up to 20 application bytes after overhead.
  • Disconnects are normal events.
iotclass.org

Major section

Indoor Positioning with BLE Beacons

A location estimate must carry its uncertainty into the decision.

  • Bluetooth Low Energy is the short-range radio system used by the beacons; it is shortened to BLE.
  • Received signal strength means the radio power that arrives at a receiver.
  • RSSI means received signal strength indicator, the radio's reported estimate of that power.

Why it matters

A single beacon can only suggest “near” or “far”; three constraints allow a best-fit (x, y) estimate.

Three beacons, three distance estimates, and a best-fit intersection for BLE trilateration
Three beacons, three distance estimates, and a best-fit intersection for BLE trilateration
iotclass.org

Major section

Indoor Positioning with BLE Beacons (continued)

This route test does not prove room-level accuracy everywhere.

  • A beacon signal near a doorway is not yet a position.
  • RSSI, calibration, anchor placement, filtering, movement, walls, and uncertainty decide whether a location claim is useful or misleading.
  • The mathematical gist.: Distance sits in an exponent: d̂/d0=10^(δ/10n).
iotclass.org

Major section

Indoor Positioning with BLE Beacons (continued)

With n=2, an 8 dB weaker reading multiplies 2.00 m to 5.02 m, while an 8 dB stronger reading gives 0.796 m—not the chapter quiz’s 1.4 m.

  • The linear shortcut predicts 92.1% at 8 dB, but the exact increase is 151%.
  • The estimated position is where the three circles overlap most closely.
  • The circles represent the distance measurement from each beacon — the estimated position is where they intersect.
iotclass.org

Major section

Common Mistake: Not Handling BLE Disconnections in Long-Running Scripts

The error:: A Python script using bleak connects to a BLE temperature sensor, reads data in a loop, but doesn't handle disconnections.

  • Script connects successfully.
  • Outer while loop:: Retries connection if it fails initially or drops.
  • Rule of thumb:: All production BLE scripts need reconnection logic.

Key terms

BLE
BLE is wireless and inherently unreliable—disconnections are normal, not exceptions.

Why it matters

Backoff delay: 5-second wait between reconnect attempts (prevents busy loop).

iotclass.org

Deck summary

Key takeaways

Bleak is a Central/client API.

  • A 38 byte sensor report needs MTU negotiation or application-level chunking with an order field and a missing-chunk timeout.
  • A location estimate must carry its uncertainty into the decision.
  • This route test does not prove room-level accuracy everywhere.
  • With n=2, an 8 dB weaker reading multiplies 2.00 m to 5.02 m, while an 8 dB stronger reading gives 0.796 m—not the chapter quiz’s 1.4 m.
iotclass.org

Retrieval practice

Recall check 1 of 3

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

Q1Per this BLE cheat sheet's GATT structure, what is the correct hierarchy from top to bottom?

AProfile -> Service -> Characteristic
BCharacteristic -> Service -> Profile
CService -> Profile -> Characteristic
DProfile -> Characteristic -> Service
Show answer

Answer: A The cheat sheet states the GATT structure directly: Profile -> Service -> Characteristic, where each Characteristic supports Read/Write/Notify operations and each Service is identified by a 16-bit or 128-bit UUID.

Q2Per this cheat sheet's Roles list, which role 'has the GATT database'?

AServer
BClient
CCentral
DPeripheral
Show answer

Answer: A The cheat sheet's Roles section lists four terms: Central (scans, initiates connection), Peripheral (advertises, accepts connection), Server (has GATT database), and Client (reads/writes characteristics).

iotclass.org

Retrieval practice

Recall check 2 of 3

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

Q3Why is RSSI-based distance estimation considered approximate in BLE applications?

ABecause BLE radios have poor signal quality compared to Wi-Fi
BBecause real environments distort RSSI
CBecause BLE uses frequency hopping which changes the signal frequency constantly
DBecause RSSI values are only available during advertising, not during connections
Show answer

Answer: B Correct!

iotclass.org

Retrieval practice

Recall check 3 of 3

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

Q4A trilateration system uses three beacons with clean RSSI readings, yet position estimates still wander by several metres. What is the most fundamental reason?

ARSSI-to-distance conversion is approximate because walls, bodies, and multipath distort signal strength.
BThree beacons define a triangle, and positions inside triangles are unstable.
CBLE advertisements are encrypted, so the readings arrive with random noise added.
DThe 2.4 GHz band only permits one distance measurement per second.
Show answer

Answer: A Right.

iotclass.org

Print reference

Answers

Answer key.

  1. A · The cheat sheet states the GATT structure directly: Profile -> Service -> Characteristic, where each Characteristic supports Read/Write/Notify operations and each Service is identified by a 16-bit or 128-bit UUID.
  2. A · The cheat sheet's Roles section lists four terms: Central (scans, initiates connection), Peripheral (advertises, accepts connection), Server (has GATT database), and Client (reads/writes characteristics).
  3. B · Correct!
  4. A · Right.
iotclass.org