Kotlin Multiplatform BLE library for iOS, Android, macos, windows and javascript
This example demonstrates Blue Falcon 2.x with Compose Multiplatform using the legacy callback-based API.
This is the 2.x (legacy) version of Blue Falcon. For new projects, use the 3.0 version which features:
This is a complete Compose Multiplatform application showing:
ComposeMultiplatform-Legacy-Example/
├── shared/
│ ├── src/
│ │ ├── commonMain/kotlin/
│ │ │ ├── ble/
│ │ │ │ ├── data/
│ │ │ │ │ ├── BleDelegate.kt # Callback delegate
│ │ │ │ │ └── DeviceEvent.kt # Event types
│ │ │ │ └── presentation/
│ │ │ │ ├── BluetoothDeviceViewModel.kt
│ │ │ │ └── component/ # UI components
│ │ │ ├── di/
│ │ │ │ └── AppModule.kt # DI setup
│ │ │ └── App.kt
│ │ ├── androidMain/
│ │ └── iosMain/
│ └── build.gradle.kts
├── androidBlueFalconExampleMP/
└── iosBlueFalconExampleMP/
Implements BlueFalconDelegate for callback-based event handling:
class BleDelegate: BlueFalconDelegate {
override fun didConnect(bluetoothPeripheral: BluetoothPeripheral) {
// Handle connection
}
override fun didDiscoverServices(bluetoothPeripheral: BluetoothPeripheral) {
// Handle service discovery
}
// ... more callbacks
}
Platform-specific Blue Falcon initialization:
// commonMain
expect class AppModule {
val blueFalcon: BlueFalcon
}
// androidMain
actual class AppModule(private val context: Context) {
actual val blueFalcon: BlueFalcon = BlueFalcon(context, delegate)
}
// iosMain
actual class AppModule {
actual val blueFalcon: BlueFalcon = BlueFalcon(delegate)
}
./gradlew :androidBlueFalconExampleMP:installDebug
Open in Xcode:
open iosBlueFalconExampleMP/iosBlueFalconExampleMP.xcodeproj
See MIGRATION_GUIDE.md for step-by-step migration instructions.
// Setup with delegate
class BleDelegate: BlueFalconDelegate {
override fun didConnect(peripheral: BluetoothPeripheral) {
// Handle connection
}
}
val blueFalcon = BlueFalcon(context, delegate)
// Scan (no return value)
blueFalcon.scan()
// Connect (callback-based)
blueFalcon.connect(peripheral)
// Result comes via didConnect callback
// Setup with DSL
val blueFalcon = BlueFalcon {
install(LoggingPlugin)
}
// Observe devices with Flow
blueFalcon.peripherals.collect { devices ->
// Update UI
}
// Scan (suspend function)
blueFalcon.scan()
// Connect (suspend function with result)
try {
blueFalcon.connect(peripheral)
// Connected successfully
} catch (e: Exception) {
// Handle error
}
This legacy version is maintained for compatibility but new development should use 3.0.