简体   繁体   English

在跨平台的 Windows/Android Kivy 应用程序中使用 BLE?

[英]Use BLE in a cross-platform Windows/Android Kivy app?

I'm working on a cross platform app with Kivy (for Windows and Android targets) and I need to use Bluetooth Low Energy (to communicate with a ESP32 BLE Server).我正在使用 Kivy(针对 Windows 和 Android 目标)开发跨平台应用程序,我需要使用低功耗蓝牙(与 ESP32 BLE 服务器通信)。 I tried using Bleak to handle BLE connections but importing the Bleak module made my app crash.我尝试使用 Bleak 来处理 BLE 连接,但导入 Bleak 模块使我的应用程序崩溃。 I use threading.Thread to split the BLE and the Kivy app but I still have a problem since I have to import both Kivy and Bleak modules in my main python file.我使用threading.Thread来拆分 BLE 和 Kivy 应用程序,但我仍然遇到问题,因为我必须在我的主 python 文件中同时导入 Kivy 和 Bleak 模块。

My main app :我的主要应用程序:

# coding: UTF-8
#!/usr/bin/python3
import threading
import asyncio
import time
from queue import Queue
from interface_kivy import MainApp # Python file with the Kivy import
from ble_kivy import ble_kivy_interface # Python file with the Bleak import

async def main():
    kivy_thread = threading.Thread(group=None, target=MainApp().run(), name='Service kivy IHM')
    kivy_thread.daemon = True
    kivy_thread.start()
    
    my_queue = MainApp().getIHMqueue()
    print(f"{my_queue}")
    kivy_thread.join()
        

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

Output when from ble_kivy import ble_kivy_interface is commented :当输出from ble_kivy import ble_kivy_interface评论:

[INFO   ] [Logger      ] Record log in C:\Users\Fabien\.kivy\logs\kivy_21-06-24_42.txt
[INFO   ] [deps        ] Successfully imported "kivy_deps.angle" 0.3.0
[INFO   ] [deps        ] Successfully imported "kivy_deps.glew" 0.3.0
[INFO   ] [deps        ] Successfully imported "kivy_deps.sdl2" 0.3.1
[INFO   ] [Kivy        ] v2.0.0
[INFO   ] [Kivy        ] Installed at "c:\_dev\venv38\lib\site-packages\kivy\__init__.py"
[INFO   ] [Python      ] v3.8.10 (tags/v3.8.10:3d8993a, May  3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)]
[INFO   ] [Python      ] Interpreter at "c:\_dev\venv38\Scripts\python.exe"
[INFO   ] [Factory     ] 186 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_pil (img_ffpyplayer ignored)
[INFO   ] [Window      ] Provider: sdl2
[INFO   ] [GL          ] Using the "OpenGL" graphics system
[INFO   ] [GL          ] GLEW initialization succeeded
[INFO   ] [GL          ] Backend used <glew>
[INFO   ] [GL          ] OpenGL version <b'4.6.0 - Build 27.20.100.9565'>
[INFO   ] [GL          ] OpenGL vendor <b'Intel'>
[INFO   ] [GL          ] OpenGL renderer <b'Intel(R) Iris(R) Xe Graphics'>
[INFO   ] [GL          ] OpenGL parsed version: 4, 6
[INFO   ] [GL          ] Shading version <b'4.60 - Build 27.20.100.9565'>
[INFO   ] [GL          ] Texture max size <16384>
[INFO   ] [GL          ] Texture max units <32>
[INFO   ] [Window      ] auto add sdl2 input provider
[INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
[INFO   ] [Text        ] Provider: sdl2
[INFO   ] [Base        ] Start application main loop
[INFO   ] [GL          ] NPOT texture support is available
[INFO   ] [Base        ] Leaving application in progress...
<queue.Queue object at 0x000001F800D45430>

Output when from ble_kivy import ble_kivy_interface is not commented :注释from ble_kivy import ble_kivy_interface时的输出:

[INFO   ] [Logger      ] Record log in C:\Users\Fabien\.kivy\logs\kivy_21-06-24_41.txt
[INFO   ] [deps        ] Successfully imported "kivy_deps.angle" 0.3.0
[INFO   ] [deps        ] Successfully imported "kivy_deps.glew" 0.3.0
[INFO   ] [deps        ] Successfully imported "kivy_deps.sdl2" 0.3.1
[INFO   ] [Kivy        ] v2.0.0
[INFO   ] [Kivy        ] Installed at "c:\_dev\venv38\lib\site-packages\kivy\__init__.py"
[INFO   ] [Python      ] v3.8.10 (tags/v3.8.10:3d8993a, May  3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)]
[INFO   ] [Python      ] Interpreter at "c:\_dev\venv38\Scripts\python.exe"
[INFO   ] [Factory     ] 186 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_pil (img_ffpyplayer ignored)
[CRITICAL] [Window      ] Unable to find any valuable Window provider. Please enable debug logging (e.g. add -d if running from the command line, or change the log level in the config) and re-run your app to identify potential causes
sdl2 - TypeError: __import__() takes at least 1 argument (0 given)
  File "c:\_dev\venv38\lib\site-packages\kivy\core\__init__.py", line 58, in core_select_lib
    mod = __import__(name='{2}.{0}.{1}'.format(

[CRITICAL] [App         ] Unable to get a Window, abort.

I have seen here and there (issues 476 , 176 , 6816 ) that people are facing similar problems but these github issues were made a year ago and I believe things might have changed.我在这里和那里(问题4761766816 )看到人们面临类似的问题,但这些 github 问题是一年前提出的,我相信事情可能已经改变了。

Is there a way to fix it ?有办法解决吗? If not, is there an alternative working both for Win and Android or will I have to use a different framework ?如果没有,是否有适用于 Win 和 Android 的替代方案,或者我是否必须使用不同的框架?

Bleak ≥ 0.13.0 now officially supports: Bleak ≥ 0.13.0现在正式支持:

  • python-for-android , Kivy's Android backend. python-for-android ,Kivy 的 Android 后端。
  • (macOS|iOS|OS X) ≥ 10.11 via Apple's Core Bluetooth API. (macOS|iOS|OS X) ≥ 10.11 通过 Apple 的核心蓝牙 API。

Bleak thus now officially supports Kivy across all mobile platforms worth tweeting about.因此,Bleak 现在正式支持所有值得发推的移动平台上的 Kivy。 Examples can found in the official Bleak repository under examples/kivy/ .示例可以examples/kivy/下的官方 Bleak 存储库中找到。

Bleak does not currently officially support BeeWare (eg, Toga). Bleak 目前正式支持 BeeWare(例如 Toga)。 If you need Bluetooth Low Energy (BLE) on Python on mobile, you're constrained to Kivy for now.如果您需要在移动设备上的 Python 上使用低功耗蓝牙 (BLE),那么您现在只能使用 Kivy。 (Please edit this answer when Bleak drops official support for BeeWare!) (当 Bleak 放弃对 BeeWare 的官方支持时,请编辑此答案!)

Praise be to Bleak for it makes the impossible ports possible.赞美 Bleak 因为它使不可能的端口成为可能。

I'm trying to connect to a BLE device from a Kivy app, did you manage to make it run?我正在尝试从 Kivy 应用程序连接到 BLE 设备,您设法让它运行了吗? or... Should I give up?或者……我应该放弃吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM