Kotlin Multiplatform BLE library for iOS, Android, macos, windows and javascript
L2capListener)Status: Proposed
Date: 2026-11-25
Deciders: Blue Falcon maintainers and community contributors
Technical Story: Issue #326: L2CAP CoC server is not supported
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:
BluetoothAdapter.listenUsingL2capChannel() /
listenUsingInsecureL2capChannel() return a BluetoothServerSocket with a dynamic PSM.
accept() blocks until a central connects.CBPeripheralManager.publishL2CAPChannelWithEncryption(_:), then
peripheralManager(_:didPublishL2CAPChannel:error:) reports the PSM and
peripheralManager(_:didOpen:error:) delivers a CBL2CAPChannel with a stream pair.Neither platform advertises the PSM. Centrals usually read it from a GATT characteristic.
blue-falcon-peripheralinterface 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)
L2capServerSocket extends the existing core BluetoothSocket. Transport code handles both
directions with one type. BluetoothSocket.peripheral describes the remote central: uuid is
the session id, and the GATT fields are empty.sessionId uses the PeripheralSession.id format (Android address, Apple CBCentral
identifier), so applications can correlate a channel with a GATT session when there is one.BlueFalconPeripheral implementations
still compile. PeripheralCapabilities keeps its previous JVM constructor as a hidden overload.publishL2capChannel is valid only in Running and runs under the manager lifecycle mutex.stop(), close() and the terminal backend-event-overflow path close every listener
before the backend stops. Closing a listener unpublishes the native server, closes queued
and delivered sockets, and completes sockets.sockets with L2capException.Following the bounded-queue pattern of ADR 0007:
L2capInboundBuffer: a 64-chunk bounded channel that starts forwarding
to incoming only after the first collector subscribes. When it is full, the reader stops
reading (blocking read on Android, paused stream drain on Apple). The platform’s L2CAP credit
flow control then pushes back on the central. Bytes sent before the application collects are not
dropped.AndroidL2capServer runs the blocking accept() loop on Dispatchers.IO. Closing the
native server socket unblocks it. FrameworkAndroidBluetoothStack.listenL2cap checks API 29+ and
BLUETOOTH_CONNECT (API 31+).FrameworkApplePeripheralStack serializes publications, because Core Bluetooth reports
didPublishL2CAPChannel without a request id. A late publication after a timeout is
unpublished immediately. Channels opened on unknown PSMs are closed. Retiring the manager
unpublishes every PSM. AppleL2capServerSocket keeps a reference to the CBL2CAPChannel while
open and schedules the streams on the main run loop, like the central-side socket.AndroidL2capServerChannel, AppleL2capChannel, FakePeripheralBackend)
without Bluetooth hardware.L2CapSocket / AppleL2CapSocket
logic. The peripheral module must not depend on the engine modules (ADR 0007), and core has no
platform source sets. Moving both into a shared module is a possible follow-up.tryEmit and can drop
bytes under backpressure, so the two directions currently differ in that respect.capabilities.l2capServer is false there.ABDD3056-28FA-441D-A470-55A75A52553A
(CBUUIDL2CAPPSMCharacteristicString).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.
SharedFlow/Channel for sockets and dataWhy not chosen: contradicts the bounded-resource rules of ADR 0007. A fast central could exhaust memory.
BluetoothAdapter.listenUsingL2capChannelCBPeripheralManager.publishL2CAPChannel(withEncryption:)