3

I´m writing an android app that connects to a device through bluetooth using RFCOMM. I use the BluetoothChat example as basis for establishing a connection and everything works perfectly most of the time.

However, sometimes I cannot reconnect due to a message that the socket is already open:

RFCOMM_CreateConnection - already opened state:2, RFC state:4, MCB state:5

This tends to happen if I connect to the device, close the app (call onDestroy()), reopen it and try to connect again, which results in the above.

I use this method for connecting in the ConnectThread(ref.BluetoothChat example):

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

The only thing that resolves this problem is turning off/on the Bluetooth of the Android phone.

This leads me to believe that the socket is not being closed in onDestroy() but still I´m calling on closing all threads as shown in the before mentioned example.
Any ideas would be appreciated.

user1725145
  • 3,993
  • 2
  • 37
  • 58
  • I solved this by doing a check in my main activity, where onResume checks for connection status and restards the bluetooth connection accordingly. – Guðrún Hauksdóttir Oct 25 '11 at 14:59
  • Hey, this might be super random on a 5 year old question, but would you happen to remember what device you observed this on? I work at Google and I'm trying to hunt down the root cause of this. – Zach Johnson May 12 '16 at 06:29
  • @ZachJohnson As I am having exactly this problem in 2016 and trying to solve it for one of my business partners. Device is HTC Flyer P510e. I hope this will help a little. – Lvka May 18 '16 at 07:52
  • @Lvka which version of Android is it running? – Zach Johnson May 18 '16 at 23:17
  • @ZachJohnson Firmware version is "htc_wwe/htc_flyer/flyer:3.2.1/HTK75C/205068.1:user/release-keys", It is Honeycomb (3.2.1) – Lvka May 19 '16 at 10:26

1 Answers1

4

I stumbled upon this one too, and here is the answer I found:

This error may happen, if you open and close a bluetooth socket connection multiple times.

Solution

Starting from API Level 14 there is a Method in BluetoothSocket called isConected(), which returns true, if this socket is already connected and false otherwise, here the original excerpt from the API:

Get the connection status of this socket, ie, whether there is an active connection with remote device.

For API levels < 14 you can work around this issue by putting your Bluetooth Handling Thread to sleep after closing the connection - 1000 ms should be enough, here is an example (btDevice is of the type BluetoothDevice and has been initialized prior to the code snippet below):

    try {
        //Open the socket to an SPP device (UUID taken from Android API for createRfcommSocketToServiceRecord)
        BluetoothSocket btSocket = btDevice.createRfcommSocketToServiceRecord("00001101-0000-1000-8000-00805F9B34FB");
        //Connect to the socket
        btSocket.connect();
        //Close the socket
        btSocket.close();
        //Sleep time of 1000ms after closing the socket
        SystemClock.sleep(POST_RESET_DELAY);

    } catch (Throwable e) {
        // Log error message
    }

P.s. Instead of SystemClock.sleep you can also use Thread.sleep - however the SystemCock's sleep can't be interrupted, whereas the Thread.sleep can be interrupted, so it depends on your use-case which option better suits your purpose.

Source: Louis A. Prado

AgentKnopf
  • 4,295
  • 7
  • 45
  • 81