java.io.IOException: read failed, socket might closed or timeout, read ret: -1 on Android 5.0.1 Lollipop version

12,161

Solution 1

Use createRfcommSocketToServiceRecord instead of createRfcommSocket

createRfcommSocketToServiceRecord takes the UUID you pass and uses SDP to decide what radio channel to use for the connection. It also checks to make sure that a server is listening on the remote endpoint, with the same UUID. In this way, it's the most reliable way to get a connection: it'll always use the correct channel, and if opening the connection succeeds, you know something at the other end can understand your protocol.

In contrast, createRfcommSocket just connects to the channel you tell it. There's no way to know whether anything is listening on the remote endpoint: you only know the device is there. Also, your choice of radio channel may be completely inappropriate. That's why this function is not published in the API, and the other function is preferred.

createRfcommSocket may appear at first to be more reliable, but it's because it's not checking for the presence of a listener at the other endpoint: it's ignoring some error cases. This might be alright for experimenting, but it's no use for a production system, because often the user will forget to start the server on the other endpoint, and your app will fail in confusing ways.

Of course, as createRfcommSocket isn't published in the API, you've no guarantee it will continue to work at all in future releases of Android.

Solution 2

I've been facing the same issue on 6.0.1, after reading about it at various threads/forums/blogs I understood that it is because of a missing fallback. And you can take care of it by catching the exception and creating the required fallback.

To be more specific, the BluetoothManager returns a default value of -1, which is not a acceptable state and hence, the error. This will raise an exception, which can be handled to create a fallback to solve the issue by replacing the error of -1.

Here is the link which helped me:

https://github.com/don/BluetoothSerial/issues/89

Reference: IOException: read failed, socket might closed - Bluetooth on Android 4.3

Solution 3

I had a similar problem on Lollipop only (was working on previous versions) and replacing "createRfcommSocket" by "createInsecureRfcommSocket" fixed the problem.

if you choose the official API you could try createInsecureRfcommSocketToServiceRecord as createRfcommSocketToServiceRecord was not working for me either.

Share:
12,161
Kushal
Author by

Kushal

Currently working for : F5, Hyderabad, India Linkedin : https://www.linkedin.com/in/kushal-gandhi-09357630/

Updated on June 14, 2022

Comments

  • Kushal
    Kushal almost 2 years

    I am making Bluetooth socket connection to Bluetooth device and want to read bytes from the device.

    I have established connection correctly :

     try {
             Method m = mmDevice.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
              temp = (BluetoothSocket) m.invoke(mmDevice, 1);
     } catch (Exception e) {
     }
    

    I am reading bytes correctly from Bluetooth device.

    I am getting the exception :

    java.io.IOException: read failed, socket might closed or timeout, read ret: -1

    Due to this, the connection is broken and the communication is also over between my device and Bluetooth device.

    This problem is coming on Android 5.0.1 Lollipop especially

    Can anyone have workaround ?

  • Kushal
    Kushal about 9 years
    Thank you for your help alot.. Is there any specific problem with createRfcommSocket and Android 5.0.1 Lollipop ? I was facing this error more in Lollipop
  • madhu131313
    madhu131313 about 9 years
    I am not sure, but the method I provided is more reliable because of UUID
  • rfornal
    rfornal almost 9 years
    This is a series of comments on the question; please post them as such when you can.
  • madhu131313
    madhu131313 almost 9 years
    Even with Insecure method, the same error is persisting.
  • Fuad
    Fuad almost 8 years
    Functions such as "createRfcommSocket..." are only used to create a socket for the client side. However, I'm getting this problem on the server side. (I made the socket using serversocket). Do you know how to fix it for server side?
  • Fuad
    Fuad almost 8 years
    From the documentation: "To create a BluetoothSocket for connecting to a known device, use BluetoothDevice.createRfcommSocketToServiceRecord(). Then call connect() to attempt a connection to the remote device. This call will block until a connection is established or the connection fails. To create a BluetoothSocket as a server (or "host"), see the BluetoothServerSocket documentation."
  • Vaibhav Bajaj
    Vaibhav Bajaj almost 8 years
    Do not add such "comments" as an answer.