Disconnect from connected BLE device's GATT server

2.8k views Asked by At

I connect to a BLE device's GATT server in one area of my app and I'd like to disconnect from the device in another area of my app. The problem is that when I want to disconnect, I no longer have access to the BluetoothGatt client object (which has the disconnect() method). Is there any way to disconnect from a BLE device without a reference to a BluetoothGatt?

1

There are 1 answers

0
Doug Koellmer On

I would write your own basic abstraction layer. At its simplest it can be one class with one instance that you have some global reference to. Really basic stub example:

public class MyBleWrapper
{
    private static MyBleWrapper s_instance = new MyBleWrapper();

    private BluetoothDevice m_device;
    private BluetoothGatt m_gatt;

    public static MyBleWrapper getInstance()
    {
        return s_instance;
    }
}

You would then add various methods like connect() and disconnect() that would operate on the m_gatt member. It's a good idea to write your own abstraction layer anyway because of all the quirks and bugs that you will start to discover with Android BLE. It's best to tuck those away behind a clean interface.