Skip to content

Commit

Permalink
Added fix for duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
Reedyuk committed Aug 7, 2024
1 parent 217dacb commit 0701fb4
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/KotlinMP-Example/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
2 changes: 1 addition & 1 deletion examples/KotlinMP-Example/iosApp/iosApp/DevicesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion library/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion library/src/appleMain/kotlin/dev/bluefalcon/BlueFalcon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlueFalconDelegate> = mutableSetOf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<BluetoothService>>(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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 0701fb4

Please sign in to comment.