创建蓝牙连接Kivy

时间:2020-09-21 19:02:10

标签: arduino bluetooth kivy

与Kivy相比,这更是一个基本的OOP问题。我有一个带有4个按钮的应用程序。当我按下一个按钮时,我想初始化一个从笔记本电脑到arduino uno的蓝牙连接,并计划一个每秒发送一次蓝牙信息的函数。

我正在使用以下代码:

class HomeScreen(Screen):
    def OnConnect(self):
        print('Start')
        port = "COM7"
        #connect to bluetooth
        bluetooth = serial.Serial(port, 9600)
        print("Connected to HC-06")
        bluetooth.flushInput()

        #schedule a function that sends tester present to arduino
        Clock.schedule_interval(self.SendData, 1)

    def SendData(self,*args):
        bluetooth.write(b"Boop")

显然,在“ OnConnect”功能外部看不到“蓝牙”。我希望蓝牙对OnConnect和SendData都可见,但是我只想在调用OnConnect时连接到arudino。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

我在以下位置查看了文档

https://kivy.org/doc/stable/api-kivy.clock.html

它说:

如果要安排一个函数使用默认参数进行调用,则可以使用functools.partial python模块:

所以我认为这样的事情应该起作用:


        #schedule a function that sends tester present to arduino
        Clock.schedule_interval(partial(self.SendData, bluetooth), 1)

    def SendData(self, bt):
        bt.write(b"Boop")