mirror of https://github.com/labapart/gattlib
gattlib-py: Added BLE Scan sample
parent
40eec80170
commit
bca923366e
|
@ -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)
|
|
@ -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)
|
Loading…
Reference in New Issue