简体   繁体   English

断开连接的BLE设备的GATT服务器的连接

[英]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. 我在应用程序的一个区域中连接到BLE设备的GATT服务器,我想与应用程序的另一个区域中的设备断开连接。 The problem is that when I want to disconnect, I no longer have access to the BluetoothGatt client object (which has the disconnect() method). 问题是,当我想断开连接时,我不再有权访问BluetoothGatt客户端对象(该对象具有disconnect()方法)。 Is there any way to disconnect from a BLE device without a reference to a BluetoothGatt ? 没有引用BluetoothGatt方法是否可以从BLE设备断开连接?

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. 然后,您将添加可在m_gatt成员上运行的各种方法,例如connect()disconnect() 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. 无论如何,最好编写自己的抽象层,因为您将开始在Android BLE中发现所有怪癖和错误。 It's best to tuck those away behind a clean interface. 最好将它们藏在干净的界面后面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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