MeshNetwork

A BLE Mesh network.

MeshNetwork is the central entry point for BLE Mesh operations. It manages the network state (keys, nodes, IV Index), provides connectivity via GATT Proxy, and enables sending and receiving mesh messages.

Lifecycle

  • Created via MeshNetwork factory function with a builder DSL.

  • connectProxy establishes mesh connectivity through a proxy node.

  • send transmits mesh messages to any address on the network.

  • close disconnects the proxy and releases all resources.

Usage

val network = MeshNetwork {
networkKey(myNetKey)
applicationKey(myAppKey)
element(MeshElement(0, UnicastAddress(0x0001u), ElementLocation.MAIN,
listOf(MeshModelId.GenericOnOffClient)))
}

network.connectProxy(proxyPeripheral)
network.send(destination, MeshModelId.GenericOnOffServer,
MeshOpcode(0x8202u), byteArrayOf(0x01), myAppKey)
network.close()

Inheritors

Properties

Link copied to clipboard

Application keys registered in this network.

Link copied to clipboard

Client for configuring nodes post-provisioning.

Link copied to clipboard
abstract val incomingMessages: Flow<MeshMessage>

Flow of incoming mesh messages addressed to our models.

Link copied to clipboard
abstract val isProxyConnected: StateFlow<Boolean>

Whether we are currently connected to a proxy node.

Link copied to clipboard
abstract val ivIndex: StateFlow<IvIndex>

Current IV Index of the network.

Link copied to clipboard
abstract val networkKeys: List<NetworkKey>

Network keys registered in this network.

Link copied to clipboard
abstract val nodes: StateFlow<List<MeshNode>>

All provisioned nodes in this network.

Link copied to clipboard

Our unicast address on this mesh network.

Functions

Link copied to clipboard
abstract suspend fun addApplicationKey(key: ApplicationKey)

Add an application key to this network.

Link copied to clipboard
abstract suspend fun addNetworkKey(key: NetworkKey)

Add a network key to this network.

Link copied to clipboard
abstract suspend fun addNode(node: MeshNode)

Add a provisioned node to this network.

Link copied to clipboard
abstract override fun close()

Close the network and release all resources.

Link copied to clipboard
abstract suspend fun connectProxy(peripheral: Peripheral): ProxyConnection

Connect to the mesh network via a GATT Proxy node.

Link copied to clipboard
abstract suspend fun disconnectProxy()

Disconnect from the current proxy node.

Link copied to clipboard

Find a node by its primary unicast address.

Link copied to clipboard
abstract suspend fun removeNode(address: MeshAddress.UnicastAddress)

Remove a node from this network.

Link copied to clipboard
abstract suspend fun send(destination: MeshAddress, modelId: MeshModelId, opcode: MeshOpcode, payload: ByteArray, appKey: ApplicationKey, acknowledged: Boolean = true, ttl: UByte): MeshMessageResponse?

Send a mesh message to a destination address.

Link copied to clipboard
abstract suspend fun sendConfig(destination: MeshAddress, opcode: MeshOpcode, payload: ByteArray, deviceKey: DeviceKey, acknowledged: Boolean = true, ttl: UByte): MeshMessageResponse?

Send a configuration message encrypted with the node's DeviceKey.