Receive data via bluetooth using python on android phone
Question:
I have an ESP32 board which sends data via bluetooth. I can receive data on PC using this python code:
from bluetooth import *
import sys
def input_and_send():
while True:
data = input()
if len(data) == 0: break
sock.send(data)
sock.send("n")
def rx_and_echo():
sock.send("nsend anythingn")
while True:
data = sock.recv(buf_size)
if data:
print(data.decode('utf-8'))
sock.send(data)
addr = "XX:XX:XX:XX:XX:XX"
service_matches = find_service( address = addr )
buf_size = 1024
if len(service_matches) == 0:
print("couldn't find the SampleServer service =(")
sys.exit(0)
for s in range(len(service_matches)):
print("nservice_matches: [" + str(s) + "]:")
print(service_matches[s])
first_match = service_matches[0]
port = first_match["port"]
name = first_match["name"]
host = first_match["host"]
port = 1
print("connecting to "%s" on %s, port %s" % (name, host, port))
sock = BluetoothSocket(RFCOMM)
sock.connect((host, port))
print("connected")
# input_and_send()
rx_and_echo()
sock.close()
Data.append(data.decode('utf-8'))
Now, I want to get data on android phone. I wrote a python program using kivy, but bluetooth package does not work on android. I tried bleak and jnius packages, but they did not work. Is there another packages which can use bluetooth of phone properly? I see, some persons advise using jnius package for android, but I could not get data using "BluetoothReceive" function.
Any help is appreciated.
Answers:
I understood how to receive data using python on android. I used jnius package, and tried to look errors using this code on android phone.
I found theBufferReader
should be used instead of getInputStream()
, and readLine()
instead of readline()
. You can receive data using the below code:
from jnius import autoclass
BluetoothAdapter = autoclass('android.bluetooth.BluetoothAdapter')
BluetoothDevice = autoclass('android.bluetooth.BluetoothDevice')
BluetoothSocket = autoclass('android.bluetooth.BluetoothSocket')
UUID = autoclass('java.util.UUID')
BufferReader = autoclass('java.io.BufferedReader')
InputStream = autoclass('java.io.InputStreamReader')
paired_devices = BluetoothAdapter.getDefaultAdapter().getBondedDevices().toArray()
socket = None
for device in paired_devices:
if device.getName() == 'ESP32':
socket = device.createRfcommSocketToServiceRecord(
UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"))
recv_stream = BufferReader(InputStream(socket.getInputStream()))
break
socket.connect()
Serial_Data = recv_stream.readLine()
Add this bluetooth permision to your buildozer.spec file.
android.permissions = BLUETOOTH, BLUETOOTH_ADMIN, ACCESS_FINE_LOCATION
Since jnius package does not work on PC (Windows or Linux), I hope there be another way to debug the python code directly, without making application and testing it on android phone.
I have an ESP32 board which sends data via bluetooth. I can receive data on PC using this python code:
from bluetooth import *
import sys
def input_and_send():
while True:
data = input()
if len(data) == 0: break
sock.send(data)
sock.send("n")
def rx_and_echo():
sock.send("nsend anythingn")
while True:
data = sock.recv(buf_size)
if data:
print(data.decode('utf-8'))
sock.send(data)
addr = "XX:XX:XX:XX:XX:XX"
service_matches = find_service( address = addr )
buf_size = 1024
if len(service_matches) == 0:
print("couldn't find the SampleServer service =(")
sys.exit(0)
for s in range(len(service_matches)):
print("nservice_matches: [" + str(s) + "]:")
print(service_matches[s])
first_match = service_matches[0]
port = first_match["port"]
name = first_match["name"]
host = first_match["host"]
port = 1
print("connecting to "%s" on %s, port %s" % (name, host, port))
sock = BluetoothSocket(RFCOMM)
sock.connect((host, port))
print("connected")
# input_and_send()
rx_and_echo()
sock.close()
Data.append(data.decode('utf-8'))
Now, I want to get data on android phone. I wrote a python program using kivy, but bluetooth package does not work on android. I tried bleak and jnius packages, but they did not work. Is there another packages which can use bluetooth of phone properly? I see, some persons advise using jnius package for android, but I could not get data using "BluetoothReceive" function.
Any help is appreciated.
I understood how to receive data using python on android. I used jnius package, and tried to look errors using this code on android phone.
I found theBufferReader
should be used instead of getInputStream()
, and readLine()
instead of readline()
. You can receive data using the below code:
from jnius import autoclass
BluetoothAdapter = autoclass('android.bluetooth.BluetoothAdapter')
BluetoothDevice = autoclass('android.bluetooth.BluetoothDevice')
BluetoothSocket = autoclass('android.bluetooth.BluetoothSocket')
UUID = autoclass('java.util.UUID')
BufferReader = autoclass('java.io.BufferedReader')
InputStream = autoclass('java.io.InputStreamReader')
paired_devices = BluetoothAdapter.getDefaultAdapter().getBondedDevices().toArray()
socket = None
for device in paired_devices:
if device.getName() == 'ESP32':
socket = device.createRfcommSocketToServiceRecord(
UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"))
recv_stream = BufferReader(InputStream(socket.getInputStream()))
break
socket.connect()
Serial_Data = recv_stream.readLine()
Add this bluetooth permision to your buildozer.spec file.
android.permissions = BLUETOOTH, BLUETOOTH_ADMIN, ACCESS_FINE_LOCATION
Since jnius package does not work on PC (Windows or Linux), I hope there be another way to debug the python code directly, without making application and testing it on android phone.