Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Bluetooth example with Kivy. A more fleshed-out example here: A more modern way of doing it: And this guy used Kivy to connect to an Arduino: Native Kivy code (?):
# 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(
recv_stream = socket.getInputStream()
send_stream = socket.getOutputStream()
return recv_stream, send_stream
if __name__ == '__main__':
kv = '''
text: '0'
on_release: app.reset([b1, b2, b3, b4, b5])
id: b1
text: '1'
on_release: app.send(self.text)
id: b2
text: '2'
on_release: app.send(self.text)
id: b3
text: '3'
on_release: app.send(self.text)
id: b4
text: '4'
on_release: app.send(self.text)
id: b5
text: '5'
on_release: app.send(self.text)
from kivy.lang import Builder
from 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):
def reset(self, btns):
for btn in btns:
btn.state = 'normal'
Copy link

rupikat commented Dec 8, 2018

How to install jnius on windows?

Copy link

ManasaJaddu commented May 1, 2019

The above code is not working in android mobile.I build the apk using buildozer in linux.

Copy link

piyush05124 commented May 25, 2020

How to install jnius on windows?

using pip install jnius

Copy link

Chri5G commented Nov 18, 2020

JAVA error

Copy link

xloem commented Dec 15, 2020

prototype BLE interface in kivy at . gattlib is a c-based interface for ble on desktop. i just stumbled on it first kinda randomly when i needed to pick an interface.

Copy link

wladek1909 commented Mar 31, 2021

Hi man, i have one problem. When i run app in android , app crashed. In adb logcat i have error
**AttributeError: 'Nonetype' object has no attribute 'connect'**
That is error on this line ? **socket = None**
Please help. Some fix ?

Copy link

Chri5G commented Apr 6, 2021

You need to change the socket to rfcomm
I used my laptop with regular bluetooth but for android you will need ble and different socket

Copy link

xloem commented Apr 6, 2021

Note: has an open pull request with example code for ble android support at hbldh/bleak#422

Copy link

ps2229 commented Mar 30, 2022

You need to change the socket to rfcomm
I used my laptop with regular bluetooth but for android you will need ble and different socket

Were you able to solve this error? I am stuck in the same situation with no avail.

Copy link

NewAge12 commented Apr 2, 2022

Why am I getting jnius.jnius.JavaException: JVM exception occurred: read failed, socket might closed or timeout, read ret: -1

Copy link

imvickykumar999 commented May 28, 2022

I am getting this error.

Traceback (most recent call last):
  File "C:\Users\Vicky\Desktop\Repository\Android-with-Kivy-Framework\Examples\", line 15, in <module>
    BluetoothAdapter = autoclass('android.bluetooth.BluetoothAdapter')
  File "C:\Users\Vicky\AppData\Local\Programs\Python\Python39\lib\site-packages\jnius\", line 211, in autoclass
    c = find_javaclass(clsname)
  File "jnius\jnius_export_func.pxi", line 26, in jnius.find_javaclass
  File "jnius\jnius_utils.pxi", line 91, in jnius.check_exception
jnius.JavaException: JVM exception occurred: android/bluetooth/BluetoothAdapter java.lang.NoClassDefFoundError

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment