Sebastien FERRAND
Sebastien FERRAND

Reputation: 2150

Android bluetooth sometimes connect sometimes don't

I do the following to connect to an already paired device :

        if (connectCreateRfcommSocketToServiceRecordInvoke()) {
            Log.d(TAG_BT, "Connection created 1");
            connected = true;
        } else if (connectCreateRfcommSocket()) {
            Log.d(TAG_BT, "Connection created 2");
            connected = true;
        } else if (connectSecureRfCom(UUID_DEFAULT)) {
            Log.d(TAG_BT, "Connection created 3");
            connected = true;
        } else if (connectInsecureRfCom(UUID_DEFAULT)) {
            Log.d(TAG_BT, "Connection created 4");
            connected = true;
        }else if (tryConnectDifferentUuid()) {
            Log.d(TAG_BT, "Connection created 5");
            connected = true;
        }

Here are the definitions of the called functions :

 private boolean tryConnectDifferentUuid() {

    ParcelUuid[] supportedUuids = mDevice.getUuids();

    if (supportedUuids != null){
        for (ParcelUuid Uuid : supportedUuids) {
            if (connectSecureRfCom(Uuid.toString())) {
                return true;
            } else if (connectInsecureRfCom(Uuid.toString())) {
                return true;
            }
        }
    }
    return false;
}


private boolean connectSecureRfCom(String UuidString) {
    try {
        Log.d(TAG_BT, "connectSecureRfCom ");
        mmSocket = mDevice.createRfcommSocketToServiceRecord(UUID.fromString(UuidString));
        mmSocket.connect();
        Log.d(TAG_BT, "Connected");
        return true;
    } catch (Exception e) {
        Log.e(TAG_BT, "Couldn't establish Bluetooth connection!", e);
        return false;
    }
}

private boolean connectInsecureRfCom(String UuidString) {
    try {
        Log.d(TAG_BT, "connectInsecureRfCom ");
        mmSocket = mDevice.createInsecureRfcommSocketToServiceRecord(UUID.fromString(UuidString));
        mmSocket.connect();
        Log.d(TAG_BT, "Connected");
        return true;
    } catch (Exception e) {
        Log.e(TAG_BT, "Couldn't establish Bluetooth connection!", e);
        return false;
    }
}

private boolean connectCreateRfcommSocket() {
    try {
        Log.d(TAG_BT, "connectSecureRfComInvoke ");


        mmSocket =(BluetoothSocket) mDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(mDevice,1);
        mmSocket.connect();

        Log.d(TAG_BT, "Connected");

        return true;

    } catch (Throwable e) {
        Log.e(TAG_BT, "Couldn't establish Bluetooth connection!", e);
        return false;
    }
}

private boolean connectCreateRfcommSocketToServiceRecordInvoke() {
    try {
        Log.d(TAG_BT, "createRfcommSocketToServiceRecord ");

        mmSocket =(BluetoothSocket) mDevice.getClass().getMethod("createRfcommSocketToServiceRecord", new Class[] {int.class}).invoke(mDevice,1);
        mmSocket.connect();

        Log.d(TAG_BT, "Connected");

        return true;

    } catch (Throwable e) {
        Log.e(TAG_BT, "Couldn't establish Bluetooth connection!", e);
        return false;
    }
}

Problem is it sometimes work many times in a row, no matter if I open/close the app or disconnect/connect bluetooth, and sometimes it won't work with the following output :

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

Upvotes: 0

Views: 240

Answers (1)

hcknl
hcknl

Reputation: 1249

Well, sockets are closed or timed out after a while if no reading or writing is found. You should have use Sockets in a different thread with a limitless loop.

public class Client extends(Thread or AsyncTask or Runnable ){
    public void startConnection(){socket.connect(); isConnected = socket.isConnected();}

    private void listenSocket(){
    while(isConnected){ listening process }}

    public void stopConnection(){thread.cancel(); isConnected = false;}

}

Upvotes: 1

Related Questions