Bluetooth & BLE · Study deck

Python BLE: Scanning and Connections

A Bluetooth Low Energy (BLE) scanner listens for adverts, the short broadcasts nearby devices send.

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:

  • Implement Production BLE Scanners: Configure device filtering by RSSI threshold and name patterns using the bleak library
  • Analyze GATT Services: Connect to BLE devices and enumerate services and characteristics to assess device capabilities
  • Distinguish Beacon Protocols: Compare iBeacon and Eddystone advertisement packet structures and justify protocol selection for a given use case
  • Design Proximity Detection Systems: Apply RSSI smoothing algorithms and construct zone-based presence detection with exponential moving averages
iotclass.org

Major section

Start With the Decision

A Bluetooth Low Energy (BLE) scanner listens for adverts, the short broadcasts nearby devices send.

  • The code must filter for the right device before connecting.
  • A timeout is the limit on how long it waits for that connection.

It can see many adverts but still miss the device a service needs.

iotclass.org

Major section

In 60 Seconds

BLE is the low-energy branch of Bluetooth: RSSI is a number that shows the received signal level.

  • The program must find the right tag, connect only when needed, read the expected value, and continue after the tag moves away.
  • Signal strength can support a rough zone, but it is not a tape measure.

Key terms

BLE
BLE is the low-energy branch of Bluetooth.
RSSI
RSSI is a number that shows the received signal level.
iotclass.org

Major section

In 60 Seconds (continued)

Walls, bodies, radio power, and device position can move the reading.

  • Under the Hood explains runtime limits and the maths behind rough signal zones.
  • A name may be absent or copied.
  • An awaited task lets other work run while it waits.
  • A long block in a callback can hold up new data and clean shutdown.

BLE is the low-energy branch of Bluetooth.

iotclass.org

Major section

In 60 Seconds (continued)

Production apps need RSSI filtering, GATT service exploration, and exponential smoothing for proximity -- zone-based classification (immediate/near/far) is far more reliable than precise distance calculations due to RSSI variability.

  • The program should end without a stuck scan or hidden task.
  • For long runs, add a reconnect rule.
  • Smooth only enough to support the named zone.
iotclass.org

Major section

In 60 Seconds (continued)

For the museum case it might be, "Find tag A, read its heat value, and store one fresh row." That line keeps a scan demo from growing into an unsafe service by accident.

  • Recheck after any of these change.
  • A script that works once is a start, not a support plan.
  • BLE development in Python uses the bleak library for cross-platform async scanning, connecting, and GATT interaction.
iotclass.org

Major section

Key Concepts

BleakClient: bleak class representing a connection to a BLE peripheral; provides methods for service discovery, read, write, start_notify, stop_notify.

  • BleakScanner: bleak class for BLE device discovery; supports filtering by service UUID, device name, and RSSI threshold.
  • asyncio.run(): Python coroutine runner; required for bleak operations which are all async; use asyncio.get_event_loop() for integration with existing async frameworks.
iotclass.org

Major section

Python BLE Implementation Pattern

In this pattern, the Python host is the BLE Central and GATT client.

  • While scanning it is acting as an Observer; after selection it initiates the connection and reads, writes, or subscribes to the peripheral's GATT server.

Use Python BLE code where the device doing the Bluetooth work has enough compute, storage, and operating-system support to run a gateway or test tool.

iotclass.org

Major section

BLE Scanner with Device Filtering

For implementation review, keep a repeatable scanner log: adapter used, scan timeout, target name or service UUID, RSSI threshold, advertisements received, advertisements rejected by filters, and the selected device identifier.

  • The script should keep a short sample window and select the strongest stable match instead of connecting to the first advertisement packet it sees.

- The scan duration and minimum RSSI threshold, for example 15.0s and -70 dBm.

iotclass.org

Major section

BLE Proximity Detector

A typical approach trace should show the raw RSSI, smoothed RSSI, estimated distance, and current zone at each sample.

  • A higher alpha reacts faster but lets more noise through.
  • A lower alpha is steadier but takes longer to follow real movement.
  • Values around 0.2 to 0.3 are common starting points for zone-based proximity.
iotclass.org

Deck summary

Key takeaways

A Bluetooth Low Energy (BLE) scanner listens for adverts, the short broadcasts nearby devices send.

  • BLE is the low-energy branch of Bluetooth: RSSI is a number that shows the received signal level.
  • Walls, bodies, radio power, and device position can move the reading.
  • Production apps need RSSI filtering, GATT service exploration, and exponential smoothing for proximity -- zone-based classification (immediate/near/far) is far more reliable than precise distance calculations due to RSSI variability.
  • BleakClient: bleak class representing a connection to a BLE peripheral; provides methods for service discovery, read, write, start_notify, stop_notify.
iotclass.org

Retrieval practice

Recall check 1 of 6

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

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

AA claim Bluetooth is common, without deployment power or failure evidence.
BA one-time connection screenshot without security or recovery evidence.
CA plan to define retest triggers only after field failures start.
DA BLE design record covering roles, power, security, recovery, and retest.
Show answer

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

iotclass.org

Retrieval practice

Recall check 2 of 6

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

Q2An async BLE app stops receiving data after a disconnect. What should its event flow handle?

AHandle disconnection and reconnection with a bounded retry budget.
BKeep reading a disconnected client forever without a new link attempt.
CRun discovery once and assume the connection never drops later.
DTreat each missing value as a new proximity zone rather than a link fault.
Show answer

Answer: A BLE links can drop during normal use, so the app needs a reconnect path.

iotclass.org

Retrieval practice

Recall check 3 of 6

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

Q3A Bleak client connects but cannot read the expected value. What should it inspect next?

AInspect GATT services, characteristics, and read permissions.
BInspect scanner RSSI alone, ignoring connected GATT properties.
CInspect only the computer clock instead of BLE data access.
DInspect a similarly named value without verifying its characteristic.
Show answer

Answer: A The device must expose the expected GATT path and operation.

iotclass.org

Retrieval practice

Recall check 4 of 6

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

Q4A venue needs beacon IDs grouped by building area and individual display. Which format matches that field plan?

AA GATT read with no beacon-advertisement identity fields.
BAn RSSI level treated as a stable building and display identifier.
CAn iBeacon UUID, major, and minor field plan for area and display.
DAn Eddystone URL treated as if it contained major and minor fields.
Show answer

Answer: C Those fields support grouping and a distinct beacon identity.

iotclass.org

Retrieval practice

Recall check 5 of 6

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

Q5A beacon must broadcast a compact web link. Which format fits that job?

AAn Eddystone URL frame advertising a compact web address.
BAn iBeacon major field used as though it held a web link.
CAn iBeacon calibrated-power field used as a URL payload.
DA GATT notification that needs a connection to carry the link.
Show answer

Answer: A That frame carries a compact link in its advertisement.

iotclass.org

Retrieval practice

Recall check 6 of 6

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

Q6A proximity zone flips back and forth as RSSI varies. What should the app add?

ASmooth RSSI before applying zone thresholds to noisy readings.
BTreat each raw RSSI sample as exact distance despite variation.
CClassify by device name alone without using observed signal level.
DIgnore thresholds and label every detected device as nearby.
Show answer

Answer: A Smoothing reduces jumps caused by noisy radio samples.

iotclass.org

Print reference

Answers

Answer key.

  1. D · BLE implementation review needs observable behavior, not just a one-time demo result.
  2. A · BLE links can drop during normal use, so the app needs a reconnect path.
  3. A · The device must expose the expected GATT path and operation.
  4. C · Those fields support grouping and a distinct beacon identity.
  5. A · That frame carries a compact link in its advertisement.
  6. A · Smoothing reduces jumps caused by noisy radio samples.
iotclass.org