From 5df809a4923c26abccd2dd5d02a0b29b150b7b5f Mon Sep 17 00:00:00 2001 From: darken Date: Thu, 5 Jun 2025 18:44:16 +0200 Subject: [PATCH] Improve main device determination logic This commit refines the logic for selecting the main Pod device. Previously, the `RPAChecker` was used directly to verify the Identity Resolving Key (IRK). The new approach leverages the `isIRKMatch` flag within the `ApplePods` class. This flag is set during the device parsing process, indicating whether the device's address matches the configured IRK. This change: - Simplifies the `PodMonitor` by removing the direct dependency on `RPAChecker`. - Encapsulates the IRK matching logic within the `ApplePods` class, improving modularity. - Ensures that the main device selection correctly prioritizes devices whose IRK has been matched. --- .../darken/capod/monitor/core/PodMonitor.kt | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/app-common/src/main/java/eu/darken/capod/monitor/core/PodMonitor.kt b/app-common/src/main/java/eu/darken/capod/monitor/core/PodMonitor.kt index 244a9f1a..f197988d 100644 --- a/app-common/src/main/java/eu/darken/capod/monitor/core/PodMonitor.kt +++ b/app-common/src/main/java/eu/darken/capod/monitor/core/PodMonitor.kt @@ -19,8 +19,8 @@ import eu.darken.capod.main.core.GeneralSettings import eu.darken.capod.main.core.PermissionTool import eu.darken.capod.pods.core.PodDevice import eu.darken.capod.pods.core.PodFactory +import eu.darken.capod.pods.core.apple.ApplePods import eu.darken.capod.pods.core.apple.protocol.ProximityPairing -import eu.darken.capod.pods.core.apple.protocol.RPAChecker import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.delay import kotlinx.coroutines.flow.Flow @@ -33,7 +33,6 @@ import kotlinx.coroutines.flow.onStart import kotlinx.coroutines.flow.retryWhen import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock -import okio.ByteString.Companion.toByteString import java.time.Duration import java.time.Instant import javax.inject.Inject @@ -45,11 +44,10 @@ class PodMonitor @Inject constructor( private val bleScanner: BleScanner, private val podFactory: PodFactory, private val generalSettings: GeneralSettings, - private val bluetoothManager: BluetoothManager2, + bluetoothManager: BluetoothManager2, private val debugSettings: DebugSettings, private val podDeviceCache: PodDeviceCache, - private val permissionTool: PermissionTool, - private val rpaChecker: RPAChecker, + permissionTool: PermissionTool, ) { private val deviceCache = mutableMapOf() @@ -200,13 +198,11 @@ class PodMonitor @Inject constructor( } private fun determineMainDevice(pods: List): PodDevice? { - generalSettings.mainDeviceIdentityKey.value - .also { log(TAG) { "Identity-Resolving-Key (IRK): ${it?.toByteString()}" } } - ?.let { irkKey -> pods.firstOrNull { pod -> rpaChecker.verify(pod.address, irkKey) } } - ?.let { - log(TAG) { "Main device determined via IRK: $it" } - return it - } + val irkHit = pods.filterIsInstance().firstOrNull { it.flags.isIRKMatch } + if (irkHit != null) { + log(TAG) { "Main device determined via IRK: $irkHit" } + return irkHit + } val mainDeviceModel = generalSettings.mainDeviceModel.value val presorted = sortPodsToInterest(pods).sortedByDescending {