10

I have a project that connects to a device over Bluetooth. It used to work fairly reliably, but now it fails the BluetoothSocket.connect() call every time. (Well, I got it to connect once during the thousands of attempts over a 4 hour period.) Most of the code has been taken from the standard sample chat code in the API, with the exception of the common modification in getting the BluetoothSocket device itself:

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

Here's the method of interest, which gets run once a BluetoothSocket is obtained:

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);
}

The relevant log entry (printed when the exception is caught while calling connect()) is this:

11-30 10:23:51.685: E/BluetoothChatService(2870): Connection to ZYNO-700091 at 00:06:66:42:8E:01 failed:read failed, socket might closed, read ret: -1

This error used to come up once in a while. I have an aggressive reconnect system - it basically hammers the connection over and over until it connects, and if it were ever to disconnect, it starts hammering it again. So, it kills the connection thread and starts from scratch constantly. I had considered that there might be an issue there - maybe a multithreading one, or maybe in handling the socket cleanup/initialization. However, if that were the case, I'd still expect the first connection attempt to succeed, since that system doesn't kick in until there's a failed connection attempt.

I looked into the source code throwing the exception. The issue seems to be that the underlying InputStream has no data. Of course, that's not really an answer, just a step towards it. Why would the stream have no data?

I'm trying to keep an open mind about the potential issue. Am I getting the BluetoothSocket properly? The fact that it was once an intermittent issue and is now nearly constant makes me suspect multithreading, but that's a relatively simple topic in Java compared to C++ - hard to screw up if you know what you're doing. Plus, the majority of this code (in particular, the parts dealing with synchronizing the threads) is straight out of the sample code.

The device on the other end is an embedded Bluetooth device, so there's not much hope of debugging the problem from that end.

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

It occurred to me that it might be due to an OS upgrade (I'm running on Galaxy Nexus phones - I have several to test with). So I unpacked a new phone with 4.0.4 and it worked! So then went back and tested on the two original test phones, both running 4.2, expecting the failure I've been seeing all this time. Strangely, now it works on those phones too. I'd like to say I did something to make this work again, but I didn't. I'm still mystified, and now also suspicious that this thing is going to work when I really need it to.

I wonder if there's a possibility that somehow connecting using 4.0.4 could have properly set the state of the server module, making it receptive to the 4.2 devices? Just a shot in the dark, I suppose...

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

I've found that unpairing and re-pairing will allow the devices to connect. It's a workaround, but it's better than nothing.

Todd Sjolander
  • 1,459
  • 1
  • 15
  • 28
  • I have the same issue, I have a code for print vía bluetooth to a Datamax o'neil APEX4 printer. Works fine on a Galaxy Tab 7 plus with Honeycomb 3.2. But in other devices with ICS or higher, don't work. I'm comparing the BluetoothSocket object and are very differents. Are you sure you have not added any code? – ClarkXP Nov 30 '12 at 20:22
  • Jellybean has a completely different Bluetooth stack, so version differences could certainly be triggering something, but that in itself wouldn't explain why it stays working or not-working after connecting with an older device. Could it be to do with pairing? If it happens again, try unpairing from the device and pairing again, and see what happens. – Dan Hulme Nov 30 '12 at 21:04
  • @DanHulme I think you're right, it's the different Bluetooth stack that's causing it. I did some tests with 4.1, and it works fine there. If you want to write this up as an answer, I'd be happy to accept it. – Todd Sjolander Dec 03 '12 at 12:19
  • "It basically hammers the connection over and over until it connects" - thanks! That sentence helped me a lot. I didn't know that you were supposed to do that, so in a Bluetooth app I'm writing I gave up when connect failed. But after changing that to a loop it works. – Thomas Padron-McCarthy May 14 '13 at 14:42
  • @ThomasPadron-McCarthy would you mind sharing your code that made it work? I'm still stuck on this. – Kent Andersen Aug 07 '13 at 18:07
  • 1
    @Kent Andersen: http://basen.oru.se/temporary/MorseBuzzer.zip (But note that that app is a proof of concept for internal use and not a production-quality program.) – Thomas Padron-McCarthy Aug 10 '13 at 19:44
  • @ThomasPadron-McCarthy thanks. So, did this work for you on android 4.2? or 4.3? I tried something pretty close to that and i found myself in a infinite loop of connection errors. – Kent Andersen Aug 12 '13 at 15:32
  • @Kent Andersen: Works on HTC Desire and Nexus 7. I don't remember which Android versions they run and I don't have them here to check. – Thomas Padron-McCarthy Aug 13 '13 at 07:39
  • @ThomasPadron-McCarthy thanks again. If you are using a nexus 7 and have kept it up to date, right now it should be running 4.3. I'll have to try it on my nexus 4 and post later if it worked – Kent Andersen Aug 13 '13 at 17:20

4 Answers4

10

Jellybean has a completely different Bluetooth stack, so version differences could certainly be triggering something, but that in itself wouldn't explain why it stays working or not-working after connecting with an older device. Could it be to do with pairing? If it happens again, try unpairing from the device and pairing again.

Dan Hulme
  • 14,779
  • 3
  • 46
  • 95
  • 2
    I have the very same problem only with Nexus 7, Android4.2.1 - on all other Android versions and other phones/devices it works perfectly. Re-pairing does not help. Is there anything else that needs to be done to make it work again? Many thanks! ps die UUID ist OK – user387184 Jan 14 '13 at 21:35
  • I am also stuck with the same problem, any help would be appreciated. – Anil Maddala Feb 02 '13 at 00:54
2

I know this is kind of an old question. But as I was not able to find any solution on the web, here is a workaround I recently have created: IOException: read failed, socket might closed - Bluetooth on Android 4.3

Community
  • 1
  • 1
matthes
  • 2,972
  • 3
  • 15
  • 18
1

In my case it was due to bad UUID in createRfcommSocketToServiceRecord() function. I want to connect to 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 in android documents' page for SPP.

sbsohrabi
  • 11
  • 2
0

I had the same problem while connecting to the arduino via bluetooth module.The problem only arised while connecting with arduino as it connected smoothly with another android phone bluetooth. What worked for me was changing the UUID string..