| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- import asyncio
- from bleak import BleakScanner
- from bleak import BleakClient
- target_name = "Long name works now"
- SERVICE_UUID= "d86aecf2-d87d-489f-b664-b02de82b2fc0"
- CHARACTERISTIC_UUID= "d86aecf2-d87d-489f-b664-b02de82b2fc0"
- async def connect():
- target_address = None
- devices = await BleakScanner.discover()
- for d in devices:
- print(d)
- if target_name == d.name:
- target_address = d.address
- print("found target {} bluetooth device with address {} ".format(target_name,target_address))
- break
- else:
- print("could not find device")
- return target_address
- async def main():
- print("yes")
- target_address = await connect()
- print("yes1")
- if target_address is not None:
- print("yes2")
- async with BleakClient(target_address) as client:
- print("yes3")
- print(f"Connected: {client.is_connected}")
- while 1:
- #text = input()
- #if text == "quit":
- # break
- #await client.write_gatt_char(CHARACTERISTIC_UUID, bytes(text, 'UTF-8'), response=True)
- try:
- data = await client.read_gatt_char(CHARACTERISTIC_UUID)
- datastr = data.decode('utf-8') #convert byte to str
- print("data: {}".format(datastr))
- except Exception:
- print("failed to get data, restarting connection.")
- asyncio.run(main())
|