FastBle icon indicating copy to clipboard operation
FastBle copied to clipboard

ReprotBug:该库在连接成功后,发现服务时,如果蓝牙关闭,导致服务发现失败,这个时候上层应用收不到连接失败的回调

Open philadelphia opened this issue 9 months ago • 0 comments

该库在连接成功后,发现服务时,如果蓝牙关闭,导致服务发现失败,这个时候时不会回调的 代码:

     @Override
          public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
              super.onConnectionStateChange(gatt, status, newState);
              BleLog.i("BluetoothGattCallback:onConnectionStateChange "
                      + '\n' + "status: " + status
                      + '\n' + "newState: " + newState
                      + '\n' + "currentThread: " + Thread.currentThread().getId());
  
              bluetoothGatt = gatt;
              //此处在蓝牙连接成功后就移除了连接超时的延迟消息,
              mainHandler.removeMessages(BleMsg.MSG_CONNECT_OVER_TIME);
            //然后发送延迟消息发现设备服务
          if (newState == BluetoothProfile.STATE_CONNECTED) {
                Message message = mainHandler.obtainMessage();
                message.what = BleMsg.MSG_DISCOVER_SERVICES;
                mainHandler.sendMessage(message);

            } 

在发现服务成功后才回调连接成功,但是之前连接状态变更就已经移除了连接超时的检测

  case BleMsg.MSG_DISCOVER_SUCCESS: {
     lastState = LastState.CONNECT_CONNECTED;
                    isActiveDisconnect = false;
                    BleManager.getInstance().getMultipleBluetoothController().removeConnectingBle(BleBluetooth.this);
                    BleManager.getInstance().getMultipleBluetoothController().addBleBluetooth(BleBluetooth.this);

                    BleConnectStateParameter para = (BleConnectStateParameter) msg.obj;
                    int status = para.getStatus();
                    if (bleGattCallback != null)
                        bleGattCallback.onConnectSuccess(bleDevice, bluetoothGatt, status);               
                }

此时若蓝牙关闭,导致服务发现失败,上层是无法收到连接失败回调的,因为此时系统不会回调gattCallBack的onServicesDiscovered方法,此时无法得知服务获取失败,这时因为没有了连接超时的检测,倒是上层一直收不到onConnectFail回调,正确做法应该在加一个服务检测的超时计时机制

philadelphia avatar Feb 28 '25 10:02 philadelphia