ble的写/确认/通知/指示

默认情形,characteristic write是需要确认的。对端设备将发送确认消息过来,其他GATT流量才会继续。
为提升吞吐,可以用 write without response, 对端设备发出 notifications ,结合起来提升吞吐率


外围设备可以发出notification或者indication来通知中央设备,来进行读写。
indication从外围设备发出后,中央设备需要对其进行确认。虽然这个确认是蓝牙协议栈做出的,跟应用层没有关系,但这也会降低速度。
当Android系统对 indication确认后, 会回调应用层的 onCharacteristicChanged 方法。

如果是notification, 那么 Client 应该去 设置 Server 上的 Client Characteristic Configuration descriptor为 ENABLE_NOTIFICATION_VALUE。
如果是indication, 则应该设置为 ENABLE_INDICATION_VALUE

在Android SDK中,调用 BluetoothGatt#setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enable) 将enable 设置为 true, 就可以了, 对于 notifications 和 indication都这样处理。

gatt.setCharacteristicNotification(characteristic, true);
...
UUID uuid = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(uuid);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);

Indications and Notifications are a way for a GATT Client to subscribe to data provided by a GATT Server.
A Notification is an unacknowledged message or update while an Indication is an acknowledged message or update.
These Notifications and Indications are sent any time the relevant data in the GATT table on the GATT Server is updated. (You must “subscribe” to the data that you would like to be Notified or Indicated of) In a way Indications and Notifications are much like TCP and UDP packets. TCP requires that when data is sent, the receiver acknowledges that the data has been received by sending back an ACKnowledgement packet. UDP just sends off data without any concern whether it is actually confirmed to be received or not. In this sense Indications are akin to TCP and Notifications are akin to UDP.

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注