This is my little Kivy project. My goal for now is to send a string (message) to my PC and print it print(message)
:
main.py
import kivy
from kivy.app import App
from kivy.uix.button import Button
from bluetooth import AndroidBluetoothClass
kivy.require("2.1.0")
# class in which we are creating the button
class ButtonApp(App):
def build(self):
# use a (r, g, b, a) tuple
self.btn = Button(text="Push Me !",
font_size="20sp",
background_color=(1, 1, 1, 1),
color=(1, 1, 1, 1),
size=(32, 32),
size_hint=(.2, .2),
pos=(300, 250))
# bind() use to bind the button to function callback
self.btn.bind(on_press=self.callback)
return self.btn
# callback function tells when button pressed
def callback(self, event):
try:
a = AndroidBluetoothClass()
a.getAndroidBluetoothSocket('MSI')
a.BluetoothSend("Balls")
except Exception as e:
self.btn.text = str(e)
root = ButtonApp()
root.run()
bluetooth.py (src: Can't send bluetooth from Kivy App on Android)
from jnius import autoclass
class AndroidBluetoothClass:
def getAndroidBluetoothSocket(self, DeviceName):
paired_devices = self.BluetoothAdapter.getDefaultAdapter().getBondedDevices().toArray()
socket = None
for device in paired_devices:
if device.getName() == DeviceName:
socket = device.createRfcommSocketToServiceRecord(
self.UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"))
self.ReceiveData = self.BufferReader(self.InputStream(socket.getInputStream()))
self.SendData = socket.getOutputStream()
socket.connect()
self.ConnectionEstablished = True
print('Bluetooth Connection successful')
return self.ConnectionEstablished
def BluetoothSend(self, Message, *args):
if self.ConnectionEstablished == True:
self.SendData.write(Message)
else:
print('Bluetooth device not connected')
def BluetoothReceive(self, *args):
DataStream = ''
if self.ConnectionEstablished == True:
DataStream = str(self.ReceiveData.readline())
return DataStream
def __init__(self):
self.BluetoothAdapter = autoclass('android.bluetooth.BluetoothAdapter')
self.BluetoothDevice = autoclass('android.bluetooth.BluetoothDevice')
self.BluetoothSocket = autoclass('android.bluetooth.BluetoothSocket')
self.UUID = autoclass('java.util.UUID')
self.BufferReader = autoclass('java.io.BufferedReader')
self.InputStream = autoclass('java.io.InputStreamReader')
self.ConnectionEstablished = False
def __del__(self):
print('class AndroidBluetooth destroyer')
How can i collect the data sent by my android application? There is a way? Hope you can help me :D
If you need it buildozer.spec
[app]
title = wtheeel
version = 1.1
package.name = wtheeel
package.domain = test.able
source.dir = .
source.include_exts = py,png,jpg,kv,atlas
android.permissions =
FOREGROUND_SERVICE,
BLUETOOTH,
BLUETOOTH_ADMIN,
BLUETOOTH_SCAN,
BLUETOOTH_CONNECT,
BLUETOOTH_ADVERTISE,
ACCESS_FINE_LOCATION
requirements = kivy==2.1.0,python3,able_recipe
services = Able:service.py:foreground
android.accept_sdk_license = True
# android.api = 31
# android.minapi = 31
[buildozer]
warn_on_root = 1
# (int) Log level (0 = error only, 1 = info, 2 = debug (with command output))
log_level = 2
I tried with the socket library but i was already expecting a bad news. server.py
import socket
hostMACAddress = 'my mac address'
port = 4
backlog = 1
size = 1024
s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
s.bind((hostMACAddress, port))
s.listen(backlog)
try:
client, address = s.accept()
while 1:
data = client.recv(size)
if data:
print(data)
client.send(data)
except:
print("Closing socket")
client.close()
s.close()