Introduction
In this tutorial, you'll learn how to create a Bluetooth tracker proximity detection system using Python and Bluetooth Low Energy (BLE) technology. This hands-on project will help you understand how devices like Apple AirTags and budget Bluetooth trackers work at the technical level, allowing you to build your own proximity detection system that can monitor when devices come within range of each other.
By the end of this tutorial, you'll have a working Python application that can detect nearby BLE devices and measure their signal strength, similar to how commercial trackers like AirTags operate.
Prerequisites
- Python 3.7 or higher installed on your system
- Bluetooth 5.0 or higher capable device
- Linux or macOS system (Windows support is limited for BLE)
- Basic understanding of Python programming
- Required Python packages:
bleak,asyncio,time
Step-by-Step Instructions
Step 1: Install Required Python Packages
First, you'll need to install the necessary Python packages for BLE communication. The bleak library is a cross-platform Bluetooth Low Energy (BLE) client for Python.
pip install bleak asyncio
Why this step: The bleak library provides the foundation for communicating with BLE devices across different operating systems, which is essential for building a portable tracker detection system.
Step 2: Create a Basic BLE Scanner
Now create a Python script that will scan for nearby BLE devices. This mimics how AirTags and similar trackers broadcast their presence.
import asyncio
from bleak import BleakScanner
async def scan_devices():
devices = await BleakScanner.discover()
for device in devices:
print(f"Device: {device.name} - {device.address}")
asyncio.run(scan_devices())
Why this step: This basic scanner demonstrates how BLE devices broadcast their presence. The scanner will detect all nearby BLE devices, including trackers, phones, and other IoT devices.
Step 3: Implement Signal Strength Monitoring
Next, we'll enhance our scanner to monitor signal strength, which is crucial for proximity detection. The RSSI (Received Signal Strength Indicator) value tells us how strong the signal is from a device.
import asyncio
from bleak import BleakScanner
async def scan_with_rssi():
devices = await BleakScanner.discover()
for device in devices:
print(f"Device: {device.name} - {device.address} - RSSI: {device.rssi} dBm")
asyncio.run(scan_with_rssi())
Why this step: RSSI values are critical for determining proximity. A stronger signal (closer device) will have a higher RSSI value, while a weaker signal (farther device) will have a lower RSSI value.
Step 4: Create a Proximity Detection System
Now we'll build a system that can detect when a specific device comes within a certain proximity range. This simulates how AirTags might trigger notifications.
import asyncio
from bleak import BleakScanner
import time
# Define proximity thresholds
PROXIMITY_THRESHOLD = -70 # dBm
async def monitor_device(device_address, device_name):
print(f"Monitoring {device_name} at {device_address}")
while True:
devices = await BleakScanner.discover()
for device in devices:
if device.address == device_address:
rssi = device.rssi
print(f"{device_name}: RSSI {rssi} dBm")
if rssi > PROXIMITY_THRESHOLD:
print(f"\n🚨 ALERT: {device_name} is within proximity range!")
print(f"Signal strength: {rssi} dBm\n")
break
await asyncio.sleep(2) # Check every 2 seconds
# Replace with actual device address you want to monitor
asyncio.run(monitor_device("AA:BB:CC:DD:EE:FF", "MyTracker"))
Why this step: This creates a real-time monitoring system that can detect when a device enters a specific proximity range, similar to how commercial trackers alert users when they're near their lost items.
Step 5: Add Device Filtering and Logging
Enhance your system to filter specific devices and log detection events for analysis.
import asyncio
from bleak import BleakScanner
import time
import json
from datetime import datetime
# Device database
TRACKER_DATABASE = {
"AA:BB:CC:DD:EE:FF": "AirTag",
"11:22:33:44:55:66": "Budget Tracker",
"77:88:99:AA:BB:CC": "Phone"
}
# Log file
LOG_FILE = "tracker_log.json"
async def enhanced_monitoring():
print("Starting enhanced proximity monitoring...")
while True:
devices = await BleakScanner.discover()
for device in devices:
if device.address in TRACKER_DATABASE:
tracker_name = TRACKER_DATABASE[device.address]
rssi = device.rssi
# Check proximity
if rssi > -70: # Adjust threshold as needed
timestamp = datetime.now().isoformat()
log_entry = {
"timestamp": timestamp,
"device": tracker_name,
"address": device.address,
"rssi": rssi,
"status": "IN_RANGE"
}
# Write to log file
try:
with open(LOG_FILE, 'r') as f:
log_data = json.load(f)
except FileNotFoundError:
log_data = []
log_data.append(log_entry)
with open(LOG_FILE, 'w') as f:
json.dump(log_data, f, indent=2)
print(f"🚨 {tracker_name} ({device.address}) detected at {rssi} dBm")
await asyncio.sleep(3)
asyncio.run(enhanced_monitoring())
Why this step: This adds real-world functionality by filtering specific devices and logging events, which is essential for practical tracker applications. It also demonstrates how commercial systems might store and analyze tracking data.
Step 6: Test Your System
Run your proximity detection system and test it with actual devices. Place your tracker at different distances from your computer to observe how RSSI values change.
python tracker_monitor.py
Why this step: Testing validates that your system works correctly and helps you understand how different physical environments affect signal strength, which explains why not all Bluetooth range is created equal as mentioned in the article.
Summary
This tutorial demonstrated how to build a Bluetooth proximity detection system using Python and the bleak library. You learned how to:
- Scan for nearby BLE devices
- Monitor signal strength (RSSI) values
- Implement proximity detection logic
- Filter specific devices and log detection events
The system you built mirrors the core technology used by commercial trackers like Apple AirTags and budget alternatives. Understanding these principles helps explain why different trackers perform differently in real-world conditions, as signal strength, broadcasting frequency, and antenna design all impact performance.
By modifying the proximity thresholds and adding additional features like notification systems or database integration, you can extend this system to create a fully functional personal tracking solution.



