Blue-Falcon

Kotlin Multiplatform BLE library for iOS, Android, macos, windows and javascript

View the Project on GitHub Reedyuk/blue-falcon

ADR 0016: Peripheral-Role L2CAP CoC Server (L2capListener)

Status: Proposed

Date: 2026-11-25

Deciders: Blue Falcon maintainers and community contributors

Technical Story: Issue #326: L2CAP CoC server is not supported

Context

The central side can open L2CAP connection-oriented channels (BlueFalcon.openL2capChannel, returning BluetoothSocket). The peripheral module (ADR 0007) can only exchange data through GATT notifications and writes. For bulk transfer (files, images, mesh message batches) GATT is several times slower than a CoC channel, because every notification pays ATT overhead and is limited by the ATT MTU.

Both platforms provide a CoC server:

Neither platform advertises the PSM. Centrals usually read it from a GATT characteristic.

Decision

1. Public API in blue-falcon-peripheral

interface BlueFalconPeripheral {
    // ...
    suspend fun publishL2capChannel(secure: Boolean = true): L2capListener // default: throws L2capException
}

interface L2capListener {
    val psm: Int
    val secure: Boolean
    val isOpen: Boolean
    val sockets: Flow<L2capServerSocket>   // single consumer, completes on close
    fun close()
}

interface L2capServerSocket : BluetoothSocket {
    val sessionId: PeripheralSessionId
}

data class PeripheralCapabilities(/* ... */, val l2capServer: Boolean = false)

2. Lifecycle owned by the common manager

3. Bounded queues, loss-free inbound data

Following the bounded-queue pattern of ADR 0007:

4. Platform backends

Consequences

Positive

Negative

Neutral

Alternatives Considered

Depend on the engine modules to reuse their socket classes

Pros: no duplicated IO code. Cons: breaks the ADR 0007 dependency direction, and peripheral-only apps would inherit the central engine manifest, including location permissions. Why not chosen: the dependency and permission cost is larger than the duplicated code.

Unbounded SharedFlow/Channel for sockets and data

Why not chosen: contradicts the bounded-resource rules of ADR 0007. A fast central could exhaust memory.

References