forked from alpha-hacks/alpha-bluetooth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
46 lines (36 loc) · 1.11 KB
/
main.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
import bluetooth
from alpha_1s import Command
def main():
msg = message(b'\x18', [b'\x00'])
print(msg)
bd_addr = discover()
if bd_addr:
port = 6
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((bd_addr, port))
print('Connected')
sock.settimeout(60.0)
sock.send(msg)
print('Sent data')
response = sock.recv(1024)
print(Command().get(response))
sock.close()
def message(command, parameters):
header = b'\xFB\xBF'
end = b'\xED'
parameter = b''.join(parameters)
# len(header + length + command +parameters + check)
length = bytes([len(parameters) + 5])
data = [command, length]
data.extend(parameters)
check = bytes([sum(ord(x) for x in data)])
return header+length+command+parameter+check+end
def discover():
print("searching ...")
nearby_devices = bluetooth.discover_devices(lookup_names=True)
print("found %d devices" % len(nearby_devices))
for addr, name in nearby_devices:
if name == "ALPHA 1S":
return addr
if __name__ == '__main__':
main()