-
Notifications
You must be signed in to change notification settings - Fork 6
/
simplepyble_test.py
45 lines (33 loc) · 1.54 KB
/
simplepyble_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import simplepyble
if __name__ == "__main__":
adapters = simplepyble.Adapter.get_adapters()
if len(adapters) == 0:
print("No adapters found")
# Query the user to pick an adapter
print("Please select an adapter:")
for i, adapter in enumerate(adapters):
print(f"{i}: {adapter.identifier()} [{adapter.address()}]")
choice = int(input("Enter choice: "))
adapter = adapters[choice]
print(f"Selected adapter: {adapter.identifier()} [{adapter.address()}]")
adapter.set_callback_on_scan_start(lambda: print("Scan started."))
adapter.set_callback_on_scan_stop(lambda: print("Scan complete."))
adapter.set_callback_on_scan_found(lambda peripheral: print(f"Found {peripheral.identifier()} [{peripheral.address()}]"))
# Scan for 5 seconds
adapter.scan_for(5000)
peripherals = adapter.scan_get_results()
# Query the user to pick a peripheral
print("Please select a peripheral:")
for i, peripheral in enumerate(peripherals):
print(f"{i}: {peripheral.identifier()} [{peripheral.address()}]")
choice = int(input("Enter choice: "))
peripheral = peripherals[choice]
print(f"Connecting to: {peripheral.identifier()} [{peripheral.address()}]")
peripheral.connect()
print("Successfully connected, listing services...")
services = peripheral.services()
for service in services:
print(f"Service: {service.uuid()}")
for characteristic in service.characteristics():
print(f" Characteristic: {characteristic.uuid()}")
peripheral.disconnect()