Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make sure app is disconnecting from a Bluetooth LE device?

I have two devices that I'm connecting to. When I leave the app I'm disconnecting from the devices. Both go through the same process but sometimes one of the devices maintains a connection until I force close the app.

I have indicator lights on the devices confirming that it still thinks it's connected and other instances of the app cannot connect to it until I force close the first. In the log below the first listed device stayed connected.

//call gatt.disconnect();
BluetoothGatt: cancelOpen() - device: F0:3D:A0:04:CA:E7
BluetoothGatt: onClientConnectionState() - status=0 clientIf=7 device=F0:3D:A0:04:CA:E7

//wait for connection state change callback then call gatt.close();
BluetoothGatt: close()
BluetoothGatt: unregisterApp() - mClientIf=7

//call gatt.disconnect();
BluetoothGatt: cancelOpen() - device: FF:A9:CA:EF:08:A4
BluetoothGatt: onClientConnectionState() - status=0 clientIf=5 device=FF:A9:CA:EF:08:A4

//wait for connection state change callback then call gatt.close();
BluetoothGatt: close()
BluetoothGatt: unregisterApp() - mClientIf=5

After I call gatt.close() I grab the BluetoothManager and look for my device in the list of connected devices. It's in the list.

From BluetoothGattCallback

public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
    super.onConnectionStateChange(gatt, status, newState);
    final String address = gatt.getDevice().getAddress();
    if (newState == BluetoothProfile.STATE_CONNECTED) {
        onConnected(gatt, address);
    } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
        onDisconnected(gatt, address);
    }
}

private void onDisconnected(BluetoothGatt gatt, String address) {
    Log.v(TAG, address + " disconnected");
    if (devices.containsKey(address)) {
        devices.get(address).setState(Connection.STATE_DISCONNECTED);
        connect(gatt.getDevice());
    } else {
        gatt.close();
        verifyDisconnect(gatt, address);
    }
}

private void verifyDisconnect(BluetoothGatt gatt, String address) {
    BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
    final List<BluetoothDevice> devices = bluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
    for (final BluetoothDevice device : devices) {
        if (device.getAddress().equals(address)) {
            Log.d(TAG, address + " is still connected!!!!");
            addCommand(new DisconnectCommand(gatt));
            break;
        }
    }
}

This class is added to a command queue.

public class DisconnectCommand extends BluetoothCommand {

    private final BluetoothGatt gatt;

    public DisconnectCommand(BluetoothGatt gatt) {
        this.gatt = gatt;
        priority = BluetoothCommand.DISCONNECT_PRIORITY;
    }

    public boolean execute() {
        gatt.disconnect();
        return true;
    }
}
like image 861
Jeff Engebretsen Avatar asked Nov 10 '22 03:11

Jeff Engebretsen


1 Answers

You have to call first

bluetoothGatt.disconnect();

Then you have to wait for the callback to be called so that you can close the connection successfully

public void onConnectionStateChange(BluetoothGatt gatt, int status,
                                                int newState) {
                if (newState == BluetoothProfile.STATE_CONNECTED) {
                    Log.d(TAG, "Connected to GATT server.");
                    bluetoothGatt.discoverServices(); // onServicesDiscovered callback will handle the action
                } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                    bluetoothGatt.close();
                    bluetoothGatt = null;
                    Log.d(TAG, "Disonnected fromActivity GATT server.");
                }
            }
like image 115
Javier Castellanos Cruz Avatar answered Nov 14 '22 23:11

Javier Castellanos Cruz