Skip to content

BLE device like Heart Rate device can't be disconnected completely when GATT server is opened #631

Description

@SayaSheng1

Hi
Has anyone encountered this issue where a connected BLE device, such as a heart rate monitor, cannot be fully disconnected when a GATT server is running? The BluetoothManager.getConnectionState() function still returns CONNECTED, even though DISCONNECTED is received in BluetoothGattCallback.onConnectionStateChange(). Is there any impact when calling server.cancelConnection(device) in BluetoothGattServerCallback.onConnectionStateChange() after receiving the DISCONNECTED status?

BleServerManager.java

public void onConnectionStateChange(@NonNull final BluetoothDevice device, final int status, final int newState) {
			if (newState == BluetoothGatt.STATE_CONNECTED) {
				log(Log.INFO, "[Server] " + device.getAddress() + " is now connected");
				if (serverObserver != null)
					serverObserver.onDeviceConnectedToServer(device);
			} else if (newState == BluetoothGatt.STATE_DISCONNECTED) {
				if (status == BluetoothGatt.GATT_SUCCESS) {
					log(Log.INFO, "[Server] " + device.getAddress() + " is disconnected");
				} else {
					log(Log.WARN, "[Server] " + device.getAddress() + " has disconnected connected with status: " + status);
				}

				server.cancelConnection(device); // <- add this line

				final BleManagerHandler handler = getRequestHandler(device);
				if (handler != null && handler.bluetoothGatt == null) {
					// There is no client connection to the device.
					// We have to notify disconnection manually.
					handler.notifyDeviceDisconnected(device, status);
				}
				if (serverObserver != null)
					serverObserver.onDeviceDisconnectedFromServer(device);
			}
		}

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions