【问题标题】:Android BLE Disconnecting after multiple devices connect多个设备连接后Android BLE断开连接
【发布时间】:2021-09-02 13:12:32
【问题描述】:

我正在使用 android BLE。在这个任务中,android 应用程序应该连接到多个 BLE 设备,这没关系。我可以连接成功。

我知道这个代码需要连接BLE设备:

BluetoothGatt gaat = device.connectGatt(getApplicationContext(), false, gattCallback);

我使用Map<String, BluetoothGatt> connectedDeviceMap 来保留BLE 设备addressesBluetoothGatt

    List<BluetoothDevice> devices = bluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
    if (devices != null && !(devices.isEmpty())) {
        for (BluetoothDevice device : devices) {
            if (device.getType() == BluetoothDevice.DEVICE_TYPE_LE) {
                BluetoothDevice d = bluetoothAdapter.getRemoteDevice(device.getAddress());
                gatt = d.connectGatt(getApplicationContext(), false, gattCallback);
                connectedDeviceMap.put(d.getAddress(), gatt);
            }
        }
    }

如您所见,为了获取每个设备的 gatt 值,我需要调用 connectGatt 函数。但是当此函数多次调用时,设备与应用程序断开连接(我认为这是因为 BLE 的内存已满)

所以我使用close() 释放所有内存并再次连接:

 List<BluetoothDevice> devices = bluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
if (devices != null && !(devices.isEmpty())) {
    for (BluetoothDevice device : devices) {
        if (device.getType() == BluetoothDevice.DEVICE_TYPE_LE) {
            BluetoothDevice d = bluetoothAdapter.getRemoteDevice(device.getAddress());
            gatt = d.connectGatt(getApplicationContext(), false, gattCallback);
            connectedDeviceMap.put(d.getAddress(), gatt);
            gatt .close();
        }
    }
}

但这不起作用,并且设备在多次调用connectGatt时与应用程序断开连接。

【问题讨论】:

    标签: java android android-studio bluetooth bluetooth-lowenergy


    【解决方案1】:

    就像这个例子中的Android BLE multiple connections。 如果我在链接中看到您的解决方案与此解决方案之间的正确差异,您是否没有检查设备连接状态

    【讨论】:

    • 我在另一个代码中检查了设备连接状态。但问题仍未解决
    • 您在连接前和关闭前检查?
    • 是的,我在连接之前检查了是否在忽略之前连接,之后是否在忽略并继续之前关闭
    猜你喜欢
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 2016-10-05
    • 2016-05-25
    相关资源
    最近更新 更多