Kotlin Multiplatform BLE library for iOS, Android, macos, windows and javascript
This example demonstrates Blue Falcon 3.0 with Compose Multiplatform using the new coroutine and Flow-based API.
This is the 3.0 version of Blue Falcon featuring:
For comparison with the legacy 2.x API, see the Legacy Example.
This is a complete Compose Multiplatform application showing:
ComposeMultiplatform-3.0-Example/
├── shared/
│ ├── src/
│ │ ├── commonMain/kotlin/
│ │ │ ├── ble/
│ │ │ │ ├── data/ # (BleDelegate not needed in 3.0)
│ │ │ │ └── presentation/
│ │ │ │ ├── BluetoothDeviceViewModel.kt # Flow-based ViewModel
│ │ │ │ └── component/ # UI components
│ │ │ ├── di/
│ │ │ │ └── AppModule.kt # Engine setup
│ │ │ └── App.kt
│ │ ├── androidMain/ # Android engine
│ │ └── iosMain/ # iOS engine
│ └── build.gradle.kts # Blue Falcon 3.0 dependencies
├── androidBlueFalconExampleMP/
└── iosBlueFalconExampleMP/
3.0 (This Example):
// Collect peripherals from StateFlow
blueFalcon.peripherals.collect { devices ->
// Update UI with discovered devices
}
3.0 (This Example):
// All operations are suspend functions
CoroutineScope(Dispatchers.IO).launch {
try {
blueFalcon.scan() // Start scanning
blueFalcon.connect(peripheral) // Connect
blueFalcon.discoverServices(peripheral) // Discover services
blueFalcon.readCharacteristic(peripheral, characteristic) // Read
blueFalcon.writeCharacteristic(peripheral, characteristic, value) // Write
} catch (e: Exception) {
// Handle errors inline
}
}
3.0 (This Example):
// androidMain
actual class AppModule(context: Context) {
actual val blueFalcon: BlueFalcon = BlueFalcon(
engine = AndroidEngine(context)
).apply {
// Install logging plugin for debugging
plugins.install(LoggingPlugin(LoggingPlugin.Config().apply {
level = LogLevel.DEBUG
logDiscovery = true
logConnections = true
logGattOperations = true
})) { }
// Install retry plugin for better reliability
plugins.install(RetryPlugin(RetryPlugin.Config().apply {
maxRetries = 3
initialDelay = Duration.parse("1s")
})) { }
}
}
// iosMain
actual class AppModule {
actual val blueFalcon: BlueFalcon = BlueFalcon(
engine = IosEngine()
).apply {
// Same plugin configuration...
}
}
Logs all BLE operations for debugging:
Configuration:
plugins.install(LoggingPlugin(LoggingPlugin.Config().apply {
level = LogLevel.DEBUG // Minimum log level
logDiscovery = true // Log device scanning
logConnections = true // Log connect/disconnect
logGattOperations = true // Log read/write/notify
})) { }
Automatically retries failed operations with exponential backoff:
Configuration:
plugins.install(RetryPlugin(RetryPlugin.Config().apply {
maxRetries = 3 // Maximum retry attempts
initialDelay = Duration.parse("1s") // Initial delay
backoffMultiplier = 2.0 // Exponential backoff
})) { }
} } ```
3.0 (This Example):
// androidMain
actual class AppModule(context: Context) {
actual val blueFalcon: BlueFalcon = BlueFalcon(
engine = AndroidEngine(context)
)
}
// iosMain
actual class AppModule {
actual val blueFalcon: BlueFalcon = BlueFalcon(
engine = IosEngine()
)
}
./gradlew :androidBlueFalconExampleMP:installDebug
cd shared
./gradlew :shared:linkDebugFrameworkIosSimulatorArm64
open iosBlueFalconExampleMP/iosApp.xcodeproj
commonMain.dependencies {
// Blue Falcon 3.0 Core
implementation("dev.bluefalcon:blue-falcon-core:3.0.0")
// Optional plugins
implementation("dev.bluefalcon:blue-falcon-plugin-logging:3.0.0")
implementation("dev.bluefalcon:blue-falcon-plugin-retry:3.0.0")
}
androidMain.dependencies {
// Android Engine
implementation("dev.bluefalcon:blue-falcon-engine-android:3.0.0")
}
iosMain.dependencies {
// iOS Engine
implementation("dev.bluefalcon:blue-falcon-engine-ios:3.0.0")
}
See MIGRATION_GUIDE.md for complete migration instructions.
This legacy version is maintained for compatibility but new development should use 3.0.