1

I have the following code to connect to a Bluetooth device:

class BiSymConnectThread extends Thread {
    BluetoothDevice mDevice;

    public BiSymConnectThread(BluetoothDevice device) throws SecurityException, NoSuchMethodException {

        mDevice = device;

        UUID uuid = mDevice.getUuids()[0].getUuid();
        try {
            biSymSocket = mDevice.createInsecureRfcommSocketToServiceRecord(uuid);
        } catch (IOException e) {
            Log.e("Error", "Could not connect!");
        }
    }

    public void cancel() {
        interrupt();
        try {
            Log.i("Treadmill", "in connect thread cancellation");
            if (biSymSocket != null) {
                biSymSocket.close();
            }
        } catch (IOException localIOException) {
            Log.e("Treadmill", "exception + " + localIOException.getMessage());

        }
    }

    public void run() {
        try {

            if (biSymSocket.isConnected()) {
                biSymSocket.close();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new IOException();
                }
            }

           biSymSocket.connect();
            eventHandler.obtainMessage(MESSAGE_CONNECT_BISYM, 0, 0, "").sendToTarget();
            BluetoothConnectionService.setSocket(biSymSocket);
            BluetoothConnectionService.sendMessage(biSymSocket, "S");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                Log.e("Error", "InterruptedException: " + e.getMessage(), e);
                throw new IOException();
            }
        } catch (IOException e) {
            Log.e("Error", "IOException: " + e.getMessage(), e);
            eventHandler.obtainMessage(MESSAGE_ERRORCONNECT_BISYM, 0, 0, "").sendToTarget();
            if (biSymSocket != null) {
                try {
                    biSymSocket.close();
                } catch (IOException e1) {
                    Log.e("Error", "Can't close socket!");
                }
            }
        }
        synchronized (this) {
            biSymConnectThread = null;
        }
    }
}

If I attempt to reconnect to the device, I get the following error: RFCOMM_CreateConnection - already opened state:2, RFC state:4, MCB state:5

In the other question asking about this error, someone mentions the isConnected() method. However, in my case, isConnected() returns false and the connection still fails.

Does anyone know what is the problem here? It appears this is some obscure error, since there doesn't seem to be anything on the web about this.

Pink Jazz
  • 784
  • 4
  • 13
  • 34

0 Answers0