Skip to content

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

Description

@philadelphia

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

     @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回调,正确做法应该在加一个服务检测的超时计时机制

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions