RFCOMM_CreateConnection-已打开状态:2,RFC状态:4,MCB状态:5-isConnected返回false且无法重新连接

时间:2018-06-25 17:51:20

标签: android bluetooth android-bluetooth rfcomm spp

我有以下代码可以连接到蓝牙设备:

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

如果我尝试重新连接到设备,则会出现以下错误: RFCOMM_CreateConnection - already opened state:2, RFC state:4, MCB state:5

在另一个询问此错误的问题中,有人提到isConnected()方法。但是,就我而言,isConnected()返回false,并且连接仍然失败。

有人知道这是什么问题吗?似乎这是一个晦涩的错误,因为网络上似乎没有关于此的任何内容。

0 个答案:

没有答案