What is “reliable write” in BLE?

六眼飞鱼酱① 提交于 2019-11-29 09:22:37
bottersb

Reliable write allows checking back transmitted values and atomic execution of one or mutliple transmitted messages.

A good explaination of the reliable write procedure can be found in the BLE part of Mozillas Boot 2 Gecko Project documentation. Even though it's meant for JavaScript the description of beginReliableWrite() in particular is very helpful for understanding the process:

Once a reliable write transaction has been initiated, all calls to characteristic.writeValue() are sent to the remote device for verification and queued up for atomic execution. An Promise that carries the written value is returned in response to every characteristic.writeValue() call and the application is responsible for verifying whether the value has been transmitted accurately. After all characteristics have been queued up and verified, executeReliableWrite() will execute all writes. If a characteristic was not written correctly, calling abortReliableWrite() will cancel the current transaction without committing any values on the remote LE device.

You begin the reliable write,

gatt.beginReliableWrite();

set the value of the characteristic and write it.

characteristic.setValue(value);
gatt.writeCharacteristic(characteristic);

The writeCharacteristic() call will trigger its 'normal' callback. The parameter characteristic contains the actual, written value which can be verified:

@Override
public void onCharacteristicWrite(BluetoothGatt gatt,
                BluetoothGattCharacteristic characteristic, 
                int status) {
    ...

    if(characteristic.getValue() != value) { 
        gatt.abortReliableWrite();
    } else {
        gatt.executeReliableWrite();
    }

    ...
}

Executing the reliable write will trigger the onReliableWriteCompleted(BluetoothGatt gatt, int status) callback.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!