在Android BLE中处理指示而不是通知 [英] Handling indications instead of notifications in Android BLE

查看:436
本文介绍了在Android BLE中处理指示而不是通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Bluetooth SIG Application Accelerator代码,可以很好地演示蓝牙低功耗的不同概念.但是,与通知相比,在提及的内容中没有提及任何迹象.我知道与通知不同,需要确认指示,在代码中,我将执行byte[] val = enabled ? BluetoothGattDescriptor.ENABLE_INDICATION_VALUE : BluetoothGattDescriptor.DISABLE_INDICATION_VALUE;而不是byte[] val = enabled ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;,但是还有什么需要做的吗?如何准确地让服务器知道我已收到所需的指示?我需要添加

Using the Bluetooth SIG Application Accelerator code and it does a good job of demonstrating the different concepts of bluetooth low energy. However, in mentions nothing about indications as opposed to notifications. I know that indications need to be acknowledge unlike notifcations, and in the code I would do byte[] val = enabled ? BluetoothGattDescriptor.ENABLE_INDICATION_VALUE : BluetoothGattDescriptor.DISABLE_INDICATION_VALUE; instead of byte[] val = enabled ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;, but is there anything else I need to do? How exactly do I let the server know that I received the indication as that is required? Is there something I need to add in

@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);
        }

?

推荐答案

您所描述的内容就足够了,但是有一个小错误.

What you describe is sufficient, but there's a slight error.

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

Indeed, BLE indications need to be acknowledged by the client whereas notifications do not. However, this is handled entirely behind the scenes by Android. Indications are acknowledged by the system as your onCharacteristicChanged callback gets called.

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

The only difference, which you already found out about, is that you need to enable the right flag in the Client Characteristic Configuration descriptor on the BLE server. For regular notifications, use ENABLE_NOTIFICATION_VALUE. For indications, use ENABLE_INDICATION_VALUE. Note that you disable both by writing DISABLE_NOTIFICATION_VALUE. The DISABLE_INDICATION_VALUE that you mentioned does not exist, as per the documentation!

在Android方面,使用 BluetoothGatt#setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enable) ,其中enable = true.这将同时用于通知和指示.在这两种情况下,都将使用您的onCharacteristicChanged回调.

On the Android side, it's sufficient to use BluetoothGatt#setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enable) where enable = true. This will work for both notifications and indications. In both cases, your onCharacteristicChanged callback will be used.

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

(You've probably already figured this out by now, but posting anyway in case someone ends up here via Google.)

这篇关于在Android BLE中处理指示而不是通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆