【问题标题】:How do I make sure app is disconnecting from a Bluetooth LE device?如何确保应用程序与蓝牙 LE 设备断开连接?
【发布时间】:2016-01-19 18:28:26
【问题描述】:

我有两个要连接的设备。当我离开应用程序时,我将与设备断开连接。两者都经历相同的过程,但有时其中一个设备会保持连接,直到我强制关闭应用程序。

我的设备上有指示灯,确认它仍然认为它已连接,并且应用程序的其他实例无法连接到它,直到我强制关闭第一个实例。 在下面的日志中,第一个列出的设备保持连接状态。

//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

在我调用 gatt.close() 之后,我抓取了 BluetoothManager 并在已连接设备列表中查找我的设备。它在列表中。

来自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;
        }
    }
}

这个类被添加到一个命令队列中。

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;
    }
}

【问题讨论】:

  • 尽管关闭了 GATT 数据库,但您在列表中看到该设备的原因是 Android 的蓝牙堆栈已将其缓存。请参阅this,这可能会给你一个线索。
  • 它仅在设备的指示灯 lite 也打开时显示在列表中。该解决方案适用于发现未断开连接的服务。
  • 在调用close()之前你是否调用了BluetoothGatt的disconnect()方法?
  • 是的。我在上面的日志中标记了我调用这两个方法的位置。
  • 贴一些代码看看哪里出了问题会有所帮助,查看上面的日志并不完全有帮助。

标签: android bluetooth bluetooth-lowenergy android-ble


【解决方案1】:

你必须先打电话

bluetoothGatt.disconnect();

那你要等待回调被调用才能成功关闭连接

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.");
                }
            }

【讨论】:

    【解决方案2】:

    尝试:

    public void disconnect() {
        if (blegatt== null) {
            Log.w(TAG, "BluetoothAdapter not initialized");
        }
        else {
            bleGatt.disconnect();
            close();
            gatt = null;
        }
    }
    /**
     * After using a given BLE device, the app must call this method to ensure resources are
     * released properly.
     */
    public void close() {
        if (gatt == null) {
            return;
        }
        gatt.close();
    }
    

    【讨论】:

      猜你喜欢
      • 2018-05-22
      • 1970-01-01
      • 2021-05-21
      • 2012-10-09
      • 2014-10-04
      • 2015-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多