Added comments

This commit is contained in:
himalia416
2025-08-28 14:49:12 +02:00
committed by Himali Aryal
parent 5e69ce44a3
commit c4056420f6

View File

@@ -73,6 +73,9 @@ internal class ProfileService : NotificationService() {
super.onDestroy() super.onDestroy()
} }
/**
* Initiates a connection to the peripheral with the given address.
*/
private fun connect(address: String) { private fun connect(address: String) {
// Return if already managed to avoid multiple connection jobs. // Return if already managed to avoid multiple connection jobs.
if (managedConnections.containsKey(address)) return if (managedConnections.containsKey(address)) return
@@ -91,13 +94,16 @@ internal class ProfileService : NotificationService() {
Timber.e(e, "Failed to connect to $address") Timber.e(e, "Failed to connect to $address")
} }
// Observe connection state changes and react accordingly. // Observe connection state changes.
observeConnectionState(peripheral) observeConnectionState(peripheral)
} }
managedConnections[address] = job managedConnections[address] = job
} }
/**
* Observes the connection state of the given peripheral and updates the service state accordingly.
*/
@OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class)
private fun CoroutineScope.observeConnectionState(peripheral: Peripheral) { private fun CoroutineScope.observeConnectionState(peripheral: Peripheral) {
peripheral.state peripheral.state
@@ -135,6 +141,9 @@ internal class ProfileService : NotificationService() {
}.launchIn(this) }.launchIn(this)
} }
/**
* Discovers services on the peripheral and sets up observation for each recognized service.
*/
@OptIn(ExperimentalUuidApi::class) @OptIn(ExperimentalUuidApi::class)
private fun discoverAndObserveServices( private fun discoverAndObserveServices(
peripheral: Peripheral, peripheral: Peripheral,
@@ -170,7 +179,10 @@ internal class ProfileService : NotificationService() {
} }
/**
* Observes interactions for a specific service on the peripheral.
*/
@OptIn(ExperimentalUuidApi::class)
private suspend fun observeService( private suspend fun observeService(
peripheral: Peripheral, peripheral: Peripheral,
service: RemoteService, service: RemoteService,
@@ -185,6 +197,9 @@ internal class ProfileService : NotificationService() {
} }
} }
/**
* Disconnects from the peripheral with the given address.
*/
private fun disconnect(address: String) { private fun disconnect(address: String) {
centralManager.getPeripheralById(address)?.let { peripheral -> centralManager.getPeripheralById(address)?.let { peripheral ->
lifecycleScope.launch { lifecycleScope.launch {
@@ -212,13 +227,17 @@ internal class ProfileService : NotificationService() {
} }
} }
// Logger and other helper functions remain largely the same. /**
* Initializes the logger if not already initialized.
*/
private fun initLogger(deviceAddress: String) { private fun initLogger(deviceAddress: String) {
if (logger != null) return if (logger != null) return
logger = nRFLoggerTree(this, getString(R.string.app_name), deviceAddress) logger = nRFLoggerTree(this, getString(R.string.app_name), deviceAddress)
.also { Timber.plant(it) } .also { Timber.plant(it) }
} }
/**
* Uproots and clears the logger.
*/
private fun uprootLogger() { private fun uprootLogger() {
logger?.let { Timber.uproot(it) } logger?.let { Timber.uproot(it) }
logger = null logger = null