Kivy 和使用蓝牙发现设备

2023-03-21移动开发问题
44

本文介绍了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?

推荐答案

你错过了 android.broadcast 模块中的 android.broadcast" rel="nofollow">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 和使用蓝牙发现设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

从 Documents 目录存储和读取文件 iOS 5
Storing and reading files from Documents directory iOS 5(从 Documents 目录存储和读取文件 iOS 5)...
2024-08-12 移动开发问题
9

如何在使用 cocos2d 的 iphone 应用程序中使用 MYSQL 数据库连接?
How can i use MYSQL database connection in iphone application useing cocos2d?(如何在使用 cocos2d 的 iphone 应用程序中使用 MYSQL 数据库连接?)...
2024-08-12 移动开发问题
5

在 cocos2d 中平滑拖动一个 Sprite - iPhone
Smoothly drag a Sprite in cocos2d - iPhone(在 cocos2d 中平滑拖动一个 Sprite - iPhone)...
2024-08-12 移动开发问题
10

CCScrollView 滚动和触摸事件永远不会触发
CCScrollView scroll and touch events never firing(CCScrollView 滚动和触摸事件永远不会触发)...
2024-08-12 移动开发问题
1

使用 OpenGLES 的抗锯齿去除绳索的锯齿状边缘
removing jagged edges of my ropes using antialiasing of OpenGLES(使用 OpenGLES 的抗锯齿去除绳索的锯齿状边缘)...
2024-08-12 移动开发问题
34

cocos2d 在场景之间移动
cocos2d Moving between scene(cocos2d 在场景之间移动)...
2024-08-12 移动开发问题
2