From 96407ad763cb8fcf6e8a9eb32b7a17046720787f Mon Sep 17 00:00:00 2001 From: Olivier Martin Date: Wed, 7 Feb 2024 17:43:21 +0100 Subject: [PATCH] gattlib-py/examples/ble_scan: Fix example --- gattlib-py/examples/ble_scan/ble_scan.py | 30 ++++++++++++++---------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/gattlib-py/examples/ble_scan/ble_scan.py b/gattlib-py/examples/ble_scan/ble_scan.py index 1746a29..ea081e0 100755 --- a/gattlib-py/examples/ble_scan/ble_scan.py +++ b/gattlib-py/examples/ble_scan/ble_scan.py @@ -8,40 +8,44 @@ import argparse import threading +import time from gattlib import adapter parser = argparse.ArgumentParser(description='Gattlib BLE scan example') +parser.add_argument('--duration', default=30, type=int, help='Duration of the BLE scanning') args = parser.parse_args() # We only use a lock to not mixed printing statements of various devices lock = threading.Lock() - def connect_ble_device(device): - device.connect() - lock.acquire() + try: + print("---------------------------------") + print(f"Found BLE Device {device.mac_address}. Connection tentative...") + device.connect() - print("---------------------------------") - print(f"Found BLE Device {device.mac_address}. Connection tentative...") - device.discover() - - for key, val in device.characteristics.items(): - print("- GATTCharacteristic: 0x%x" % key) + device.discover() + for key, val in device.characteristics.items(): + print("- GATTCharacteristic: 0x%x" % key) + device.disconnect() + except Exception as e: + print("EXCEPTION: %s:%s", type(e), str(e)) lock.release() - device.disconnect() def on_discovered_ble_device(device, user_data): threading.Thread(target=connect_ble_device, args=(device,)).start() - # Use default adapter default_adapter = adapter.Adapter() -# Scan for 30 seconds +# Scan for 'args.duration' seconds default_adapter.open() -default_adapter.scan_enable(on_discovered_ble_device, 30) +default_adapter.scan_enable(on_discovered_ble_device, args.duration) + +# Because scan_enable() is not blocking, we need to wait for the same duration +time.sleep(args.duration)