forked from rani-i/Mi365Locker-RASPI
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscooter-scan.py
More file actions
114 lines (84 loc) · 2.8 KB
/
scooter-scan.py
File metadata and controls
114 lines (84 loc) · 2.8 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
from bluepy import btle
from bluepy.btle import Scanner, DefaultDelegate
from bluepy.btle import BTLEDisconnectError
from bluepy.btle import BTLEGattError
import codecs
import signal
import sys
import os
# CHARACTERISTIC
WRITE_UUID = "6e400002-b5a3-f393-e0a9-e50e24dcca9e"
# COMMANDS
LOCK = "55aa032003700168ff"
UNLOCK = "55aa032003710167ff"
# CONSTANTS
TIMEOUT_LENGTH = 3
FILE_NAME = "scootersAddr.txt"
COMMAND = ""
if sys.argv[2] == "lock":
COMMAND = LOCK
elif sys.argv[2] == "unlock":
COMMAND = UNLOCK
else:
raise Exception('Command not recognised')
def timeout_handler(signum, timeout_handler):
raise TimeoutError
signal.signal(signal.SIGALRM, timeout_handler)
class ScanDelegate(DefaultDelegate):
def __init__(self):
DefaultDelegate.__init__(self)
scanner = Scanner().withDelegate(ScanDelegate())
devices = scanner.scan(2)
def add_addr_to_known(dev_addr):
file_exists = os.path.exists('./' + FILE_NAME)
if file_exists:
with open(FILE_NAME) as file:
addresses = [line.strip() for line in file]
for addr in addresses:
if addr == dev_addr:
return None
f = open(FILE_NAME, "a")
print(dev_addr, file=f)
f.close()
def write_command(dev, command):
signal.alarm(TIMEOUT_LENGTH)
peri = btle.Peripheral(dev)
characteristics = peri.getCharacteristics(uuid=WRITE_UUID)[0]
characteristics.write(codecs.decode(command, 'hex'))
peri.disconnect()
print("Success!")
add_addr_to_known(dev.addr)
def write_devices(devs, command):
for dev in devs:
try:
print("Attempting to send command to device ", dev.addr, dev.getScanData())
write_command(dev, command)
except (BTLEDisconnectError, BTLEGattError, TimeoutError):
print("Couldn't connect")
def get_known_addr(devs):
file_exists = os.path.exists('./' + FILE_NAME)
known_devices = []
if file_exists:
print('file exists')
with open(FILE_NAME) as file:
known_addr = [line.strip() for line in file]
print('[%s]' % ', '.join(map(str, known_addr)))
for dev in devs:
for addr in known_addr:
if dev.addr == addr:
known_devices.append(dev)
else:
raise Exception('No saved addresses in file ' + FILE_NAME)
return known_devices
if sys.argv[1] == "scan":
print("Scanning ", len(devices), " device/s in bluetooth area")
write_devices(devices, COMMAND)
elif sys.argv[1] == "saved":
knownDevices = get_known_addr(devices)
if knownDevices:
print("Scanning ", len(knownDevices), " device/s in bluetooth area")
write_devices(knownDevices, COMMAND)
else:
raise Exception('Could not find any known devices in area')
else:
raise Exception('incorrect arguments')