Reputation: 1
I am developing an android app using python. I want to connect this app with a server on my computer via bluetooth using pyjnius library. I am using rfcomm-server.py script as a server from this link: https://github.com/pybluez/pybluez/blob/master/examples/simple/rfcomm-server.py
Client script:
from jnius import autoclass
server_mac_address = '94:08:53:52:B5:98'
uuid = "00001101-0000-1000-8000-00805F9B34FB"
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):
adapter = BluetoothAdapter.getDefaultAdapter()
device = adapter.getRemoteDevice(server_mac_address)
socket = device.createRfcommSocketToServiceRecord(UUID.fromString(uuid))
adapter.cancelDiscovery()
socket.connect()
recv_stream = socket.getInputStream()
send_stream = socket.getOutputStream()
return recv_stream, send_stream
if __name__ == '__main__':
recv_stream, send_stream = get_socket_stream('linvor')
send_stream.write('hello\n')
send_stream.flush()
When I run this code I get the following error: "jnius.jnius.JavaException: JVM exception occurred: read failed, socket might closed or timeout, read ret: -1" in socket.connect() line. I connected my phone to computer via the bluetooth menu, so I made sure that my device is paired with the phone. Could someone help me with it?
I founded different suggested solutions related to similar issue. For example this one: Bluetooth Connection failed "java.io.IOException: read failed, socket might closed or timeout, read ret: -1".
Also I tried to implement the following java code in python, but I didn't succeed:
socket = (BluetoothSocket) device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(device,1);
socket.connect();
All the solutions that I found on stack overflow are implemented in java. So this is the problem, because I don't know java :) So I need an implementation in python.
Also I tried to use different functions instead of createRfcommSocketToServiceRecord() to create a socket. For example createInsecureRfcommSocketToServiceRecord() and createRfcommSocket(), but anyway it didn't work.
Upvotes: 0
Views: 726
Reputation: 11
Maybe you shouldn't use pyjnius? If you want to use something on Android, you can use the python project for Android. P4A already includes pyjnius and Kivy and has scripts for including other Python modules and for creating an APK from your application. python-for-android also provides a VirtualBox virtual machine, which simplifies the work process for Windows users.
Upvotes: 0