2

I am able to connect to an external paired bluetooth hardware first time. After that if i repeat connect/disconect procedures sometimes getting exception.

Exception = read failed, socket might closed or timeout, read ret: -1

and after many trials able to connect again.Sometimes second trial itself is successful

The issue is observed with Devices:Nexux7(version 4.3) and MotoG(Kitkat)

Code for connection:

private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(devAddress);      
socket = device.createRfcommSocketToServiceRecord(MY_UUID);

and calling socket.connect() from asynctask

Code for closing socket:Subject

if (in != null) {
            Logger.loge(CLASS_NAME + "." + "resetConnection", "in != null");
        try {
            in.close();
        } catch (Exception e) {
           // Log.d(TAG,"exception in closing inputstream - " + e.getMessage());
        }
        in = null;
    }
if (out != null) {
    Logger.loge(CLASS_NAME + "." + "resetConnection", "out != null");

    try {
        out.close();
    } catch (Exception e) {
       // Log.d(TAG,"exception in closing outputstream - " + e.getMessage());
    }

    out = null;
}
if (socket != null) {
    Logger.loge(CLASS_NAME + "." + "resetConnection", "socket != null");

    try {
        socket.close();
    } catch (Exception e) {
        //Log.d(TAG,"exception in closing socket - " + e.getMessage());
    }
    socket = null;
}

I have followed the links

https://groups.google.com/forum/#!topic/android-developers/UxY5xME6V5s

Android Bluetooth: java.io.IOException: Service discovery failed

Disconnect a bluetooth socket in Android

android bluetooth can't connect

None of the solution provided help me to solve the issue.

Any help will be appreciated...

Thanks

Community
  • 1
  • 1
heyjii
  • 834
  • 8
  • 26

2 Answers2

0

The Actual problem is once both the device the is connected, the socket will be open. When either one of the socket is closed, other one is not closed.When you try to reconnect the other device ll not accept the new Socket.

The solution is when any one gets disconnect you need to re-initialize the connection service on both the side,ie you need to close the socket properly on both the side. and go back to the listen mode. then only the new socket connection will accept.click Here code reference using AndroiddChat example.

Yuvaraja
  • 715
  • 6
  • 22
  • We are unable to re-initialize the connection on both ends programmatically , as one of them is an external hardware, not a phone. – heyjii Apr 16 '14 at 10:57
0

I had encountered a similar problem when I built an app involving bluetooth connectivity. After searching for a long time, I found this solution.

Community
  • 1
  • 1
saran
  • 413
  • 1
  • 3
  • 14