From 5e69ce44a31977f05eea960291f3519857e16ba1 Mon Sep 17 00:00:00 2001 From: himalia416 Date: Thu, 28 Aug 2025 14:48:44 +0200 Subject: [PATCH] Ensure bonding for CGMS profile --- .../android/service/profile/ProfileService.kt | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/lib_service/src/main/java/no/nordicsemi/android/service/profile/ProfileService.kt b/lib_service/src/main/java/no/nordicsemi/android/service/profile/ProfileService.kt index 9f2e17b9..73ae78d3 100644 --- a/lib_service/src/main/java/no/nordicsemi/android/service/profile/ProfileService.kt +++ b/lib_service/src/main/java/no/nordicsemi/android/service/profile/ProfileService.kt @@ -19,6 +19,7 @@ import kotlinx.coroutines.launch import no.nordicsemi.android.log.timber.nRFLoggerTree import no.nordicsemi.android.service.NotificationService import no.nordicsemi.android.service.R +import no.nordicsemi.android.toolbox.lib.utils.spec.CGMS_SERVICE_UUID import no.nordicsemi.android.toolbox.profile.manager.ServiceManager import no.nordicsemi.android.toolbox.profile.manager.ServiceManagerFactory import no.nordicsemi.android.ui.view.internal.DisconnectReason @@ -33,6 +34,7 @@ import no.nordicsemi.kotlin.ble.core.WriteType import timber.log.Timber import javax.inject.Inject import kotlin.uuid.ExperimentalUuidApi +import kotlin.uuid.toKotlinUuid @AndroidEntryPoint internal class ProfileService : NotificationService() { @@ -175,9 +177,8 @@ internal class ProfileService : NotificationService() { manager: ServiceManager ) { try { -// if (manager.requiresBonding(service.uuid) && peripheral.bondingState != BondState.BONDED) { -// peripheral.ensureBonded() -// } + if (service.uuid == CGMS_SERVICE_UUID.toKotlinUuid()) + peripheral.ensureBonded() manager.observeServiceInteractions(peripheral.address, service, lifecycleScope) } catch (e: Exception) { Timber.tag("ObserveServices").e(e) @@ -258,11 +259,14 @@ internal class ProfileService : NotificationService() { getPeripheral(address)?.ensureBonded() } } -} -// Helper extension function for bonding -private suspend fun Peripheral.ensureBonded() { - if (this.bondState.value == BondState.BONDED) return - // Create bond and wait until bonded. - createBond() + /** + * Ensures the peripheral is bonded. If not, initiates bonding. + */ + private suspend fun Peripheral.ensureBonded() { + if (this.bondState.value == BondState.BONDED) return + // Create bond and wait until bonded. + createBond() + } + } \ No newline at end of file