在Android BLE中处理指示而不是通知

shr*_*day 7 android bluetooth-lowenergy

使用蓝牙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);
        }
Run Code Online (Sandbox Code Playgroud)

Dri*_*yen 17

你所描述的就足够了,但是有一点点错误.

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

您已经发现的唯一区别是,您需要在BLE服务器上的客户端特性配置描述符中启用右侧标志.对于常规通知,请使用ENABLE_NOTIFICATION_VALUE.适用于适应症,请使用ENABLE_INDICATION_VALUE.请注意,您通过写入禁用它们DISABLE_NOTIFICATION_VALUE.根据DISABLE_INDICATION_VALUE文档,你提到的那个不存在!

在Android方面,它足以在BluetoothGatt#setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enable)哪里使用enable = true.这适用于通知和指示.在这两种情况下,onCharacteristicChanged都会使用您的回调.

(你现在可能已经想到了这一点,但无论如何都要发帖,以防有人通过谷歌来到这里.)

  • 它确实帮助了通过谷歌在这里结束的人 (4认同)