simulatePeripheral

fun <ID : Any> simulatePeripheral(identifier: ID, addressType: AddressType = AddressType.RANDOM_STATIC, type: PeripheralType = PeripheralType.LE, proximity: Proximity = Proximity.IMMEDIATE, builder: PeripheralSpec.Builder<ID>.() -> Unit = {}): PeripheralSpec<ID>

Simulates a peripheral with given ID and proximity.

Note, that if the identifier is a Bluetooth Address, the 2 most significant bits should match the addressType:

See Bluetooth Core Specification v6.2, Vol 6, Part B, Section 1.3 Device address: link.

Example

val peripheralSpec = PeripheralSpec.simulatePeripheral(
identifier = "12:34:56:78:9A:BC",
addressType = AddressType.RANDOM_STATIC,
proximity = Proximity.NEAR
) {
advertising(
parameters = LegacyAdvertisingSetParameters(
connectable = true,
interval = 500.milliseconds,
),
isAdvertisingWhenConnected = false,
delay = 1.seconds,
// timeout = 10.seconds,
// maxAdvertisingEvents = 30,
) {
CompleteLocalName("Nordic_LBS")
ServiceUuid(Uuid.parse("00001523-1212-EFDE-1523-785FEABCD123"))
IncludeTxPowerLevel()
}
advertising(
parameters = Bluetooth5AdvertisingSetParameters(
connectable = true,
interval = 1.seconds,
primaryPhy = PrimaryPhy.PHY_LE_CODED,
secondaryPhy = Phy.PHY_LE_CODED,
txPowerLevel = TxPowerLevel.HIGH,
includeTxPowerLevel = true,
),
delay = 4.seconds,
timeout = 10.seconds,
) {
Flags(
AdvertisingDataFlag.LE_GENERAL_DISCOVERABLE_MODE,
AdvertisingDataFlag.BR_EDR_NOT_SUPPORTED
)
CompleteLocalName("HR Sensor")
ServiceUuid(Uuid.fromShortUuid(0x1809))
ServiceUuid(Uuid.fromShortUuid(0x180A))
}
connectable(
name = "Nordic_Blinky",
maxAttMtu = 247,
maxL2capMtu = 251,
// Uncommenting this line switches to a different "connectable" method, which
// makes the peripheral "cached" (there's additional param "cachedServices" to provide).
// In that case, the mock impl assumes, that the peripheral was connected before
// and services were cached, i.e. the Device Name was read. Hence, the scanner
// will switch from "Nordic_LBS" to "Nordic_Blinky".
//
// isBonded = false,
eventHandler = blinkyImpl,
) {
GenericAccessService()
GenericAttributeService()
// Add LED Button Service (Blinky)
Service(
uuid = Uuid.parse("00001523-1212-EFDE-1523-785FEABCD123")
) {
Characteristic(
uuid = Uuid.parse("00001524-1212-EFDE-1523-785FEABCD123"),
properties = CharacteristicProperty.READ and CharacteristicProperty.WRITE,
permissions = Permission.READ and Permission.WRITE,
) {
// CCCD is added automatically
CharacteristicUserDescriptionDescriptor("Button 1")
}
Characteristic(
uuid = Uuid.parse("00001525-1212-EFDE-1523-785FEABCD123"),
properties = CharacteristicProperty.READ and CharacteristicProperty.NOTIFY,
permission = Permission.READ,
) {
// CCCD is added automatically
CharacteristicUserDescriptionDescriptor("LED 1")
}
}
}
}

@param identifier The peripheral identifier.
@param addressType The address type of the peripheral, default is [AddressType.RANDOM_STATIC].
@param type The peripheral type. By default set to [PeripheralType.LE].
@param proximity Approximate distance to the peripheral.
By default set to [Proximity.IMMEDIATE].