onWriteRequest

open fun onWriteRequest(peripheral: MockPeripheral, characteristic: MockRemoteCharacteristic, value: ByteArray): WriteResponse

A callback called when the client sends a Write Request (write with response) to a characteristic.

The maximum length of the value is guaranteed to be at most PeripheralSpec.mtu- 3 byte. 1 byte is consumed by the OpCode and 2 by the handle number.

For Long Write procedure and Reliable Write procedure, see onPrepareWriteRequest, which replies back the written value.

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

Read more in Bluetooth Code Specification 6.2, Vol 3 (Host), Part G (GATT), 4.9.3 Write Characteristic Value: 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 WriteResponse.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 write operation. This emulates a response received from the peripheral, and it will be delayed to the client by one connection interval.

Parameters

peripheral

The peripheral the event is for. It can be used to simulate its behavior.

characteristic

The characteristic to write to.

value

The value written to the characteristic.

See also

Throws

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


open fun onWriteRequest(peripheral: MockPeripheral, descriptor: MockRemoteDescriptor, value: ByteArray): WriteResponse

A callback called when the client sends a Write Request (write with response) to a characteristic descriptor.

The maximum length of the value is guaranteed to be at most PeripheralSpec.mtu- 3 byte. 1 byte is consumed by the OpCode and 2 by the handle number.

For Long Write procedure and Reliable Write procedure, see onPrepareWriteRequest, which replies back the written value.

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

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

Client Characteristic Configuration descriptor

This method is also called when the client enables or disables notifications or indications by writing to the Client Characteristic Configuration descriptor (CCCD). The state of the CCCD is updated by the mock implementation only when WriteResponse.Success is returned. Return WriteResponse.Failure to reject the request, e.g. with OperationStatus.InsufficientAuthentication. Mind, that returning an error for unknown descriptors will also reject writes to the CCCD.

This method is called before the Write Response is sent to the client, so notifications sent from here using PeripheralSpec.simulateValueUpdate may be received by the client before the response.

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 WriteResponse.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 write operation. This emulates a response received from the peripheral, and it will be delayed to the client by one connection interval.

Parameters

peripheral

The peripheral the event is for. It can be used to simulate its behavior.

descriptor

The characteristic descriptor to write to.

value

The value written to the characteristic descriptor.

Throws

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