from kivy.app import App from kivy.uix.widget import Widget from kivy.uix.button import Button from kivy.uix.gridlayout import GridLayout from kivy.uix.label import Label from kivy.uix.textinput import TextInput from kivy.clock import Clock import asyncio from bleak import BleakClient,BleakScanner import json STM32_UUID = "00001101-0000-1000-8000-00805F9B34FB" class MainWidget(Widget): def __init__(self, **kwargs): super().__init__(**kwargs) self.size = (1600,800) self.last_flag = False self.flag = False self.scanning = Label(text="Scanning...",font_size=100,top=self.top,center_x = self.center_x) def update(self, dt): if self.flag and (not self.last_flag): self.connect() self.last_flag = self.flag def start(self): self.add_widget(self.scanning) # def start_scan(self, dt): # asyncio.run(self.scan) async def scan(self): self.devices = await BleakScanner.discover() self.flag = True def connect(self): self.remove_widget(self.scanning) self.grid00 = GridLayout(cols=1,rows=2,width=self.width,height=self.height / 4,top=self.top) self.add_widget(self.grid00) grid01 = GridLayout(cols=2,rows=1,width=self.grid00.width,height=self.grid00.height / 2) self.grid00.add_widget(grid01) grid01.add_widget(Label(text="Device Name:",width=self.grid00.width / 2,height=self.grid00.height / 2,font_size=50)) deviceName = "HC-06" grid01.add_widget(TextInput(text=deviceName,width=self.grid00.width / 2,height=self.grid00.height / 2,font_size=40)) connect_button = Button(text="Connect",width=self.grid00.width,height=self.grid00.height / 2,center_x=self.grid00.center_x,font_size=40) self.grid00.add_widget(connect_button) if connect_button.state == "down": self.clear_widgets(self.grid00) class STI2021A(App): def __init__(self): super().__init__() self.main_widget = MainWidget() def build(self): self.main_widget.start() Clock.schedule_interval(self.main_widget.update, 1) return self.main_widget async def scan(self): await self.main_widget.scan() async def main(app): await asyncio.gather(app.async_run("asyncio"), app.scan()) if __name__ == "__main__": app = STI2021A() asyncio.run(main(app))