Ensure bonding for CGMS profile

This commit is contained in:
himalia416
2025-08-28 14:48:44 +02:00
committed by Himali Aryal
parent 125a022f01
commit 5e69ce44a3

View File

@@ -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()
}
}