from kivy.app import App from kivy.uix.widget import Widget from kivy.uix.button import Button from kivy.properties import ( StringProperty,NumericProperty, ) from kivy.clock import Clock import asyncio from bleak import BleakClient,BleakScanner import json STM32_UUID = "00001101-0000-1000-8000-00805F9B34FB" class InfoWidget(Widget): info = StringProperty("") def __init__(self, **kwargs): super(InfoWidget, self).__init__(**kwargs) # asyncio.run(self.discover_devices()) # async def discover_devices(self): # devices = await BleakScanner.discover() # for d in devices: # button = Button(text=f"{d.address}: {d.name}") # button.bind(on_press=lambda instance, d=d: asyncio.run(self.connect_and_receive(d.address))) # self.add_widget(button) # async def connect_and_receive(self, address): # async with BleakClient(address) as client: # if await client.is_connected(): # rawdata = await client.read_gatt_char() # data = rawdata.decode("utf-8") # if data.startswith("[") and data.endswith("]"): # list = json.loads(data) # self.info = "基波频率:" + str(list[0]) + "Hz\n" + \ # "THD:" + str(list[6]) + "%\n" + \ # "基波幅值:" + str(list[1]) + "V\n" + \ # "二次谐波:" + str(list[2]) + "V\n" + \ # "三次谐波:" + str(list[3]) + "V\n" + \ # "四次谐波:" + str(list[4]) + "V\n" + \ # "五次谐波:" + str(list[5]) + "V\n" # else: # self.info = "传输数据出错" # return # else: # print("Failed to connect to device.") def debug(self): for i in range(6): button = Button(text=str(i) + "!!!!!!") self.add_widget(button) class STI2021A(App): def build(self): info = InfoWidget() info.debug() return info if __name__ == '__main__': STI2021A().run()