Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot connect to socket via bluetooth - JVM exception occurred: read failed, socket might closed or timeout, read ret: -1 #314

Closed
simon2x opened this issue Dec 11, 2017 · 10 comments

Comments

@simon2x
Copy link

simon2x commented Dec 11, 2017

  • Python: 3.5.2
  • OS: Android 7.1.2

Description

I am trying to connect to bluetooth module HC-06 from android device. The code below will successfully detect paired devices but will not connect - raising "JVM exception occurred: read failed, socket might closed or timeout, read ret: -1".

Code and Logs

12-11 12:57:36.876 26500 26535 I python  :    File "main.py", line 57, in build
12-11 12:57:36.877 26500 26535 I python  :      self.recv_stream, self.send_stream = get_socket_stream('HC-06')
12-11 12:57:36.877 26500 26535 I python  :    File "main.py", line 46, in get_socket_stream
12-11 12:57:36.879 26500 26535 I python  :      socket.connect()
12-11 12:57:36.879 26500 26535 I python  :    File "jnius_export_class.pxi", line 638, in jnius.JavaMethod.__call__ (jnius/jnius.c:22410)
12-11 12:57:36.880 26500 26535 I python  :    File "jnius_export_class.pxi", line 732, in jnius.JavaMethod.call_method (jnius/jnius.c:23494)
12-11 12:57:36.881 26500 26535 I python  :    File "jnius_utils.pxi", line 93, in jnius.check_exception (jnius/jnius.c:3741)
12-11 12:57:36.882 26500 26535 I python  :  jnius.JavaException: JVM exception occurred: read failed, socket might closed or timeout, read ret: -1
12-11 12:57:36.986 26500 26535 I python  : Python for android ended.

Permissions

android.permissions = INTERNET,BLUETOOTH,BLUETOOTH_ADMIN,BLUETOOTH_PRIVILEGED,ACCESS_WIFI_STATE,ACCESS_NETWORK_STATE

Here is example function taking name of bluetooth device as arg.

def get_socket_stream(name):
    paired_devices = BluetoothAdapter.getDefaultAdapter().getBondedDevices().toArray()
    print("PAIRED: ", paired_devices)
    socket = None
    for device in paired_devices:#
        print(device.getName())
        if device.getName() == name:
            print(device.getName())
            
            # socket = device.createInsecureRfcommSocketToServiceRecord(
                # UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"))
            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
@cheath2013
Copy link

I am having the same issue using similar code. Has any solution been found?

@Ruud14
Copy link

Ruud14 commented Jul 30, 2018

I am facing the same error too, has any of you found a solution yet?

@Ruud14
Copy link

Ruud14 commented Jul 30, 2018

For me the issue was that I didn't use '00001101-0000-1000-8000-00805F9B34FB'

@KeyWeeUsr
Copy link
Contributor

This is not a PyJNIus bug, it's a normal Java error. However, take a look at this answer and you might find the solution.

@Giammi77
Copy link

Can't understan how to fix the problem in kivy please help me thank you
Gianmaria

@lucasvreis
Copy link

Me too. Could somebody help?

@Hmerman6006
Copy link

Hmerman6006 commented Oct 19, 2019

The problem is that when you use UUID to create a createRfcommSocketToServiceRecord socket on Android post 4.2 the socket may not be acceptable i.e. -1. So if you are still struggling to get an answer (which took me 5 hrs to grasp) try one of the following:
1] After creating the socket check if the port is less than one and assign a new port number > 0 -

if socket.port <= 0:
            socket = device.createRfcommSocket(1)
            if not socket.connected:
                socket.connect()

2] Use java class createInsecureRfcommSocketToServiceRecord(UUID) instead (have not tried this one)
[https://stackoverflow.com/questions/18657427/ioexception-read-failed-socket-might-closed-bluetooth-on-android-4-3](Link to insecure rfcomm comments)

Hope this helps, because when I originally clicked on this link, the drips and drabs of linked answers was quite political.

@Enkumicahel
Copy link

Hello guys,
Has anyone been able to figure a solution for this one?

@ps2229
Copy link

ps2229 commented Apr 1, 2022

AttributeError: 'android.bluetooth.BluetoothSocket' object has no attribute 'port'
I am getting this error after using the above solution does anyone know how to solve this?

@alexmosc
Copy link

alexmosc commented Aug 9, 2023

AttributeError: 'android.bluetooth.BluetoothSocket' object has no attribute 'port' I am getting this error after using the above solution does anyone know how to solve this?

I also has had this error following the solution by @Hmerman6006

Maybe in three years the API in Python changed?

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

No branches or pull requests

10 participants