-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.py
executable file
·46 lines (33 loc) · 1.36 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
#!/usr/bin/env python3
import asyncio
import logging
import uuid
from bleak import BleakScanner, BleakClient
# Enable debug output
# logging.basicConfig(level=logging.DEBUG)
DEVICE_NAME = "m5-stack"
SERVICE_UUID = uuid.UUID("4fafc201-1fb5-459e-8fcc-c5c9c331914b")
CHAR_UUID = uuid.UUID("beb5483e-36e1-4688-b7f5-ea07361b26a8")
async def run(loop):
print("Searching devices...")
devices = await BleakScanner.discover()
device = list(filter(lambda d: d.name == DEVICE_NAME, devices))
if len(device) == 0:
raise RuntimeError(f"Failed to find a device name '{DEVICE_NAME}'")
address = device[0].address
print(f"Connecting to the device... (address: {address})")
async with BleakClient(address, loop=loop) as client:
print("Message from the device...")
value = await client.read_gatt_char(CHAR_UUID)
print(value.decode())
print("Sending message to the device...")
message = bytearray(b"hi!")
await client.write_gatt_char(CHAR_UUID, message, True)
def callback(sender, data):
print(f"Received: {data}")
print("Subscribing to characteristic changes...")
await client.start_notify(CHAR_UUID, callback)
print("Waiting 60 seconds to receive data from the device...")
await asyncio.sleep(60)
loop = asyncio.get_event_loop()
loop.run_until_complete(run(loop))