Python kivy android bluetooth

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 ()
Читайте также:  Remove content using css

Источник

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 ()
Читайте также:  Переменная php содержит код

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Kivy app to read Inputstream via bluetooth

Hmerman6006/BluetoothReadInputstream

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Kivy app that reads the Inputstream via Bluetooth

The Kivy and KivyMD uses existing OS Api to read the string output from bluetooth device.

*So far only platform implemented for is android

main.py, main.kv, helpers.py ----- |_/views directory contains root and other view .kv |_/settings directory contains settingsjson.py and other configs |_/res contains external java, images and fonts 
  • /.buildozer/android/platform/python-for-android/pythonforandroid/recipes/android/src/android/broadcast.py , by adding the code below after first build.

BROADCASTRECEIVER CODE:

def _expand_partial_name(partial_name): if '.' in partial_name: return partial_name # Its actually a full dotted name else: name = 'ACTION_<>'.format(partial_name.upper()) if hasattr(Intent, name): return getattr(Intent, name) elif hasattr(BluetoothAdapter, name): return getattr(BluetoothAdapter, name) else: raise Exception('The intent <> doesnt exist'.format(name)) # resolve actions/categories first Intent = autoclass('android.content.Intent') BluetoothAdapter = autoclass('android.bluetooth.BluetoothAdapter') 
  1. build with buildozer command buildozer android debug deploy .
  2. after amending above mentioned file run buildozer android clean .
  3. run build command again.

kivy, kivymd, plyer, android (see buildozer.spec)
*uses widgets of kivymd where possible due to TypeError: argument of type ‘module’ is not iterable if widgets are not loaded in App class.

buildozer.spec list of Java files to add section points to:
android.add_src = %(source.dir)s/res/ext_java/*.java

  1. Sometimes UI freezes if cannot find connection. SOLVED
  2. Handle bluetooth device sudden power off or disconnection. SOLVED
  3. .gif presplash not having animated loading effect.
  4. BroadcastReceiver does not start if initialised in class scope and bluetooth is already ON. SOLVED
  5. Maybe test thread for connection to RfcommSocket.
  1. UI does not freeze when connection is suddenly lost due to Threads on bluetooth RfcommSocket connection.
  2. Added BroadcastReceiver to check Bluetooth state on.
  3. BroadcastReceiver is not needed if bluetooth is ON, since enabled state is checked by using BluetoothAdapter library. But thanks to AdyWizards wifiscan project the receiver works on_resume and on_pause .

Cleaned up code. Tested and no problems yet. When intialy connecting to socket the method is unthreaded. This may cause problems and needs further testing.

Tested with following versions:

About

Kivy app to read Inputstream via bluetooth

Источник

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