Bluetooth & BLE · Study deck

Python BLE: Scanning and Connections

A BLE scanner can see many adverts but still miss the device a service needs.

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 BLE scanner can see many adverts but still miss the device a service needs.

  • The code must filter first, then connect with a clear timeout.
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.
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.
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.
iotclass.org

Deck summary

Key takeaways

A BLE scanner can see many adverts but still miss the device a service needs.

  • 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

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 statement that Bluetooth is common in consumer devices, without checking this deployment
BA successful connection screenshot with no power, security, or failure-behavior evidence
CA decision to defer retest triggers until after deployment problems appear
DA BLE design evidence record
Show answer

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

iotclass.org

Print reference

Answers

Answer key.

  1. D · BLE implementation review needs observable behavior, not just a one-time demo result.
iotclass.org