代码之家  ›  专栏  ›  技术社区  ›  Pink Jazz

RFCOMM_CreateConnection-已打开州:2,RFC公司州:4,微型断路器州:5-isConnected返回false,无法重新连接

  •  1
  • Pink Jazz  · 技术社区  · 7 年前

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

    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() 方法。不过,就我而言, 断开连接() 返回false,连接仍然失败。

    有人知道这里有什么问题吗?这似乎是个隐晦的错误,因为网络上似乎没有任何关于这个的信息。

    0 回复  |  直到 7 年前