MockBluetoothServer

class MockBluetoothServer(initialStatus: ServerStatus = ServerStatus.AVAILABLE, setupMocks: Boolean = true) : BluetoothServer

A behavioral mock implementation of BluetoothServer.

The status, isAdvertising and services flows are backed by MutableStateFlows the test can observe, and the methods are also mocked using the Kaluga mocking library so calls can be verified.

Unlike a purely call-recording mock, add actually builds the requested LocalService graph (via buildCapturingLocalService), stores it in servicesState and captures the per-characteristic read/write actions into reads/writes. This lets a test simulate a connected central reading, writing and subscribing through triggerRead, triggerWrite and subscribe without a live Bluetooth stack or a real BluetoothServer.

Parameters

initialStatus

the initial ServerStatus

setupMocks

if true the mocks are configured with sensible default behaviour

Constructors

Link copied to clipboard
constructor(initialStatus: ServerStatus = ServerStatus.AVAILABLE, setupMocks: Boolean = true)

Properties

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

The read actions captured per-descriptor UUID while building services.

Link copied to clipboard

The write actions captured per-descriptor UUID while building services.

Link copied to clipboard
open override val isAdvertising: StateFlow<Boolean>
Link copied to clipboard
val isAdvertisingState: MutableStateFlow<Boolean>

The MutableStateFlow backing isAdvertising

Link copied to clipboard

The read actions captured per-characteristic UUID while building services.

Link copied to clipboard
Link copied to clipboard
open override val services: StateFlow<List<LocalService>>
Link copied to clipboard
val servicesState: MutableStateFlow<List<LocalService>>

The MutableStateFlow backing services

Link copied to clipboard
open override val status: StateFlow<ServerStatus>
Link copied to clipboard
val statusState: MutableStateFlow<ServerStatus>

The MutableStateFlow backing status

Link copied to clipboard

The UUIDs of the characteristics that can be subscribed to.

Link copied to clipboard

The write actions captured per-characteristic UUID while building services.

Functions

Link copied to clipboard
open suspend override fun add(uuid: UUID, service: LocalService.DSL.() -> Unit): LocalService?
Link copied to clipboard
open suspend override fun advertise(data: AdvertiseData.Builder.() -> Unit): Boolean
Link copied to clipboard
open override fun close()
Link copied to clipboard
open suspend override fun remove(service: LocalService): Boolean
Link copied to clipboard
open suspend override fun removeAllServices(): Boolean
Link copied to clipboard
open override fun stopAdvertising()
Link copied to clipboard
fun subscribe(characteristic: UUID, device: ConnectedDevice = MockConnectedDevice())

Simulates a connected central subscribing to characteristic. The characteristic must be a LocalCharacteristic.Notifiable.

Link copied to clipboard
suspend fun triggerDescriptorRead(descriptor: UUID, device: ConnectedDevice = MockConnectedDevice(), offset: Int = 0): GattResponse.ReadResponse

Simulates a connected central reading descriptor by invoking the captured descriptor read action.

Link copied to clipboard
suspend fun triggerDescriptorWrite(descriptor: UUID, value: ByteArray, device: ConnectedDevice = MockConnectedDevice(), offset: Int = 0): GattResponse.WriteResponse

Simulates a connected central writing value to descriptor by invoking the captured descriptor write action.

Link copied to clipboard
suspend fun triggerRead(characteristic: UUID, device: ConnectedDevice = MockConnectedDevice(), offset: Int = 0): GattResponse.ReadResponse

Simulates a connected central reading characteristic by invoking the captured read action.

Link copied to clipboard
suspend fun triggerWrite(characteristic: UUID, value: ByteArray, device: ConnectedDevice = MockConnectedDevice(), offset: Int = 0): GattResponse.WriteResponse

Simulates a connected central writing value to characteristic by invoking the captured write action.