Alex MitrovicBLE & IoT app developer, Android and iOS

iOS & KMP

Kotlin Multiplatform for BLE apps: what to share and what to keep native

How to structure a BLE app with Kotlin Multiplatform: shared protocol and device state in commonMain, BluetoothGatt and CoreBluetooth underneath.

Short answer

In a Kotlin Multiplatform BLE app, share the parts that must behave the same on Android and iOS: the device protocol (framing, parsing, checksums, OTA chunking), the connection state machine, retry policy and device state, behind a small BleLink interface. Implement that interface natively on each platform over BluetoothGatt and CoreBluetooth, because their threading, pairing and background rules really differ.

Key takeaways

  • Share the protocol and the state machine; keep the Bluetooth API calls native.
  • A small interface (connect, write, notifications, state) is the seam between shared and native code.
  • The shared code can be tested with a fake link, without phones, on every commit.
  • Expose StateFlows to SwiftUI with a Swift-friendly wrapper; plan Swift interop early.
  • Libraries like Kable can cover the platform layer; writing it yourself is fine when you need full control.

I build BLE apps for both platforms on a shared Kotlin Multiplatform core, and I led a startup's rebuild from Flutter to KMP. The question I get most is “how much can you share?” For BLE apps the honest answer is: most of the logic, none of the radio.

The split

App architecture for a BLE deviceNative Android and iOS UI on top of one shared Kotlin Multiplatform core with the connection manager, GATT operation queue, protocol and device state, which talks to BluetoothGatt on Android and CoreBluetooth on iOS, and through the radio to the device firmware. Android UIJetpack Compose iOS UISwiftUI Shared core · Kotlin Multiplatform Connection manager GATT queue Protocol + OTA Device state BluetoothGatt CoreBluetooth Your device · firmware
Native UI on top, one shared core in the middle, each platform's Bluetooth API underneath.
LayerWhereWhy
UINative (Compose, SwiftUI) or Compose MultiplatformA product choice; both work
Device state, screens' view modelsSharedBoth apps must show the same state for the same device
Protocol: framing, parsing, CRCs, versionsSharedPure logic; bugs here are the costliest to fix twice
OTA chunking, offsets, verification flowSharedIdentical on both platforms by definition
Connection state machine, retry policySharedSame decisions; inputs mapped from each platform
Operation queueShared on Android's termsAndroid needs it; on iOS it's harmless
Scanning, connecting, GATT callsNativeBluetoothGatt and CoreBluetooth differ in threading, pairing and errors
Background work, permissionsNativeForeground services vs background modes and restoration

The seam: one small interface

commonMain: the interface and a session that uses it
// commonMain
interface BleLink {
    val state: StateFlow<LinkState>                  // Disconnected, Connecting, Ready(mtu), Failed(reason)
    suspend fun connect(id: DeviceId)
    suspend fun disconnect()
    suspend fun write(char: CharId, bytes: ByteArray, withResponse: Boolean = false)
    suspend fun read(char: CharId): ByteArray
    fun notifications(char: CharId): Flow<ByteArray>
}

class DeviceSession(private val link: BleLink, private val protocol: Protocol) {
    val temperature: Flow<Celsius> =
        link.notifications(CharId.Temperature).map(protocol::parseTemperature)

    suspend fun setTarget(t: Celsius) =
        link.write(CharId.Setpoint, protocol.encodeSetpoint(t), withResponse = true)
}

Everything above the interface is shared and testable. Everything below is about thirty methods of platform code per side. On Android it's the familiar BluetoothGatt with a queue; on iOS it's a CBCentralManagerDelegate and CBPeripheralDelegate, written in Kotlin against the CoreBluetooth headers, or in Swift and passed in.

The iOS side in practice

  • Delegates in Kotlin/Native are classes extending NSObject and implementing the protocol. It works, but Swift reads more naturally to iOS developers. If an iOS team will own that layer, let them write it in Swift and implement the shared interface.
  • Threading: give CoreBluetooth its own dispatch queue and turn delegate callbacks into channel sends or CompletableDeferred completions; don't touch shared state from the delegate thread directly.
  • State restoration stays native: the restore identifier, willRestoreState and re-assigning delegates. See state restoration.
  • Flows in Swift: Kotlin Flow and suspend functions aren't idiomatic Swift out of the box. Tools such as SKIE or KMP-NativeCoroutines expose them as async sequences and async functions, which SwiftUI views can consume cleanly.

Library or your own?

Kable (by JUUL Labs) is a Kotlin Multiplatform BLE library that covers the platform layer for Android, iOS and JavaScript with a coroutine API. It's a good default when your needs are standard. I write the layer directly when a project needs full control: unusual bonding flows, a custom DFU bootloader dance, or exact behavior on specific phones. Both approaches keep the same shared core above the seam.

Testing the shared core without phones

The biggest practical win is testing. A FakeBleLink in commonTest can simulate the device: answer writes, push notifications, drop the connection at the worst moment. The protocol, the OTA resume logic and the reconnect policy get tested on every commit, on both platforms' behalf, in seconds. Real phones are still needed for the native layer; see testing on real phones.

commonTest: OTA resume, tested once for both platforms
@Test fun otaResumesAfterDrop() = runTest {
    val link = FakeBleLink(device = FakeOtaDevice(imageSize = 300_000))
    link.dropConnectionAt(bytes = 180_000)
    OtaRunner(link).run(image)
    assertEquals(300_000, link.device.received)   // finished, without starting over
    assertEquals(1, link.device.restarts)
}

Add a lesson from the Flutter-to-KMP rebuild or your own device's app, e.g. what you'd set up differently on day one.

FAQ

Can Kotlin Multiplatform access Bluetooth on iOS?

Yes. Kotlin/Native can call CoreBluetooth directly, or the iOS app can implement a shared Kotlin interface in Swift. Libraries such as Kable also wrap CoreBluetooth for KMP.

Should the BLE layer be shared in a KMP app?

The logic should: protocol, OTA flow, state machine and retry policy. The calls to BluetoothGatt and CoreBluetooth should stay platform-specific behind a small interface, because the platforms behave differently in ways that matter.

Is Kable production-ready?

Kable is widely used and actively maintained, and it's a reasonable default for KMP BLE apps with standard needs. Evaluate it against your device's bonding, OTA and background requirements before committing.

Sources

Seeing this in your own app?

Send me the phone models and what the logs say. On a short call I'll tell you where I'd look first.

Book a free call

Free · NDA on request · reply within 1–2 working days