admin管理员组文章数量:1315247
I am using theKable library for BLE scanning and connection in a Compose Multiplatform (KMP) app for Android and iOS. Scanning works perfectly in both the plateforms, but I am unable to establish a connection with the BLE device. What could be the possible reasons, and how can I fix this issue?
implementation("com.juul.kable:core:0.27.0")
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.juul.kable.Peripheral
import com.juul.kable.Scanner
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
class ScanningViewModel : ViewModel(){
private val scanner = Scanner()
private var connectedPeripheral: Peripheral? = null
private val _devices = MutableStateFlow<List<BleDevice>>(emptyList()) // You can store the device names here
val devices: StateFlow<List<BleDevice>> = _devices
fun startScanning() {
viewModelScope.launch {
try {
// Start scanning and collect advertisements
scanner.advertisements.collect { advertisement ->
val deviceName = advertisement.name ?: "Unknown Device"
val txPower = advertisement.txPower ?: 0
val newDevice = BleDevice(
name = deviceName,
peripheralName = "", // Use peripheral name
txPower = txPower,
)
_devices.update { currentList ->
if (newDevice !in currentList) {
currentList + newDevice // Add the new device if not already present
} else {
currentList // Keep the list unchanged if device already exists
}
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
}
data class BleDevice(
val name: String,
val peripheralName: String,
val txPower: Int?,
)
Note : Connect methos is available in Peripheral object.
Expecting : Peripheral object because it has connect method
I tried with all possible version of kable lib
本文标签: kotlinHow do I make a BLE connection in compose multiplateform App using kable LibraryStack Overflow
版权声明:本文标题:kotlin - How do I make a BLE connection in compose multiplateform App. using kable Library - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741971520a2407867.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论