简体   繁体   中英

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). I tried using Bleak to handle BLE connections but importing the Bleak module made my app crash. 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.

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 :

[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 :

[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.

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 ?

Bleak ≥ 0.13.0 now officially supports:

  • python-for-android , Kivy's Android backend.
  • (macOS|iOS|OS X) ≥ 10.11 via Apple's Core Bluetooth API.

Bleak thus now officially supports Kivy across all mobile platforms worth tweeting about. Examples can found in the official Bleak repository under examples/kivy/ .

Bleak does not currently officially support BeeWare (eg, Toga). If you need Bluetooth Low Energy (BLE) on Python on mobile, you're constrained to Kivy for now. (Please edit this answer when Bleak drops official support for BeeWare!)

Praise be to Bleak for it makes the impossible ports possible.

I'm trying to connect to a BLE device from a Kivy app, did you manage to make it run? or... Should I give up?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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