-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
133 lines (101 loc) · 5.16 KB
/
Copy pathmain.py
File metadata and controls
133 lines (101 loc) · 5.16 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import simplepyble
if __name__ == "__main__":
adapters = simplepyble.Adapter.get_adapters()
if len(adapters) == 0:
print("No adapters found")
for i, adapter in enumerate(adapters):
print(f"adapter id: {adapter.identifier()}")
print(f"adapter address: {adapter.address()}")
print(f"adapter initialized: {adapter.initialized()}\n")
print(f"connected peripherals: {len(adapter.get_paired_peripherals())}")
print(f"{i+1} adapter(s) found.\n")
adapter = adapters[0]
print("[1] scan")
print("[2] adapter info")
print("[0] exit ")
parameter = int(input(" "))
match parameter:
case 0:
print("goodbye")
exit()
case 1:
print(f"Selected adapter: {adapter.identifier()} {adapter.address()}")
adapter.set_callback_on_scan_start(lambda: print("Scan started."))
adapter.scan_for(10000) # scan for 5 seconds
adapter.set_callback_on_scan_stop(lambda: print("Scan complete."))
peripherals = adapter.scan_get_results() #get peripherals
for i, peripheral in enumerate(peripherals):
print(f"{i} id: {peripheral.identifier()} address: {peripheral.address()}")
#Query the user to pick a peripheral
choice = int(input("peripheral id: "))
peripheral = peripherals[choice]
try:
peripheral.connect()
except TypeError:
print(f"couldn't connect {peripheral}")
except RuntimeError:
print(f"couldn't connect to {peripheral}")
else:
print(f"connecting to {peripheral}")
print("\n")
print(f"{peripheral.identifier()} details:")
print(f"address: {peripheral.address()}")
print(f"address type: {peripheral.address_type()}")
print(f"rssi: {peripheral.rssi()}")
print(f"transmit power: {peripheral.tx_power()}")
print(f"mtu: {peripheral.mtu()}")
print(f"connected: {peripheral.is_connected()}")
print(f"connectable: {peripheral.is_connectable()}")
print(f"manufacturer data: {peripheral.manufacturer_data()}")
#error handling for not compatible methods
try:
peripheral.is_paired()
except TypeError:
print("pairing: N/A")
except RuntimeError:
print("pairing: N/A")
else:
print(f"paired: {peripheral.is_paired()}")
#start service navigation
services = peripheral.services()
service_characteristic_pair = []
print(" ")
print(f"({len(services)}) service(s) found:")
for service in services: #insert into service(s) array
for characteristic in service.characteristics():
service_characteristic_pair.append((service.uuid(), characteristic.uuid()))
print(f"service id: {service.uuid()}")
print(f"({len(service.characteristics())}) characteristics found:")
for characteristic in service.characteristics():
print(f"\tuuid: {characteristic.uuid()}\t")
#checks if the characteristic is readable
try:
peripheral.read(service.uuid(), characteristic.uuid())
except TypeError:
print("\t unreadable")
except RuntimeError:
print("\t unreadable")
else:
print(f"\tdata: {peripheral.read(service.uuid(), characteristic.uuid())}")
print(f"\t({len(characteristic.descriptors())}) descriptor(s) found: ")
for descriptor in characteristic.descriptors():
print(f"\t\tdescriptors: {descriptor.uuid()}")
try:
peripheral.descriptor_read(service.uuid(), characteristic.uuid(), descriptor.uuid())
except TypeError:
print("\t\tunreadable")
except RuntimeError:
print("\t\tunreadable")
else:
print(f"\t\tdata: {peripheral.descriptor_read(service.uuid(), characteristic.uuid(), descriptor.uuid())}")
print("")
print(f"disconnecting from {peripheral.identifier()}")
peripheral.disconnect()
print("disconnected")
case 2:
for i, adapter in enumerate(adapters):
print(f"adapter id: {adapter.identifier()}")
print(f"adapter address: {adapter.address()}")
print(f"adapter initialized: {adapter.initialized()}\n")
print(f"connected peripherals: {len(adapter.get_paired_peripherals())}")
print(f"{i+1} adapter(s) found.\n")