|
@@ -5,6 +5,8 @@ from kivy.uix.gridlayout import GridLayout
|
|
|
from kivy.uix.label import Label
|
|
|
from kivy.uix.textinput import TextInput
|
|
|
from kivy.lang import Builder
|
|
|
+from kivy.clock import Clock
|
|
|
+from kivy.properties import BooleanProperty
|
|
|
import asyncio
|
|
|
from bleak import BleakClient,BleakScanner
|
|
|
import time
|
|
@@ -14,30 +16,36 @@ import time
|
|
|
STM32_UUID = "00001101-0000-1000-8000-00805F9B34FB"
|
|
|
|
|
|
class InfoWidget(Widget):
|
|
|
- # def __init__(self, **kwargs):
|
|
|
- # super(InfoWidget, self).__init__(**kwargs)
|
|
|
+ def __init__(self, **kwargs):
|
|
|
+ super(InfoWidget, self).__init__(**kwargs)
|
|
|
+ self.last_flag = False
|
|
|
+ self.flag = False
|
|
|
+ self.scanning = Label(text="Scanning...",font_size=100,top=self.top)
|
|
|
+ self.devices = None
|
|
|
+
|
|
|
|
|
|
- async def discover_devices(self):
|
|
|
- # scanning = Label(text="Scanning...",font_size=100,top=self.top)
|
|
|
+ def update(self, dt):
|
|
|
+ if self.flag and (not self.last_flag):
|
|
|
+ self.connect()
|
|
|
+ self.last_flag = self.flag
|
|
|
+
|
|
|
+ def start(self):
|
|
|
self.size = (1600,800)
|
|
|
+ self.add_widget(self.scanning)
|
|
|
|
|
|
- devices = await BleakScanner.discover()
|
|
|
- # self.remove_widget(scanning)
|
|
|
- grid00 = GridLayout(cols=1,rows=2,width=self.width,height=self.height / 4,top=self.top)
|
|
|
- grid00.top = self.top
|
|
|
- self.add_widget(grid00)
|
|
|
- grid01 = GridLayout(cols=2,rows=1,width=grid00.width,height=grid00.height / 2)
|
|
|
- grid00.add_widget(grid01)
|
|
|
- grid01.add_widget(Label(text="Device Name:",width=grid00.width / 2,height=grid00.height / 2,font_size=50))
|
|
|
+ def connect(self):
|
|
|
+ self.grid00 = GridLayout(cols=1,rows=2,width=self.width,height=self.height / 4,top=self.top)
|
|
|
+ self.remove_widget(self.scanning)
|
|
|
+ self.grid00.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=grid00.width / 2,height=grid00.height / 2,font_size=40))
|
|
|
- connect_button = Button(text="Connect",width=grid00.width,height=grid00.height / 2,center_x=grid00.center_x,font_size=40)
|
|
|
- Builder.load_string('''
|
|
|
-<Button>:
|
|
|
- id: connect_button
|
|
|
- on_touch_down: root.root.remove_widget(grid00)
|
|
|
- ''')
|
|
|
- grid00.add_widget(connect_button)
|
|
|
+ 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)
|
|
|
|
|
|
# for d in devices:
|
|
|
# if d.name == deviceName:
|
|
@@ -85,14 +93,33 @@ class InfoWidget(Widget):
|
|
|
# button.width = grid.width / 2
|
|
|
# button.height = grid.height / 4
|
|
|
# grid.add_widget(button)
|
|
|
+ async def discover_devices(self):
|
|
|
+ self.devices = await BleakScanner.discover()
|
|
|
+ self.flag = True
|
|
|
+
|
|
|
+ def start_discovery(self, dt):
|
|
|
+ asyncio.create_task(self.discover_devices())
|
|
|
|
|
|
class STI2021A(App):
|
|
|
def build(self):
|
|
|
+ # 在这里设置asyncio事件循环
|
|
|
+ loop = asyncio.get_event_loop()
|
|
|
+ if not loop.is_running():
|
|
|
+ asyncio.set_event_loop(loop)
|
|
|
+
|
|
|
info = InfoWidget()
|
|
|
- asyncio.run(info.discover_devices())
|
|
|
- # info.debug()
|
|
|
+ info.start()
|
|
|
+ Clock.schedule_once(info.start_discovery, 1)
|
|
|
+ Clock.schedule_interval(info.update, 1)
|
|
|
return info
|
|
|
|
|
|
+ def run(self):
|
|
|
+ # 获取当前的事件循环
|
|
|
+ loop = asyncio.get_event_loop()
|
|
|
+ # 运行应用程序,直到完成
|
|
|
+ loop.run_until_complete(self.async_run())
|
|
|
+ loop.close()
|
|
|
+
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
STI2021A().run()
|