-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscan_tool.py
executable file
·53 lines (41 loc) · 1.48 KB
/
scan_tool.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
#!/usr/bin/env python3
'''
Scanner tool for Brifit Bluetooth LE thermometer/hygrometer devices.
Shows the characteristics of the found devices
'''
import sys
from bluepy.btle import Scanner, DefaultDelegate
import tb_protocol
class ScanDelegate(DefaultDelegate):
def __init__(self):
DefaultDelegate.__init__(self)
def handleDiscovery(self, dev, isNewDev, isNewData):
if isNewDev:
pass
if True or isNewData:
#logger.debug('discobvery:' + dev.addr)
complete_name=dev.getValueText(0x09)
manufact_data=dev.getValueText(0xff)
if complete_name is None or manufact_data is None:
return
bvalue=bytes.fromhex(manufact_data)
if len(bvalue)!=20 or complete_name!='ThermoBeacon':
return
data = tb_protocol.TBAdvData(bvalue[0]+(bvalue[1]<<8), bvalue[2:])
#print(manufact_data)
print('MAC [{0}], T= {1:5.2f}\xb0C, H = {2:3.2f}%, Button:{4}, Battery : {5:02.0f}%, UpTime = {3:.0f}s'.\
format(dev.addr, data.tmp, data.hum, data.upt, 'On ' if data.btn else 'Off', data.btr))
scanDelegate = ScanDelegate()
scanner = Scanner().withDelegate(scanDelegate)
while True:
try:
scanner.clear()
scanner.start()
scanner.process(20)
scanner.stop()
except Exception as exc:
#print(str(exc))
pass
except KeyboardInterrupt:
print('\nInterrupted!')
sys.exit(0)