BluetoothGattServer cancelConnection does not cancel the connection
Asked Answered
H

3

15

I have Android application which exposes BLE Server. I connect with BluetoothGattServer#connect. It works - my app gets call to BluetoothGattServerCallback#onConnectionStateChange with STATE_CONNECTED. When I'm done with the client I try to disconnect from my app with BluetoothGattServer#cancelConnection.

But I do not get call to BluetoothGattServerCallback#onConnectionStateChange and it seems that the connection is still active as my BLE client does not start to advertise (which it does when nothing is connected to it).

In logcat I see only:

BluetoothGattServer: cancelConnection() - device: XX:XX:XX:XX:XX:XX

The funny part is, my app gets call to BluetoothGattServerCallback#onConnectionStateChange with STATE_DISCONNECTED as soon as I turn off BT completely.

Similar issues in Google's tracker: 63461 and 63464.

Hamper answered 4/8, 2016 at 8:53 Comment(3)
iirc cancelConnection is completely broken on most of the early devices when Android ble first come out and Google decided not to fix that.Honor
Did you manage to find a fix for this? I'm struggling with the same thingPuerility
Nope - I decided to move as away from BLE as I can and never come close again.Izabel
E
11

When newState==BluetoothProfile.STATE_CONNECTED, you have to call BluetoothGattServer.connect();.

@Override
public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
    super.onConnectionStateChange(device, status, newState);
    if (newState == BluetoothProfile.STATE_CONNECTED){
        mDevice = device;
        mBluetoothGattServer.connect(device, false);
    }else {
        mDevice = null;
    }
}

private void cancelConnection(){
    if (mDevice != null) {
        mBluetoothGattServer.cancelConnection(mDevice);
    }
}
Embouchure answered 11/4, 2019 at 6:23 Comment(5)
you wrote that based on what?Izabel
63461#8, add I used it.Embouchure
#16 says: Samsung S8+ running Android version 8.0.0 and Samsung Experience version 9.0 does not work for comment 8. HTC running Android version 8.0.0 does not work either.Izabel
Oh, I can work with Pixel-8.1 as peripheral. Didn't try Samsung phones.Embouchure
Confirmed on Android 14 Samsung device.Scalf
F
2

Encountering same issue when calling disconnect() method.. no disconnect is given in onConnectionStateChange in my BluetoothGattCallback.

Cycling Bluetooth seems the be the only thing that works.

edit: also, after disconnect() and close() method are called, I am still connected according to this code:

public int getConnectedBLEDevices() {
        int i = 0;
        List<BluetoothDevice> devices = mBluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
        for(BluetoothDevice device : devices) {
            if(device.getType() == BluetoothDevice.DEVICE_TYPE_LE) {
                Logs.writeEvent(TAG+".getConnectedBLEDevices()", device.getAddress() + "\n"+ getStateAsString(mBluetoothManager.getConnectionState(device, BluetoothProfile.GATT)));
                i++;
            }
        }
        return i;
    }
Faggot answered 8/8, 2016 at 21:16 Comment(3)
What do you mean by "Cycling Bluetooth seems the be the only thing that works. "?Izabel
Turning the bluetooth on and offFaggot
Did you manage to find a fix for this? I'm struggling with the same thingPuerility
F
0

pls see https://issuetracker.google.com/issues/37127644

Status: Won't Fix (Intended Behavior) You must call BluetoothGattServer.connect() to mark connection as used, then BluetoothGattServer.disconnect() to mark it as no longer used. Then after a timeout stack can decide to disconnect from the remote if no one else is using the connection. If BluetoothGattServer.connect() is not called after the connection is established, then the stack is keeping the connection until some gatt client/server app start using this connection.

Farmer answered 18/4, 2019 at 9:17 Comment(1)
There is no such method BluetoothGattServer.disconnect()Gilus

© 2022 - 2024 — McMap. All rights reserved.