diff --git a/examples/ble_scan/ble_scan.py b/examples/ble_scan/ble_scan.py deleted file mode 100755 index 6da5d8d..0000000 --- a/examples/ble_scan/ble_scan.py +++ /dev/null @@ -1,20 +0,0 @@ -#!/usr/bin/env python3 - -# export LD_LIBRARY_PATH=/home/olivier/dev/gattlib/build/dbus/:$LD_LIBRARY_PATH - -from gattlib import adapter - -adapters = adapter.Adapter.list() -print("BLE Adapters: %s" % adapters) - - -def on_discovered_device(device): - print("Discovered '%s'" % device) - # device.connect() - # device.discover() - - -default_adapter = adapter.Adapter() - -default_adapter.open() -default_adapter.scan_enable(on_discovered_device, 10) diff --git a/gattlib-py/examples/ble_scan/ble_scan.py b/gattlib-py/examples/ble_scan/ble_scan.py new file mode 100755 index 0000000..cbf854a --- /dev/null +++ b/gattlib-py/examples/ble_scan/ble_scan.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 + +import argparse +import threading + +from gattlib import adapter + +parser = argparse.ArgumentParser(description='Gattlib BLE scan example') +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() + + print("---------------------------------") + print("Found BLE Device %s" % device.id) + device.discover() + + for key, val in device.characteristics.items(): + print("- GATTCharacteristic: 0x%x" % key) + + lock.release() + + device.disconnect() + + +def on_discovered_ble_device(device): + threading.Thread(target=connect_ble_device, args=(device,)).start() + + +# Use default adapter +default_adapter = adapter.Adapter() + +# Scan for 30 seconds +default_adapter.open() +default_adapter.scan_enable(on_discovered_ble_device, 30)