奇异果和发现设备与蓝牙 [英] Kivy and discovering devices with bluetooth

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

问题描述

我在这里看到了( https://gist.github.com/tito/7432757 )如何使用pyjnius使用kivy通过蓝牙连接来访问Java类.我正在尝试做的是发现新设备并使用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?

推荐答案

您已经错过了

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()

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

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