【问题标题】:Handling indications instead of notifications in Android BLE在 Android BLE 中处理指示而不是通知
【发布时间】:2021-01-29 11:43:22
【问题描述】:

使用蓝牙 SIG 应用加速器代码,它很好地展示了蓝牙低功耗的不同概念。但是,它没有提到与通知相反的指示。我知道与通知不同,需要确认指示,并且在代码中我会使用byte[] val = enabled ? BluetoothGattDescriptor.ENABLE_INDICATION_VALUE : BluetoothGattDescriptor.DISABLE_INDICATION_VALUE; 而不是byte[] val = enabled ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;。还有什么我需要做的吗?我究竟如何让服务器知道我收到了所需的指示?有什么需要补充的吗?

@Override
        public void onCharacteristicChanged(BluetoothGatt gatt,
                                            BluetoothGattCharacteristic characteristic)
        {

            notification_id++;
            Log.d("BleWrapper","notification count = " + notification_id);
            // characteristic's value was updated due to enabled notification, lets get this value
            // the value itself will be reported to the UI inside getCharacteristicValue
            getCharacteristicValue(characteristic);
            // also, notify UI that notification are enabled for particular characteristic
            mUiCallback.uiGotNotification(mBluetoothGatt, mBluetoothDevice, mBluetoothSelectedService, characteristic);
        }

【问题讨论】:

    标签: android bluetooth-lowenergy


    【解决方案1】:

    你描述的足够了,但是有一点错误。

    确实,BLE 指示需要由客户端确认,而通知则不需要。但是,这完全由 Android 在幕后处理。当您的 onCharacteristicChanged 回调被调用时,系统会确认指示。

    您已经发现的唯一区别是您需要在 BLE 服务器上的客户端特征配置描述符中启用正确的标志。对于定期通知,请使用ENABLE_NOTIFICATION_VALUE。如需指示,请使用ENABLE_INDICATION_VALUE。请注意,您可以通过编写DISABLE_NOTIFICATION_VALUE 禁用both。根据文档,您提到的 DISABLE_INDICATION_VALUE 不存在!

    在 Android 端,使用 BluetoothGatt#setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enable) where enable = true 就足够了。这适用于通知和指示。在这两种情况下,都将使用您的 onCharacteristicChanged 回调。

    (您现在可能已经想通了,但无论如何都要发布,以防有​​人最终通过 Google 来到这里。)

    【讨论】:

    • 如何使用 gatttool 之类的工具手动执行此操作?我正在尝试从 BLE 温度计获取温度数据,并指示特征属性。我读到我必须写入客户端特征配置,但我不明白如何。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-20
    • 1970-01-01
    相关资源
    最近更新 更多