| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- import asyncio
- from bleak import BleakScanner
- from bleak import BleakClient
- import time
- target_name = "Long name works now"
- global target_address
- target_address = None
- SERVICE_UUID= "d86aecf2-d87d-489f-b664-b02de82b2fc0"
- CHARACTERISTIC_UUID= "d86aecf2-d87d-489f-b664-b02de82b2fc0"
- async def connect():
- 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))
- return target_address
- break
- else:
- print("could not find device")
- async def getData():
- target_address = await connect()
- if target_address is not None:
- async with BleakClient(target_address) as client:
- print(f"Connected: {client.is_connected}")
- while 1:
- try:
- data = await client.read_gatt_char(CHARACTERISTIC_UUID)
- datastr = data.decode('utf-8') #convert byte to str
- dataarr = convertToNumbers(datastr)
- print("time: {} ||| numbers: {}".format(time.time(), dataarr))
- except Exception:
- print("failed to get data, restarting connection.")
- def convertToNumbers(stringlist):
- dlist = stringlist.split(",")
- dlist = dlist[0:-1]
- intlist = []
- for string in dlist:
- intlist.append(float(string))
- return intlist
|