【问题标题】:mBluetoothGatt.getService(uuid) returns nullmBluetoothGatt.getService(uuid) 返回 null
【发布时间】:2013-10-24 02:55:47
【问题描述】:

在我的应用程序中,我传递了助听器服务的 UUID 编号,如 google 的 BLE 示例中所示,即 0000a00-0000-1000-8000-00805f9b34fb

但是 getservice 返回 null 意味着该服务不受 BluetoothGatt 支持。 为什么会这样,谁能帮帮我。

【问题讨论】:

  • 您发现服务了吗?

标签: android service bluetooth bluetooth-lowenergy


【解决方案1】:

您必须首先根据文档发现给定设备的所有服务。

此功能要求已为给定设备完成服务发现。 http://developer.android.com/reference/android/bluetooth/BluetoothGatt.html#getService(java.util.UUID)

@Override
    // New services discovered
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            BluetoothGattService mBluetoothGattService = mBluetoothGatt.getService(UUID.fromString(serviceUUID));
            if (mBluetoothGattService != null) {
                Log.i(TAG, "Service characteristic UUID found: " + mBluetoothGattService.getUuid().toString());
            } else {
                Log.i(TAG, "Service characteristic not found for UUID: " + serviceUUID);
        }
    }

或者你可以直接搜索

for (BluetoothGattService gattService : gattServices) {
    Log.i(TAG, "Service UUID Found: " + gattService.getUuid().toString());
}

【讨论】:

  • 如果 mBluetoothGatt.getService(UUID.fromString(serviceUUID)) 为空怎么办?怎么了?
【解决方案2】:

尝试对远程数据库进行完整的发现[1],然后遍历服务。可能你弄错了 UUID。

[1]http://developer.android.com/reference/android/bluetooth/BluetoothGatt.html#discoverServices()

【讨论】:

    【解决方案3】:

    您的 UUID 也是错误的。应该是 0000a00X-0000-1000-8000-00805f9b34fb,而不是 0000a00-0000-1000-8000-00805f9b34fb

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-25
      • 1970-01-01
      • 2021-09-26
      • 1970-01-01
      • 2019-01-03
      • 1970-01-01
      • 1970-01-01
      • 2020-06-28
      相关资源
      最近更新 更多