From 84ada65857617610a7be54464526c64ea5639276 Mon Sep 17 00:00:00 2001 From: hiar Date: Thu, 9 Oct 2025 13:50:28 +0200 Subject: [PATCH] Add try catch inside the launch --- .../channelSounding/ChannelSoundingManager.kt | 40 +++++++++++-------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/profile/src/main/java/no/nordicsemi/android/toolbox/profile/repository/channelSounding/ChannelSoundingManager.kt b/profile/src/main/java/no/nordicsemi/android/toolbox/profile/repository/channelSounding/ChannelSoundingManager.kt index 23b24012..906452ff 100644 --- a/profile/src/main/java/no/nordicsemi/android/toolbox/profile/repository/channelSounding/ChannelSoundingManager.kt +++ b/profile/src/main/java/no/nordicsemi/android/toolbox/profile/repository/channelSounding/ChannelSoundingManager.kt @@ -170,6 +170,13 @@ class ChannelSoundingManager @Inject constructor( context.mainExecutor, rangingSessionCallback ) + if (rangingSession != null) { + Timber.tag("AAA").d("Ranging session created") + } else { + _rangingData.value = + RangingSessionAction.OnError(SessionClosedReason.UNKNOWN) + return@RangingCapabilitiesCallback + } rangingSession?.let { try { it.addDeviceToRangingSession(rawRangingDeviceConfig) @@ -208,28 +215,27 @@ class ChannelSoundingManager @Inject constructor( @RequiresApi(Build.VERSION_CODES.BAKLAVA) fun closeSession(onClosed: (suspend () -> Unit)? = null) { - try { - rangingSession?.let { session -> - session.stop() - session.close() - rangingSession = null - _rangingData.value = null - // unregister the callback - rangingManager?.unregisterCapabilitiesCallback(rangingCapabilityCallback) - // Invoke the onClosed callback after a short delay to ensure the session is closed - onClosed?.let { + rangingSession?.let { session -> + session.stop() + session.close() + rangingSession = null + _rangingData.value = null + // unregister the callback + rangingManager?.unregisterCapabilitiesCallback(rangingCapabilityCallback) + } + onClosed?.let { + CoroutineScope(Dispatchers.IO).launch { + try { + // Invoke the onClosed callback after a short delay to ensure the session is closed _rangingData.value = RangingSessionAction.OnStart // Wait for a moment to ensure the session is properly closed before invoking the callback // Launch a coroutine to delay and call onClosed - CoroutineScope(Dispatchers.IO).launch { - delay(1000) - it() - } - + delay(2000) + it() + } catch (e: Exception) { + _rangingData.value = RangingSessionAction.OnError(SessionClosedReason.UNKNOWN) } } - } catch (e: Exception) { - _rangingData.value = RangingSessionAction.OnError(SessionClosedReason.UNKNOWN) } }