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

2024-05-19 12:36:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Kivy开发一个跨平台的应用程序(适用于Windows和Android目标),我需要使用蓝牙低能耗(与ESP32 BLE服务器通信)。我尝试使用Bleak来处理不稳定的连接,但导入Bleak模块使我的应用程序崩溃。我使用threading.Thread来分割BLE和Kivy应用程序,但我仍然有一个问题,因为我必须在我的主python文件中导入Kivy和Bleak模块

我的主应用程序:

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

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>

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.

我在这里和那里看到(问题4761766816)人们面临着类似的问题,但这些github问题是一年前提出的,我相信事情可能已经发生了变化

有办法解决吗?如果没有,是否有一个既适用于Win又适用于Android的替代方案,或者我必须使用不同的框架


Tags: thedepsfromimportinfoimgwindowat