简体   繁体   中英

Disconnect from connected BLE device's GATT server

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM