【问题标题】:Android GATT Services returns Null after many successful connection多次成功连接后,Android GATT 服务返回 Null
【发布时间】:2014-06-18 03:50:30
【问题描述】:

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

onServicesDiscovered(BluetoothGatt gatt, int status)

状态为 0 [表示 GATT_SUCCESS]。

现在当我尝试获取 BluetoothGattService 时:

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

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

【问题讨论】:

  • 如果您无法理解,在什么情况下提出了问题,请不要在这里提出您的问题。正如我所看到的,有两个用户已经降级了这个问题。如果您想了解有关问题的更多信息,请询问并添加评论。这种行为不是专业的观点。所以请保持这个。

标签: android bluetooth bluetooth-lowenergy


【解决方案1】:

您必须首先发现给定设备的所有服务,否则当您运行 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());
            }
        }
    }

【讨论】:

    【解决方案2】:

    你好,因为蓝牙低能量是unstable。并且正如您的问题定义的那样,您将获得带有 0 状态代码的回调函数。然后而不是使用 getServivce(UUID)。您可以使用getServices() 方法来发现可用的服务。这可能是 uuid 问题!可能你的 UUID 弄错了..

    【讨论】:

    • UUID 是正确的,因为我能够获得服务,但正如我在 7 次尝试开/关 BT 或连接断开设备后提到的,带有 UUID 的 getService 和 getServices() 都返回 null。
    猜你喜欢
    • 2015-02-17
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    • 1970-01-01
    • 2014-03-28
    • 2016-03-01
    • 1970-01-01
    相关资源
    最近更新 更多