-
Notifications
You must be signed in to change notification settings - Fork 0
/
desktop_app.py
57 lines (48 loc) · 1.71 KB
/
desktop_app.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
import asyncio
import time
from bleak import BleakScanner, BleakClient
from bleak.backends._manufacturers import MANUFACTURERS
async def scan_for_devices():
devices = await BleakScanner.discover()
for d in devices:
print('---')
print('Name:\t', d.name)
print('Address:\t',d.address)
print('RSSI:\t', d.rssi)
print('UUIDS:')
for uuid in d.metadata['uuids']:
print('\t'+str(uuid))
if "manufacturer_data" in d.metadata:
ks = list(d.metadata["manufacturer_data"].keys())
if len(ks):
mf = MANUFACTURERS.get(ks[0], MANUFACTURERS.get(0xFFFF))
value = d.metadata["manufacturer_data"].get(
ks[0], MANUFACTURERS.get(0xFFFF)
)
# TODO: Evaluate how to interpret the value of the company identifier...
print("{0} ({1})".format(mf, value))
ADDRESS = "C5:DF:52:61:6F:19"
#
async def run(address):
client = BleakClient(address)
try:
await client.connect()
print("Connceted!")
rx_char = client.services.get_characteristic("8ba86974-935c-447c-91ad-bdcbad575f31")
while True:
color = input("input color hex:")
if len(color) != 6:
print('invalid hex length')
continue
try:
hexcode = int(color, 16)
await client.write_gatt_char(rx_char, color.encode('utf-8'))
except ValueError:
print("invalid hex string")
time.sleep(1)
except Exception as e:
print(e)
finally:
await client.disconnect()
loop = asyncio.get_event_loop()
loop.run_until_complete(run(ADDRESS))