Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android BluetoothGatt writeCharacteristic with response

From the documentations I see how to write without response like this:

BluetoothGattCharacteristic characteristic = ...
characteristic.setValue(bytes);
mBluetoothGatt.writeCharacteristic(characteristic);

How do you preform a write request operation WITH response?

(In iOS there is option to select write type CBCharacteristicWriteWithResponse and CBCharacteristicWriteWithoutResponse)

like image 464
david72 Avatar asked Aug 12 '26 04:08

david72


2 Answers

  1. Your characteristic should be writable.

Checking whether the characteristic is writable:

(characteristic.getProperties() & (BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE))) != 0
  1. BluetoothGatt.writeCharacteristic() returns boolean flag indicating whether writing succeeded (true if so).
  2. Once the BluetoothGatt.writeCharacteristic succeeds BluetoothGattCallback.onCharacteristicWrite() callback is executed with the characteristic with the value that you specified.
like image 121
dawid gdanski Avatar answered Aug 14 '26 13:08

dawid gdanski


From the documentation:

public void setWriteType (int writeType)

Added in API level 18 Set the write type for this characteristic

Setting the write type of a characteristic determines how the writeCharacteristic(BluetoothGattCharacteristic) function write this characteristic.

Parameters writeType The write type to for this characteristic. Can be one of: WRITE_TYPE_DEFAULT, WRITE_TYPE_NO_RESPONSE or WRITE_TYPE_SIGNED.

like image 33
david72 Avatar answered Aug 14 '26 13:08

david72



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!