Kivy 和使用蓝牙发现设备 [英] Kivy and discovering devices with bluetooth

查看:18
本文介绍了Kivy 和使用蓝牙发现设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里看到过(https://gist.github.com/tito/7432757) 如何使用 pyjnius 访问 java 类,使用 kivy 通过蓝牙连接.我要做的是发现新设备并使用 sdp 不安全地连接到它们.我不确定如何在 kivy 中接收 startDiscovery() 的结果.在java中你必须使用广播接收器.我是否也必须使用 pyjnius 从 android 访问广播接收器?

I have seen here (https://gist.github.com/tito/7432757) how to use pyjnius to access the java classes using kivy to connect via bluetooth. What I am trying to do is to discover new devices and connect to them insecurely using sdp. I'm not sure how to receive the results of startDiscovery() in kivy. In java you have to use a broadcast receiver. Do I have to use the pyjnius to access the broadcast receiver from android as well?

推荐答案

你错过了 BroadcastReceiver :) 它完全符合您的需要,它是 Java/Pyjnius 中的一个实现这允许您在 Python 中接收结果.

You've missed the BroadcastReceiver in the Python-for-android / android.broadcast module :) It does exactly what you need, it's an implementation in Java / Pyjnius that allows you to receive the result in Python.

注意,需要监听的动作需要小写,不带ACTION_前缀.

Note that the actions you need to listen need to be written in lowercase, without the ACTION_ prefix.

您的应用程序的模型可能如下所示:

A mockup for your application can look like this:

class TestApp(App):

    def build(self):
        self.br = BroadcastReceiver(
            self.on_broadcast, actions=['found'])
        self.br.start()

    def on_broadcast(self, context, intent):
        # called when a device in found
        pass

    # Don't forget to stop and restart the receiver when the app is going
    # to pause / resume mode

    def on_pause(self):
        self.br.stop()
        return True

    def on_resume(self):
        self.br.start()

这篇关于Kivy 和使用蓝牙发现设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆