Android 蓝牙 LE 拔出设备写入成功(更快的断开连接方法?)

标签 android bluetooth bluetooth-lowenergy

我正在创建一个连接到蓝牙 LE 设备的应用程序。一切正常,但如果设备被拔掉,应用程序需要 10-15 秒才能发送 STATE_DISCONNECTED 广播。如果我在拔下设备后尝试写入设备,我仍然会在我的 BluetoothGattCallback 中将其视为 GATT_SUCCESS。

如果用户尝试更改 BLE 设备上的某些内容并且其写入失败 2-3 次,我将使用它,然后显示断开连接的屏幕(或失去连接只是为了让用户获得更快的反馈)但事实并非如此看起来它会起作用。是否有更快的方法来检查 BLE 设备是否不再可见?

@Override
    public void onCharacteristicWrite(BluetoothGatt gatt,
                                     BluetoothGattCharacteristic characteristic,
                                     int status)
    {
        if (status == BluetoothGatt.GATT_SUCCESS)
        {
            // Is true even if the device has been unplugged?
            Log.i(TAG, "onCharacteristicWrite Success!");
        } else {
            Log.i(TAG, "onCharacteristicWrite Failed!");
        }
    }

最佳答案

我在处理客户端应用程序时遇到了类似的问题。即使在断开连接约 10-15 秒后,该应用程序仍会显示它已连接到 Ble 设备。应用程序不知道设备已断开连接,它仍会尝试保持连接打开,一段时间后会超时。幸运的是,我们的 Ble 设备在应用程序每次写入后都会做出响应。因此,即使 writhecharcetersitc 说写入成功,我也会等待 Ble 设备告诉我它成功接收到我的写入,这解决了我的问题。如果您可以更改固件代码,我建议您采用相同的方法。

关于Android 蓝牙 LE 拔出设备写入成功(更快的断开连接方法?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43212349/

相关文章:

java - 如何与 OBD-II 设备通信

Android View ID 作为字符串

android - 如何从 drawable 引用到 style

Android 设计库 25.1.0 导致 FloatingActionButton.Behavior 停止工作

android - API 19 Android 中的 OnNewIntent

ios - 蓝牙通信可能性

c++ - 如何使用Qt枚举蓝牙设备

ios - 增加 connSupervisionTimeout

raspberry-pi - 需要帮助通过 bluetoothctl 发送字节数组

iOS/swift : Automatically Pair With Bluetooth Classic Device