Bluetooth & BLE · Study deck

Python BLE: Runtime Boundaries and Positioning

Bleak is the Python tool used here for Bluetooth Low Energy (BLE).

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: The imperfect overlap is meaningful: multipath, body shadowing, antenna orientation, and the path-loss model create residual error, which the implementation must retain rather than hiding behind an exact coordinate.
  • 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: The circles represent the distance measurement from each beacon — the estimated position is where they intersect.
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.

BLE trilateration uses beacons A, B and C around an estimated position. RSSI measurement feeds distance estimation and position calculation using a path-loss model.
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%.
iotclass.org

Major section

Indoor Positioning with BLE Beacons (continued)

The imperfect overlap is meaningful: multipath, body shadowing, antenna orientation, and the path-loss model create residual error, which the implementation must retain rather than hiding behind an exact coordinate.

  • The estimated position is where the three circles overlap most closely.
iotclass.org

Major section

Indoor Positioning with BLE Beacons (continued)

The circles represent the distance measurement from each beacon — the estimated position is where they intersect.

  • Checkpoint: you should now be able to explain why three noisy distances produce a best-fit position rather than a guaranteed exact location.
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 5

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

Q1A Python app finds a tag, connects, and waits for measurements. Which split of work fits Bleak?

AHave the callback discover radios before the scanner or client is ready.
BHave the client make the Python computer act as a BLE peripheral.
CUse scanner for discovery, client for connection, and callback for notifications.
DHave the scanner store the tag's GATT database without connection.
Show answer

Answer: C These roles match discovery, connection, and pushed data.

iotclass.org

Retrieval practice

Recall check 2 of 5

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

Q2Per 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.

iotclass.org

Retrieval practice

Recall check 3 of 5

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

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

AThe GATT server role, which holds services and characteristics.
BThe GATT client role, which reads or subscribes to server data.
CThe central link role, which initiates a BLE connection.
DThe peripheral link role, which advertises for a connection.
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 4 of 5

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

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

ABecause BLE signal quality is always worse than Wi-Fi in every room.
BBecause walls, bodies, and multipath distort received signal strength.
CBecause frequency hopping prevents any meaningful RSSI measurement.
DBecause RSSI exists only in advertisements and never in a connection.
Show answer

Answer: B see answers page

iotclass.org

Retrieval practice

Recall check 5 of 5

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

Q5A 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 remains approximate under walls, bodies, and multipath.
BThree beacons form a triangle whose interior is inherently unpositionable.
CEncrypted advertisements inject random noise into every measured RSSI.
DThe 2.4 GHz band allows only one distance measurement each second.
Show answer

Answer: A Right.

iotclass.org

Print reference

Answers

Answer key.

  1. C · These roles match discovery, connection, and pushed data.
  2. 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.
  3. 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).
  4. B · With this chapter's 2-meter reference and path-loss exponent n=2, a +/-8 dB offset maps to estimated distances of about 0.80m to 5.02m. Walls, human bodies, device orientation, and reflections all cause significant variance. Zone-based proximity (immediate/near/far) is more reliable than exact distance.
  5. A · Right.
iotclass.org