Python provides libraries for interacting with BLE devices. One such library is bleak
, which is a Bluetooth Low Energy (BLE) central module library with a focus on asyncio and Python 3.
Here's an example of how you can use the bleak
library to scan for a device with the name "ESP32_BLE":
pythonimport asyncio
from bleak import BleakScanner
target_device = "ESP32_BLE"
def detection_callback(device, advertisement_data):
device_name = device.name if device.name else "Unknown"
print(f"BLE device detected: {device_name} ({device.address})")
async def scan_for_device():
scanner = BleakScanner()
scanner.register_detection_callback(detection_callback)
devices = await scanner.discover()
for device in devices:
if device.name == target_device:
print(f"Target device detected: {device.name} ({device.address})")
# You can do additional actions here like connecting to the device.
loop = asyncio.get_event_loop()
loop.run_until_complete(scan_for_device())
This script creates a BLE scanner using the bleak
library. Whenever a device is detected, it calls the detection_callback
function. If the detected device's name is "ESP32_BLE", it prints a message to the console. You can replace this action with whatever you want to do when the specific device is detected.
Please note that the bleak
library is not a standard Python library, so you will need to install it before running the script. You can do this using pip:
pip install bleak
Also, keep in mind that the user running this script might need certain permissions to access BLE devices. For example, on Linux, the user might need to be in the bluetooth
group, and on macOS, the script might need to be codesigned.
If you want to endlessly scan for the device, you can modify the above script to run inside an infinite loop. However, you probably want to add some delay between scans to prevent excessive resource use.
Here's how you can adjust the script:
pythonimport asyncio
from bleak import BleakScanner
import time
target_device = "ESP32_BLE"
def detection_callback(device, advertisement_data):
device_name = device.name if device.name else "Unknown"
print(f"BLE device detected: {device_name} ({device.address})")
async def scan_for_device():
scanner = BleakScanner()
scanner.register_detection_callback(detection_callback)
while True:
devices = await scanner.discover()
for device in devices:
if device.name == target_device:
print(f"Target device detected: {device.name} ({device.address})")
# You can do additional actions here like connecting to the device.
time.sleep(5) # sleep for 5 seconds before the next scan
loop = asyncio.get_event_loop()
loop.run_until_complete(scan_for_device())
In this script, the while True:
loop causes the scanner to continuously scan for devices. After each scan, the script waits for 5 seconds before starting the next scan. You can adjust this delay to suit your needs.
Remember that endlessly running scans could lead to excessive resource use, so make sure to monitor your system's performance and adjust the delay or add some mechanism to stop the scan when needed.
You're welcome! I'm glad to hear that the code is working perfectly for you. If you have any other questions or need further assistance with Python, BLE, or any other topic, don't hesitate to ask. Happy coding!