Skip to content

Instantly share code, notes, and snippets.

@tawanda
Forked from tito/bluetooth.py
Created August 9, 2017 18:29
Show Gist options
  • Save tawanda/5ffd882c8272d343fab9f554ec87efd6 to your computer and use it in GitHub Desktop.
Save tawanda/5ffd882c8272d343fab9f554ec87efd6 to your computer and use it in GitHub Desktop.
Bluetooth example on Android using Python / Pyjnius
'''
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()
# 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()
@hajimurtaza
Copy link

what is livnor? After pairing with device, when i run kivy app on phone , it crashes.
The logs are as follows:
image

@tawanda
Copy link
Author

tawanda commented Mar 2, 2021

It's been so long, I am not familiar with this project anymore, best to contact the upstream author

@hajimurtaza
Copy link

livnor should be replaced by human readable bluetooth name (The one that shows up in bluetooth settings of your device and is editable).
Also it would be great if the code is explained further.
Like there is absolutely no documentation available about why we are using UUID , unless i explicitly learn about bluetooth standards.
Please tag the upstream author, so i can communicate with him.
I would gladly be interested in improving code and readability.

@tawanda
Copy link
Author

tawanda commented Mar 3, 2021

find the original repo here

@tito

https://gist.github.com/tito/7432757

@wladek1909
Copy link

what is livnor? After pairing with device, when i run kivy app on phone , it crashes.
The logs are as follows:
image

Hi man do u solve this error ?

@hajimurtaza
Copy link

what is livnor? After pairing with device, when i run kivy app on phone , it crashes.
The logs are as follows:
image

Hi man do u solve this error ?

Yes replace livnor with the name of your bluetooth device.
For eg. My raspberry pi bluetooth name in settings shows as raspberrypi.
Using this name fixed it.

@wladek1909
Copy link

wladek1909 commented Apr 2, 2021 via email

@wladek1909
Copy link

wladek1909 commented Apr 2, 2021 via email

@hajimurtaza
Copy link

Use common sense, The code is running on your phone, so you will try to connect with external device. Put the name that shows in bluetooth settings of your phone when you pair your phone with the device you are trying to connect to.

@wladek1909
Copy link

wladek1909 commented Apr 2, 2021 via email

@ps2229
Copy link

ps2229 commented Mar 30, 2022

what is livnor? After pairing with device, when i run kivy app on phone , it crashes.
The logs are as follows:
image

Hi man do u solve this error ?

Yes replace livnor with the name of your bluetooth device. For eg. My raspberry pi bluetooth name in settings shows as raspberrypi. Using this name fixed it.

Even after changing the name still facing the same error! Any solution to this? Perhaps I need to change the socket as well , as it is None currently.

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