onPrepareWriteRequest

open fun onPrepareWriteRequest(characteristic: MockRemoteCharacteristic, value: ByteArray): PrepareWriteResponse

Emulates a prepared write to a characteristic.

The maximum length of the value is guaranteed to be at most 512 bytes.

The response returned by this method will be delayed by one or more connection interval to emulate the time needed to send the response back to the client.

If not overridden, this method calls onWriteRequest and replies back the same data to simplify implementations.

Prepare Write

Prepare Write request is used in Long Write procedure and in Reliable Write procedure. On contrary to how Bluetooth LE works, this mock implementation sends all prepared writes in a single request. This is done to simplify the implementation and avoid the need for queuing multiple requests.

The event handler should reply with the same data to confirm that it was received correctly, or different data to indicate a transmission error, in which case the cancellation will happen automatically.

The client confirms or cancels the procedure by sending an Execute Write request. All prepared writes should be queued until the Execute Write request is received.

Long Write

Android and iOS automatically use Long Write procedure when the value to be written exceeds the maximum write size (MTU - 3 bytes). In this case, multiple Prepare Write requests are sent, each containing a part of the value, followed by an Execute Write request to commit the changes if the data returned in the responses matched the data sent in the requests, or cancel the operation otherwise.

This mock implementation simplifies the procedure by sending all parts in a single Prepare Write request instead of multiple requests.

Read more in Bluetooth Code Specification 6.2, Vol 3 (Host), Part G (GATT), 4.9.4 Write Long Characteristic Value: link.

Reliable Write

Reliable Write procedure is used to ensure that the data is transmitted correctly to the peripheral before committing the changes. The client sends multiple Prepare Write requests, each containing a part of the value, and the peripheral responds with the same data to confirm that it was received correctly. If any part of the data is incorrect, the operation will be cancelled automatically using Execute Write request with execute flag set to false.

This mock implementation simplifies the procedure by sending all parts in a single Prepare Write request instead of multiple requests.

A single Reliable Write procedure may be applied to multiple characteristics or descriptors.

Read more in Bluetooth Code Specification 6.2, Vol 3 (Host), Part G (GATT), 4.9.5 Characteristic Value Reliable Writes: link.

Exceptions

Exceptions thrown by this method are rethrown immediately, without the simulated transfer time, emulating a failure on the client side (e.g. OperationFailedException, or SecurityException).

Simulated error on the peripheral side should be reported by returning PrepareWriteResponse.Failure, which is delayed by one connection interval to emulate the time needed to send the error response.

Any exception other than OperationFailedException will be wrapped into BluetoothException.

Return

The response of the prepare write operation. This emulates a response received from the peripheral and it will be delayed to the client by one or more connection interval.

Parameters

characteristic

The characteristic to write to.

value

The part of the value to be written to the characteristic.

See also

Throws

in case of a client error (reported without a delay).


open fun onPrepareWriteRequest(descriptor: MockRemoteDescriptor, value: ByteArray): PrepareWriteResponse