BluetoothSocket.connect () throwing exception "read failed"

I have a project that connects to a device via Bluetooth. It worked reliably enough, but now it interrupts the call to BluetoothSocket.connect() every time. (Well, I got it to connect once for thousands of attempts over a 4-hour period.) Most of the code was taken from the standard chat code sample in the API, with the exception of the general modification when receiving a BluetoothSocket :

 Method m = device.getClass().getMethod( "createRfcommSocket", new Class[] { int.class }); tmp = (BluetoothSocket) m.invoke(device, Integer.valueOf(1)); 

Here's a method of interest that starts after a BluetoothSocket :

 public void run() { setName("ConnectThread" + mSocketType); // Always cancel discovery because it will slow down a connection mAdapter.cancelDiscovery(); // Make a connection to the BluetoothSocket try { mmSocket.connect(); } catch (Exception e) { Log.e(TAG, "Connection to " + mmDevice.getName() + " at " + mmDevice.getAddress() + " failed:" + e.getMessage()); // Close the socket try { mmSocket.close(); } catch (Exception e2) { Log.e(TAG, "unable to close() " + mSocketType + " socket during connection failure", e2); } connectionFailed(e.getMessage()); return; } // Reset the ConnectThread because we're done synchronized (BluetoothChatService.this) { mConnectThread = null; } // Start the connected thread connected(mmSocket, mmDevice, mSocketType); } 

Corresponding log entry (printed when an exception occurs when connect() called):

11-30 10: 23: 51.685: E / BluetoothChatService (2870): connection to ZYNO-700091 at 00: 06: 66: 42: 8E: 01 failed: reading failed, the connector may be closed, read ret: -1

This error has come from time to time. I have an aggressive reconnect system - it basically clogs the connection over and over until it connects, and if it ever disconnects, it clogs it again. Thus, it kills the connection thread and constantly starts from scratch. I thought that there might be a problem - maybe multi-threaded or, possibly, socket cleaning / initialization processing. However, if this is the case, I still expect the first connection attempt to succeed, since this system does not work until there is an unsuccessful connection attempt.

I looked into the source code , throwing an exception. The problem is that the underlying InputStream has no data. Of course, this is not quite the answer, just a step towards it. Why does the stream have no data?

I try not to forget about the potential problem. Am I getting a BluetoothSocket socket correctly? The fact that it was once an intermittent problem and now almost constant makes me suspect multithreading, but that a relatively simple topic in Java compared to C ++ is difficult to spoil if you know what you are doing. In addition, most of this code (in particular, parts related to thread synchronization) directly from the sample code.

The device on the other end is an integrated Bluetooth device, so there is no hope of debugging the problem for this purpose.

UPDATE =============================

It occurred to me that this could be due to updating the OS (I work on Galaxy Nexus phones - I have several tests). So I unpacked a new phone with 4.0.4 and it will work! So, we returned and tested on two original test phones running 4.2, expecting the failure that I saw all this time. Strange, now it works on these phones. I would like to say that I did something to do this work again, but I did not. I'm still puzzled, and now it is also suspicious that this thing will work when I really need it.

I wonder if there is any chance that somehow connecting using 4.0.4 could correctly establish the state of the server module, making it susceptible to 4.2 devices? Just a shot in the dark, I suppose ...

UPDATE 2 ============================

I found that unlocking and reconnecting will allow devices to connect. This is a workaround, but it's better than nothing.

+9
android bluetooth
Nov 30 '12 at 15:41
source share
5 answers

Jellybean has a completely different Bluetooth stack, so differences in version may, of course, trigger something, but this alone does not explain why it works or does not work after connecting to an older device. Could this be due to mating? If this happens again, try disconnecting from the device again and reconnecting.

+8
Dec 03
source share

I know this is an old question. But since I could not find any solution on the Internet, here is a workaround I recently created: IOException: reading failed, the socket may be closed - Bluetooth on Android 4.3

+2
Sep 13 '13 at 12:48 on
source share

I had the same problem when connecting to Arduino via bluetooth module. The problem occurred only when connecting with Arduino, as it smoothly connected with another Android phone with Bluetooth. For me, changing the UUID string worked.

0
Jun 06 '18 at 7:35
source share

In my case, this was due to an invalid UUID in the createRfcommSocketToServiceRecord() function. I want to connect to the SPP serial profile in raspberry pi 3, and I used this UUID:

 private static final UUID MY_UUID_SECURE = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 

I found somewhere on the Android docs page for SPP.

0
Jul 27 '19 at 7:13
source share

I think this might work: first you have to connect to the Internet and install one apk mobile market and try to find FXR WiFi Fix and save and install them and execute, I had a similar problem with my SONY Bluetooth 52 headset now this is normal .

-3
Apr 20 '14 at 9:44
source share



All Articles