-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluetest.py
98 lines (87 loc) · 4.11 KB
/
bluetest.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
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
from bluepy.btle import Scanner, DefaultDelegate, Peripheral, BTLEDisconnectError
from companyInfo import companyData
class ScanDelegate(DefaultDelegate):
def __init__(self):
DefaultDelegate.__init__(self)
def handleDiscovery(self, dev, isNewDev, isNewData):
if dev.addrType == "random":
return
if isNewDev:
print(f"Discovered device {dev.addr}, {dev.addrType}, {dev.rssi}")
elif isNewData:
print(f"Received new data from {dev.addr}")
def findCompany(companyId):
for company in companyData:
if company["Decimal"] == companyId:
return company["Company"]
return "Unknown"
def getPeripheral(device):
try:
if device.addrType == "random":
print("Random address, skipping")
return None
p = Peripheral(device.addr, device.addrType)
# try:
# p.connect(device.addr)
# except BTLEDisconnectError as e:
# print(f"Failed to connect to {device.addr} with error: {e}")
return p
except Exception as e:
print(f"Failed to connect to {device.addr} with error: {e}")
return None
scanner = Scanner().withDelegate(ScanDelegate())
devices = scanner.scan(10.0)
for dev in devices:
if dev.addrType == "random":
continue
p = getPeripheral(dev)
print(f"Device {dev.addr} ({dev.addrType}), RSSI={dev.rssi} dB")
for (adtype, desc, value) in dev.getScanData():
if adtype == 255:
print(f"{str(adtype).ljust(3)}: {desc} = {value}")
print(f" Company Name: {findCompany(value)}")
else:
print(f"{str(adtype).ljust(3)}: {desc} = {value}")
if p:
try:
services = p.getServices()
print(f"Services ({len(services)}):")
for service in services:
try:
print(f" Service: {service.uuid}")
characteristics = service.getCharacteristics()
print(f" Characteristics ({len(characteristics)}):")
for characteristic in characteristics:
try:
print(f" Characteristic: {characteristic.uuid}")
if characteristic.supportsRead():
print(f" Value: {characteristic.read()}")
else:
print(f" Value: (Not readable)")
print(f" Properties: {characteristic.propertiesToString()}")
try:
descriptors = characteristic.getDescriptors()
if len(descriptors) > 0:
print(" Descriptors:")
for descriptor in descriptors:
print(f" Descriptor: {descriptor.uuid}")
try:
print(f" Value: {descriptor.read()}")
except Exception as e:
print(f" Value: (Not readable)")
else:
print(" No descriptors available")
except Exception as e:
print(f" └──> Failed with error: {e}")
except Exception as e:
print(f" └──> Failed with error: {e}")
except Exception as e:
print(f" └──> Failed with error: {e}")
p.disconnect()
except BTLEDisconnectError as e:
print(f"-------ERROR DEVICE DISCONNECTED UNEXPECTEDLY-------")
except Exception as e:
print(f"-------ERROR DEVICE FAILED WITH ERROR: {e}-------")
else:
print("No services available")
print("----------------------------------------")