android - BLE 中的 "reliable write"是什么?

标签 android bluetooth-lowenergy android-bluetooth

在 Android 的 BLE API ( BluetoothGatt ) 中有处理可靠写入的方法:

public boolean beginReliableWrite ()

public void abortReliableWrite (BluetoothDevice mDevice)

public boolean executeReliableWrite ()

它还有一个回调(在 BluetoothGattCallback 中):

public void onReliableWriteCompleted (BluetoothGatt gatt, int status)

我找不到任何相关文档。它是什么?它与“正常”(不可靠?)写法有何不同?

最佳答案

可靠写入允许检查传输的值和一个或多个传输消息的原子执行。

可以在 BLE part of Mozillas Boot 2 Gecko Project documentation 中找到对可靠写入过程的很好解释。 .尽管它是针对 JavaScript 的,但对 beginReliableWrite() 的描述尤其有助于理解该过程:

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.

您开始可靠的写入,

gatt.beginReliableWrite();

设置特征值并写入。

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

writeCharacteristic() 调用将触发其“正常”回调。参数 characteristic 包含可以验证的实际写入值:

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

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

    ...
}

执行可靠写入会触发onReliableWriteCompleted(BluetoothGatt gatt, int status)回调。

关于android - BLE 中的 "reliable write"是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24485536/

相关文章:

android - 如何在手机休眠时保持网络通讯畅通

javascript - phonegap - 菜单按钮不会触发任何东西

android - 如何设置位图大小以在 SurfaceView 上正确显示

java - 从 Firestore 查询数据到 Arraylist 但一无所获?

java - (android textwatcher) 如何在 firebase 上推送 textWatcher

android - ScanCallback onBatchScanResults 被无限期调用

ios - 写入使用NSKeyedArchiver编码的数据时,CoreBluetooth失败?

Android - 获取此设备的蓝牙 UUID

Android Studio layout_behavior 错误

java - 蓝牙/WiFi 直接套接字的高级协议(protocol)?