多次成功连接后,Android GATT 服务返回 Null

标签 android bluetooth bluetooth-lowenergy

我正在开发一个连接到 BLE 设备的 Android 应用程序,设备在大多数时间确实连接并且我能够捕获读数。但在多次连接断开后,蓝牙开/关,我的 BluetoothGattCallback 类方法

onServicesDiscovered(BluetoothGatt gatt, int status)

状态为 0 [这意味着 GATT_SUCCESS]。

现在当我尝试获取 BluetoothGattService 时:

BluetoothGattService service = gatt.getService(getServiceUUID());

它返回 null,因此我无法执行后续步骤。 请帮我找出问题所在。

最佳答案

您必须首先发现给定设备的所有服务,否则当您运行 BluetoothGattService service = gatt.getService(getServiceUUID()); 时,它将返回 null。

我建议您添加 onServicesDiscovered 函数并改用 gatt.discoverServices();

@Override
    // New services discovered
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            for (BluetoothGattService gattService : gattServices) {
                Log.i(TAG, "Service UUID Found: " + gattService.getUuid().toString());
            }
        }
    }

关于多次成功连接后,Android GATT 服务返回 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23411573/

相关文章:

android - 如何在 android 中基于 BT 免提配置文件发送 AT 命令?

安卓应用 : Connecting to GATT server using UUIDs

flutter - 当int通常为32或64位时, “int”如何表示一个字节(8位)?

Elixir - Nerves BLE 库推荐

Android Handler 不处理消息

java - 暂停和恢复线程绘制到 SurfaceView

android - 使用 HC-05 将连续数据从 arduino 发送到 Android 应用程序

c - 如何通过BLE双模式广播图像

安卓openCV

android - 如何在 LaunchedEffect 中使用 LocalContext?