writeCharacteristic

fun writeCharacteristic(serviceUuid: UUID, characteristicUuid: UUID, value: ByteArray, callback: (Int) -> Unit)

Writes a value to a GATT characteristic on the connected device.

This is an asynchronous operation. The result is delivered via the callback parameter when the write completes or times out.

The state machine must be in ConnectionState.Ready for this operation to succeed. The method will look up the service and characteristic by their UUIDs from the discovered services.

A timeout is automatically applied to this operation. If the device does not respond within operationTimeoutMs, the callback will be invoked with TIMEOUT_ERROR_STATUS.

Note: This method uses the deprecated characteristic.value setter for compatibility. For Android 13+ (API 33+), consider using the newer write methods if available.

Parameters

serviceUuid

The UUID of the GATT service containing the characteristic.

characteristicUuid

The UUID of the characteristic to write to.

value

The byte array value to write to the characteristic.

callback

Invoked with the status code when the operation completes. BluetoothGatt.GATT_SUCCESS indicates success.

See also

Throws

if Bluetooth permissions are not granted.