Android BluetoothGatt writeCharacteristic with response

dav*_*d72 1 android ios bluetooth-lowenergy

从文档中我看到如何编写没有这样的响应:

BluetoothGattCharacteristic characteristic = ...
characteristic.setValue(bytes);
mBluetoothGatt.writeCharacteristic(characteristic);
Run Code Online (Sandbox Code Playgroud)

如何使用响应执行写请求操作?

(在iOS中,可以选择写入类型CBCharacteristicWriteWithResponse和CBCharacteristicWriteWithoutResponse)

daw*_*ski 7

  1. 你的特征应该是可写的.

检查特征是否可写:

(characteristic.getProperties() & (BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE))) != 0
Run Code Online (Sandbox Code Playgroud)
  1. BluetoothGatt.writeCharacteristic()返回boolean标志,指示写入是否成功(如果是,则返回true).
  2. 一旦BluetoothGatt.writeCharacteristic成功,BluetoothGattCallback.onCharacteristicWrite()将使用您指定的值的特性执行回调.


dav*_*d72 5

从文档:

public void setWriteType (int writeType)

在 API 级别 18 中添加 设置此特性的写入类型

设置特性的写入类型决定了 writeCharacteristic(BluetoothGattCharacteristic) 函数如何写入该特性。

参数 writeType 此特性的写入类型。可以是以下之一:WRITE_TYPE_DEFAULT、WRITE_TYPE_NO_RESPONSE 或 WRITE_TYPE_SIGNED。