Java.io.IOException: read failed, socket can be closed or timeout, read ret: -1 on Android 5.0.1 version of Lollipop

I am connecting a Bluetooth socket to a Bluetooth device and want to read bytes from the device.

I set up the 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 from a Bluetooth device correctly.

I get an exception:

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

In this regard, the connection is disconnected, and the connection between my device and the Bluetooth device also terminates.

This issue goes on Android 5.0.1 Lollipop especially

Does anyone have a workaround?

+6
android android-5.0-lollipop bluetooth
Mar 19 '15 at 11:43
source share
5 answers

Use createRfcommSocketToServiceRecord instead of createRfcommSocket

createRfcommSocketToServiceRecord accepts the UUID that you are transmitting and uses SDP to determine which radio channel is used for the connection. It also verifies that the server is listening on the remote endpoint, with the same UUID. Thus, this is the most reliable way to get a connection: it will always use the correct channel, and if the connection is successful, you know that something on the other end can understand your protocol.

In contrast, createRfcommSocket just connects to the channel you tell it to. There is no way to find out if something is listening on the remote endpoint: you know what the device is. In addition, your choice of radio channel may be completely inappropriate. This is why this function is not published in the API, and another function is preferable.

createRfcommSocket may seem more reliable at first glance, but this is because it does not check for a listener on another endpoint: it ignores some cases of errors. This may be good for experimentation, but it is useless for a production system, because often the user will forget to start the server on a different endpoint, and your application will not get confused.

Of course, since createRfcommSocket not published in the API, you have no guarantee that it will continue to work in general in future releases of Android.

+6
May 04 '15 at 11:07
source share

I encountered the same problem in version 6.0.1, having read about it on different threads / forums / blogs, I realized that this was due to a missing reserve. And you can take care of this by catching the exception and creating the necessary reserve.

To be more specific, BluetoothManager returns a default value of -1, which is not an acceptable state and therefore an error. This will throw an exception that can be handled to create a backup to solve the problem by replacing the -1 error.

Here is the link that helped me:

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

Link: IOException: read failed, socket may be closed - Bluetooth on Android 4.3

+2
Apr 27 '17 at 12:30
source share

I had a similar problem only with Lollipop (worked on previous versions) and replaced " createRfcommSocket " with " createInsecureRfcommSocket " the problem was fixed.

if you choose the official API, you can try createInsecureRfcommSocketToServiceRecord as createRfcommSocketToServiceRecord didn't work for me either.

0
May 26 '15 at 19:18
source share

By adding a filter action my problem is resolved

  // Register for broadcasts when a device is discovered IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(BluetoothDevice.ACTION_FOUND); intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); registerReceiver(mReceiver, intentFilter); 
0
Jun 17 '19 at 15:42
source share

I have a similar problem with Lollipop (in this case, 5.0.2). I do not see any problems when starting Kitkat. I am using createInsecureRfcommSocketToServiceRecord. I tried some of the suggested methods for using reflection, but it didn't seem to help (I think this solution was related to older versions of Android). To be clear, what I see usually happens when I connect to several SPP devices (barcode reader and I / O board with a Bluetooth Bluetooth router radio). Iโ€™m trying to find a solution and I think that maybe thereโ€™s not how it could be, the problem is in Lollipop if you connect to two devices with the same UUID (in this case, common SPP).

-one
Aug 02 '16 at 20:32
source share



All Articles