Python kivy android bluetooth

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"") kivy_thread.join() loop = asyncio.get_event_loop() loop.run_until_complete(main()) 
[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 [INFO ] [GL ] OpenGL version [INFO ] [GL ] OpenGL vendor [INFO ] [GL ] OpenGL renderer [INFO ] [GL ] OpenGL parsed version: 4, 6 [INFO ] [GL ] Shading version [INFO ] [GL ] Texture max size [INFO ] [GL ] Texture max units [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.
[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='..'.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 ?

Читайте также:  Sony блютуз наушники топ

Источник

cuibonobo / bluetooth_kivy.py

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

# Same as before, with a kivy-based UI
»’
Bluetooth/Pyjnius example
=========================
This was used to send some bytes to an arduino via bluetooth.
The app must have BLUETOOTH and BLUETOOTH_ADMIN permissions (well, i didn’t
tested without BLUETOOTH_ADMIN, maybe it works.)
Connect your device to your phone, via the bluetooth menu. After the
pairing is done, you’ll be able to use it in the app.
»’
from jnius import autoclass
BluetoothAdapter = autoclass ( ‘android.bluetooth.BluetoothAdapter’ )
BluetoothDevice = autoclass ( ‘android.bluetooth.BluetoothDevice’ )
BluetoothSocket = autoclass ( ‘android.bluetooth.BluetoothSocket’ )
UUID = autoclass ( ‘java.util.UUID’ )
def get_socket_stream ( name ):
paired_devices = BluetoothAdapter . getDefaultAdapter (). getBondedDevices (). toArray ()
socket = None
for device in paired_devices :
if device . getName () == name :
socket = device . createRfcommSocketToServiceRecord (
UUID . fromString ( «00001101-0000-1000-8000-00805F9B34FB» ))
recv_stream = socket . getInputStream ()
send_stream = socket . getOutputStream ()
break
socket . connect ()
return recv_stream , send_stream
if __name__ == ‘__main__’ :
kv = »’
BoxLayout:
Button:
text: ‘0’
on_release: app.reset([b1, b2, b3, b4, b5])
ToggleButton:
id: b1
text: ‘1’
on_release: app.send(self.text)
ToggleButton:
id: b2
text: ‘2’
on_release: app.send(self.text)
ToggleButton:
id: b3
text: ‘3’
on_release: app.send(self.text)
ToggleButton:
id: b4
text: ‘4’
on_release: app.send(self.text)
ToggleButton:
id: b5
text: ‘5’
on_release: app.send(self.text)
»’
from kivy . lang import Builder
from kivy . app import App
class Bluetooth ( App ):
def build ( self ):
self . recv_stream , self . send_stream = get_socket_stream ( ‘linvor’ )
return Builder . load_string ( kv )
def send ( self , cmd ):
self . send_stream . write ( ‘<> \n ‘ . format ( cmd ))
self . send_stream . flush ()
def reset ( self , btns ):
for btn in btns :
btn . state = ‘normal’
self . send ( ‘0 \n ‘ )
Bluetooth (). run ()
Читайте также:  Tpa3116d2 bluetooth 5 0

Источник

tito / bluetooth.py

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

»’
Bluetooth/Pyjnius example
=========================
This was used to send some bytes to an arduino via bluetooth.
The app must have BLUETOOTH and BLUETOOTH_ADMIN permissions (well, i didn’t
tested without BLUETOOTH_ADMIN, maybe it works.)
Connect your device to your phone, via the bluetooth menu. After the
pairing is done, you’ll be able to use it in the app.
»’
from jnius import autoclass
BluetoothAdapter = autoclass ( ‘android.bluetooth.BluetoothAdapter’ )
BluetoothDevice = autoclass ( ‘android.bluetooth.BluetoothDevice’ )
BluetoothSocket = autoclass ( ‘android.bluetooth.BluetoothSocket’ )
UUID = autoclass ( ‘java.util.UUID’ )
def get_socket_stream ( name ):
paired_devices = BluetoothAdapter . getDefaultAdapter (). getBondedDevices (). toArray ()
socket = None
for device in paired_devices :
if device . getName () == name :
socket = device . createRfcommSocketToServiceRecord (
UUID . fromString ( «00001101-0000-1000-8000-00805F9B34FB» ))
recv_stream = socket . getInputStream ()
send_stream = socket . getOutputStream ()
break
socket . connect ()
return recv_stream , send_stream
if __name__ == ‘__main__’ :
recv_stream , send_stream = get_socket_stream ( ‘linvor’ )
send_stream . write ( ‘hello \n ‘ )
send_stream . flush ()

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

# Same as before, with a kivy-based UI
»’
Bluetooth/Pyjnius example
=========================
This was used to send some bytes to an arduino via bluetooth.
The app must have BLUETOOTH and BLUETOOTH_ADMIN permissions (well, i didn’t
tested without BLUETOOTH_ADMIN, maybe it works.)
Connect your device to your phone, via the bluetooth menu. After the
pairing is done, you’ll be able to use it in the app.
»’
from jnius import autoclass
BluetoothAdapter = autoclass ( ‘android.bluetooth.BluetoothAdapter’ )
BluetoothDevice = autoclass ( ‘android.bluetooth.BluetoothDevice’ )
BluetoothSocket = autoclass ( ‘android.bluetooth.BluetoothSocket’ )
UUID = autoclass ( ‘java.util.UUID’ )
def get_socket_stream ( name ):
paired_devices = BluetoothAdapter . getDefaultAdapter (). getBondedDevices (). toArray ()
socket = None
for device in paired_devices :
if device . getName () == name :
socket = device . createRfcommSocketToServiceRecord (
UUID . fromString ( «00001101-0000-1000-8000-00805F9B34FB» ))
recv_stream = socket . getInputStream ()
send_stream = socket . getOutputStream ()
break
socket . connect ()
return recv_stream , send_stream
if __name__ == ‘__main__’ :
kv = »’
BoxLayout:
Button:
text: ‘0’
on_release: app.reset([b1, b2, b3, b4, b5])
ToggleButton:
id: b1
text: ‘1’
on_release: app.send(self.text)
ToggleButton:
id: b2
text: ‘2’
on_release: app.send(self.text)
ToggleButton:
id: b3
text: ‘3’
on_release: app.send(self.text)
ToggleButton:
id: b4
text: ‘4’
on_release: app.send(self.text)
ToggleButton:
id: b5
text: ‘5’
on_release: app.send(self.text)
»’
from kivy . lang import Builder
from kivy . app import App
class Bluetooth ( App ):
def build ( self ):
self . recv_stream , self . send_stream = get_socket_stream ( ‘linvor’ )
return Builder . load_string ( kv )
def send ( self , cmd ):
self . send_stream . write ( ‘<> \n ‘ . format ( cmd ))
self . send_stream . flush ()
def reset ( self , btns ):
for btn in btns :
btn . state = ‘normal’
self . send ( ‘0 \n ‘ )
Bluetooth (). run ()
Читайте также:  Блютуз смарт старлайн s66

Источник

Оцените статью
Adblock
detector