18 Python BLE: RSSI Smoothing and Zones
18.1 Start With the Decision
One weak BLE packet can make a nearby beacon look far away. Smooth the RSSI trace before a zone change can trigger an action.
18.2 Route Overview
This is part 2 of 3. Review Python BLE: Scanning and Connections for the preceding evidence.
18.3 Learning Objectives
- Smooth RSSI samples before classifying proximity zones.
- Test zone thresholds against path-loss and radio noise.
18.4 Chapter Roadmap
- Phoebe’s Field Notes: Why RSSI Distance Moves in Multipliers
- Try It: RSSI Smoothing and Zone Classification
- Putting Numbers to It
- RSSI Limitations
- Checkpoint: RSSI Smoothing and Zones
- Try It: ESP32 BLE Advertiser (Pairs with Python Scanner)
- BLE Power Optimization Decision Flow
- Visual Reference Gallery
- Visual: BLE Module Architecture
- Visual: BLE GATT Profile Implementation
- Visual: BLE Connection Flow
- Visual: BLE Stack Architecture
- Visual: Bluetooth Serial Port Profile
- Knowledge Check
- Deployment Pattern: BLE Proximity System for Retail Analytics
- Checkpoint: Deployment Trade-Offs
- Concept Relationships:
- See Also
- Practice Activities
- Interactive Quiz: Match Python BLE Concepts
- Interactive Quiz: Sequence the Python BLE Implementation
- Label the Diagram
- Code Challenge
- Start With the Story
The EMA filter’s effective window length and response time are:
where is the smoothing factor and is the sampling rate (Hz).
Example: RSSI sampling at 1 Hz (once per second) with :
- Effective window: samples
- Time to reach 95% of new value: seconds
Compare with (more smoothing):
- Effective window: samples
- Response time: seconds
Lower smooths more aggressively but reacts slower to real movement. For proximity detection, balances noise reduction with reasonable tracking speed.
RSSI-based distance estimation has inherent limitations:
- Multipath fading: Reflections cause +/-6 dBm variance
- Body shadowing: Human body attenuates 5-15 dBm
- Antenna orientation: Different orientations vary +/-10 dBm
- Environmental factors: Walls, furniture, humidity affect signal
Recommendation: Use zone-based classification (immediate/near/far) rather than precise distance calculations. For sub-meter accuracy, consider UWB technology instead.
Checkpoint: RSSI Smoothing and Zones
You now know:
- The chapter’s zone boundaries are immediate above
-55 dBm, near from about-55 dBmto-70 dBm, and far below-70 dBm. - EMA smoothing uses
smoothed_rssi = alpha * new_rssi + (1 - alpha) * prev_smoothed, so lowering alpha from0.3to0.1smooths more but slows response from about10seconds to about30seconds at1 Hz. - RSSI can vary by multipath, body shadowing, antenna orientation, and environment, so BLE proximity evidence should defend broad zones rather than exact meters.
18.5 BLE Power Optimization Decision Flow
When building battery-powered BLE devices, power optimization is critical. Inspect Figure 18.1 to turn that broad goal into an ordered design review: begin with the application’s actual data-rate and latency need, then choose whether the device needs a connection at all before tuning radio parameters.
Read Figure 18.1 from the data-rate question into its connection and beacon branches. A connected product tunes connection interval; a broadcaster tunes advertising interval. Both paths then converge on sleep mode and transmit power. The sequence matters because reducing TX power cannot compensate for a radio that wakes too often, and a long sleep schedule is unacceptable if it breaks the promised response time. Record those trade-offs alongside the measured current trace.
18.6 Visual Reference Gallery
Before treating a BLE module as one opaque part, inspect Figure 18.2 to locate the boundaries that can affect integration, radio performance, and power. The aim is to connect firmware-visible behavior to the controller, RF path, antenna network, and supply circuitry that produce it.
Read Figure 18.2 from the host controller interface toward the baseband processor and radio transceiver, then follow the RF path through antenna matching to the antenna. Finally inspect power management, because supply stability and sleep control affect every block. This view explains why a convenient integrated module still needs correct host signalling, board placement, power decoupling, and antenna clearance in the finished device.
Inspect Figure 18.3 to review a GATT design as an application contract, not merely a list of UUIDs. Look for the choices that determine what a client may do, how it interprets a value, and how both sides establish notification state.
Read Figure 18.3 from characteristic properties to value schema and version, then check permissions and security before following descriptors to CCCD subscription state. The evidence path at the end asks whether real clients can read, write, and receive notifications with the agreed encoding. That ordered review connects the Python implementation to interoperable behavior and exposes contracts that a connection-only test would miss.
Inspect Figure 18.4 to separate radio discovery from a usable application session. The path matters because each boundary can succeed while a later one still fails, and each failure needs different evidence.
Read Figure 18.4 from discovery into link establishment, then through security, ATT/GATT readiness, and application exchange. Follow each recovery arrow back to the earliest state that can be retried safely. This order connects latency and power tuning to correctness: shortening discovery or connection time is useful only when security, service discovery, subscriptions, data exchange, and reconnect behavior remain observable and reliable.
Inspect Figure 18.5 to locate which layer owns a symptom before changing application code. The controller/host split is especially important when a module exposes HCI rather than running the complete application stack internally.
Read Figure 18.5 upward from PHY and Link Layer through L2CAP, ATT, GATT, and GAP, noting where HCI separates controller duties from host protocols. RF loss belongs near the bottom; attribute permissions and values belong near ATT/GATT; discovery roles and advertising behavior belong to GAP. That mapping connects the Python examples to interoperable interfaces and prevents a link-layer success from being mistaken for an application-contract success.
When an existing product already speaks UART, Figure 18.6 shows what Bluetooth SPP preserves and what it inserts between the endpoints. Inspect it before assuming that a wireless serial link has the same timing and failure behavior as a physical cable.
Follow Figure 18.6 from the microcontroller UART into RFCOMM and the Bluetooth radio, then out through the host’s virtual COM port. The application can retain a serial-stream interface, but pairing, link establishment, buffering, disconnection, and reconnection now sit in the path. That makes SPP useful for legacy debugging and configuration while requiring explicit timeout and recovery handling.
18.7 Knowledge Check
18.8 Deployment Pattern: BLE Proximity System for Retail Analytics
Retail analytics systems often use BLE proximity detection to estimate customer dwell time and foot-traffic patterns. A typical design places Python gateways on small Linux computers near entrances and key departments, then classifies nearby beacon traffic into immediate, near, and far zones.
System Specifications:
- Scan interval:
2 seconds, balancing detection speed against gateway CPU load. - RSSI threshold:
-75 dBm, filtering devices beyond the useful local radius. - EMA alpha:
0.2, prioritizing stability over responsiveness for dwell-time estimates. - Zone boundaries:
-55 dBmand-70 dBm, mapping readings to immediate, near, and far zones. - Minimum samples:
3, requiring consecutive readings before assigning a zone. - Gateway density:
4to6gateways for a medium retail floor, adjusted after site survey testing.
Why EMA Alpha = 0.2 (Not 0.3)?
The standard alpha of 0.3 works well for single-device tracking, but in a crowded retail environment with 50-200 simultaneous BLE advertisers, lower alpha reduces false zone transitions caused by body shadowing. A customer stepping behind a display rack causes a sudden 10-15 dBm drop. With alpha 0.3, the smoothed RSSI reacts in 2 readings (4 seconds), potentially triggering a false “far” classification. With alpha 0.2, it takes 4 readings (8 seconds) — long enough for the customer to move again, preventing a spurious zone change.
Battery Impact on Beacons:
Assume a BLE beacon with a 1000 mAh battery advertising at 1 Hz:
- Advertising current per event:
8 mA. - Event duration, including radio ramp-up:
3 ms. - Events per day at
1 Hz:86,400. - Daily energy at
1 Hz:8 mA x 0.003 s x 86,400 = 2,073.6 mA-s, and dividing by3,600converts to0.576 mAh/day. - Estimated battery life at
1 Hz:1000 mAh / 0.576 mAh = 1,736 daysfrom advertising alone — sleep current and sensor reads shorten this in practice. - Estimated battery life at
10 Hz: about174 days, because daily energy rises to5.76 mAh/day.
This is why many deployments prefer 1 Hz advertising with gateway-side EMA smoothing. Raising the advertising rate by 10x can make detection feel faster, but it also turns a maintenance interval measured in years into one measured in months.
Checkpoint: Deployment Trade-Offs
You now know:
- The retail pattern combines a
2 secondsscan interval,-75 dBmfilter, alpha0.2,-55 dBmand-70 dBmzone boundaries, and3consecutive samples before assigning a zone. - For a
1000 mAhbeacon advertising at1 Hz, the chapter’s arithmetic gives0.576 mAh/dayand about1,736 daysfrom advertising alone. - Raising the rate to
10 Hzimproves responsiveness but cuts the advertising-only estimate to about174 days, so gateway-side smoothing is often the better maintenance choice.
At this point the script has enough design context to be reviewed. The remaining sections test whether the same choices survive quizzes, runtime limits, disconnections, and common implementation mistakes.
- RSSI filtering and zone-based detection: Threshold filtering reduces noise from distant devices before zone classification runs.
- EMA smoothing and proximity detection: Exponential moving average stabilizes noisy RSSI readings so zones do not flicker.
- GATT explorer and service discovery: Enumerating UUIDs maps device capabilities before data access.
- Beacon protocols and indoor positioning: iBeacon and Eddystone formats provide repeatable advertisement structures for location services.
- bleak and cross-platform support: One async Python API can target Windows, macOS, and Linux backends.
18.9 See Also
- BLE Hands-On Labs - Complete project implementations with heart rate monitors and positioning
- BLE Code Examples - Basic Python scanner patterns and GATT concepts
- Bluetooth Security - Understanding secure device pairing
- Bluetooth Applications - Real-world BLE deployment case studies
18.10 Practice Activities
18.11 Start With the Story
A laptop running Python can turn BLE from a black box into a repeatable inspection tool. Scans, filters, UUIDs, GATT reads, notifications, RSSI samples, and exceptions become evidence a team can rerun.
Use this chapter to make automation serve the design review. Write the smallest Bleak script that observes the claim, log the result, and connect the script output back to the device behavior it is meant to prove.
18.12 Continue to the Next Part
Carry this evidence into Python BLE: Runtime Boundaries and Positioning, which begins with Deep Dive: Bleak Role Boundaries and Runtime Limits.
