問題描述
我在這里看到過(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 和使用藍牙發現設備的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!