diff --git a/examples/KotlinMP-Example/gradle/libs.versions.toml b/examples/KotlinMP-Example/gradle/libs.versions.toml index 96160a7..e6df225 100644 --- a/examples/KotlinMP-Example/gradle/libs.versions.toml +++ b/examples/KotlinMP-Example/gradle/libs.versions.toml @@ -15,7 +15,7 @@ compose-ui-tooling = { module = "androidx.compose.ui:ui-tooling", version.ref = compose-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview", version.ref = "compose" } compose-foundation = { module = "androidx.compose.foundation:foundation", version.ref = "compose" } compose-material3 = { module = "androidx.compose.material3:material3", version.ref = "compose-material3" } -blue-falcon = { module = "dev.bluefalcon:blue-falcon", version = "1.1.0" } +blue-falcon = { module = "dev.bluefalcon:blue-falcon", version = "1.1.1" } kmm-viewmodel = { module = "com.rickclephas.kmp:kmp-observableviewmodel-core", version = "1.0.0-BETA-3" } androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" } compose-permissions = { group = "com.github.dawidraszka.compose-permission-handler", name = "core", version = "1.5.0" } diff --git a/examples/KotlinMP-Example/iosApp/iosApp/DevicesView.swift b/examples/KotlinMP-Example/iosApp/iosApp/DevicesView.swift index c74dddd..08c4cdb 100644 --- a/examples/KotlinMP-Example/iosApp/iosApp/DevicesView.swift +++ b/examples/KotlinMP-Example/iosApp/iosApp/DevicesView.swift @@ -16,7 +16,7 @@ struct DevicesView: View { Text("Scan") } List { - ForEach(viewModel.devices, id: \.name) { device in + ForEach(viewModel.devices, id: \.uuid) { device in Text(device.name ?? device.uuid) } } diff --git a/library/gradle.properties b/library/gradle.properties index b95910b..ecab497 100644 --- a/library/gradle.properties +++ b/library/gradle.properties @@ -10,7 +10,7 @@ org.gradle.configureondemand = false android.useAndroidX=true android.enableJetifier=true -version=1.1.0 +version=1.1.1 group=dev.bluefalcon libraryName=blue-falcon kotlinx_coroutines_version=1.8.1 diff --git a/library/src/androidMain/kotlin/dev/bluefalcon/BlueFalcon.kt b/library/src/androidMain/kotlin/dev/bluefalcon/BlueFalcon.kt index 50819c8..99aac6c 100644 --- a/library/src/androidMain/kotlin/dev/bluefalcon/BlueFalcon.kt +++ b/library/src/androidMain/kotlin/dev/bluefalcon/BlueFalcon.kt @@ -259,7 +259,7 @@ actual class BlueFalcon actual constructor(private val context: ApplicationConte val bluetoothPeripheral = BluetoothPeripheral(device) bluetoothPeripheral.rssi = scanResult.rssi.toFloat() - _peripherals.tryEmit(_peripherals.value.filter{ it.uuid != bluetoothPeripheral.uuid }.toSet() + setOf(bluetoothPeripheral)) + _peripherals.tryEmit(_peripherals.value + setOf(bluetoothPeripheral)) delegates.forEach { it.didDiscoverDevice(bluetoothPeripheral, sharedAdvertisementData) } diff --git a/library/src/androidMain/kotlin/dev/bluefalcon/BluetoothPeripheral.kt b/library/src/androidMain/kotlin/dev/bluefalcon/BluetoothPeripheral.kt index 45f35af..f962f07 100644 --- a/library/src/androidMain/kotlin/dev/bluefalcon/BluetoothPeripheral.kt +++ b/library/src/androidMain/kotlin/dev/bluefalcon/BluetoothPeripheral.kt @@ -21,6 +21,15 @@ actual class BluetoothPeripheral(val bluetoothDevice: BluetoothDevice) : Parcela rssi = parcel.readValue(Float::class.java.classLoader) as? Float } + override fun toString(): String = uuid + + override fun hashCode(): Int = uuid.hashCode() + + override fun equals(other: Any?): Boolean { + if (other == null || other !is BluetoothPeripheral) return false + return other.uuid == uuid + } + override fun writeToParcel(parcel: Parcel, flags: Int) { parcel.writeParcelable(bluetoothDevice, flags) parcel.writeValue(rssi) diff --git a/library/src/appleMain/kotlin/dev/bluefalcon/BlueFalcon.kt b/library/src/appleMain/kotlin/dev/bluefalcon/BlueFalcon.kt index 4a882a6..8d5efa8 100644 --- a/library/src/appleMain/kotlin/dev/bluefalcon/BlueFalcon.kt +++ b/library/src/appleMain/kotlin/dev/bluefalcon/BlueFalcon.kt @@ -5,7 +5,6 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.MutableStateFlow import platform.CoreBluetooth.* import platform.Foundation.* -import platform.darwin.NSObject actual class BlueFalcon actual constructor(private val context: ApplicationContext) { actual val delegates: MutableSet = mutableSetOf() diff --git a/library/src/appleMain/kotlin/dev/bluefalcon/BluetoothPeripheral.kt b/library/src/appleMain/kotlin/dev/bluefalcon/BluetoothPeripheral.kt index c6ff9b8..bf70bba 100644 --- a/library/src/appleMain/kotlin/dev/bluefalcon/BluetoothPeripheral.kt +++ b/library/src/appleMain/kotlin/dev/bluefalcon/BluetoothPeripheral.kt @@ -11,8 +11,15 @@ actual class BluetoothPeripheral(val bluetoothDevice: CBPeripheral, val rssiValu get() = bluetoothDevice.services?.map { BluetoothService(it as CBService) } ?: emptyList() - actual val uuid: String - get() = bluetoothDevice.identifier.UUIDString + actual val uuid: String = bluetoothDevice.identifier.UUIDString internal actual val _servicesFlow = MutableStateFlow>(emptyList()) + + override fun toString(): String = uuid + + override fun hashCode(): Int = uuid.hashCode() + override fun equals(other: Any?): Boolean { + if (other == null || other !is BluetoothPeripheral) return false + return other.uuid == uuid + } } \ No newline at end of file diff --git a/library/src/appleMain/kotlin/dev/bluefalcon/BluetoothPeripheralManager.kt b/library/src/appleMain/kotlin/dev/bluefalcon/BluetoothPeripheralManager.kt index 3e7ace1..05afc48 100644 --- a/library/src/appleMain/kotlin/dev/bluefalcon/BluetoothPeripheralManager.kt +++ b/library/src/appleMain/kotlin/dev/bluefalcon/BluetoothPeripheralManager.kt @@ -31,11 +31,10 @@ class BluetoothPeripheralManager constructor( RSSI: NSNumber ) { if (blueFalcon.isScanning) { - log("Discovered device ${didDiscoverPeripheral.name}") + log("Discovered device ${didDiscoverPeripheral.name}:${didDiscoverPeripheral.identifier.UUIDString}") val device = BluetoothPeripheral(didDiscoverPeripheral, rssiValue = RSSI.floatValue) - val sharedAdvertisementData = mapNativeAdvertisementDataToShared(advertisementData) - blueFalcon._peripherals.tryEmit(blueFalcon._peripherals.value.filter{ it.uuid != device.uuid }.toSet() + setOf(device)) + blueFalcon._peripherals.tryEmit(blueFalcon._peripherals.value.plus(device)) blueFalcon.delegates.forEach { it.didDiscoverDevice( bluetoothPeripheral = device,