From 054d2330a460b16b2c395ebcc6b4ca8f992c1b67 Mon Sep 17 00:00:00 2001
From: andrekir <andrekir@pm.me>
Date: Fri, 3 Mar 2023 11:03:43 -0300
Subject: [PATCH] fix: add @Singleton annotation to RadioInterfaceService

- ensure only one RadioInterfaceService instance is created and used throughout the app
- fix multiple startInterface() calls when Bluetooth is enabled
---
 .../mesh/repository/radio/RadioInterfaceService.kt          | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/app/src/main/java/com/geeksville/mesh/repository/radio/RadioInterfaceService.kt b/app/src/main/java/com/geeksville/mesh/repository/radio/RadioInterfaceService.kt
index 7a81d528..8937c20d 100644
--- a/app/src/main/java/com/geeksville/mesh/repository/radio/RadioInterfaceService.kt
+++ b/app/src/main/java/com/geeksville/mesh/repository/radio/RadioInterfaceService.kt
@@ -22,6 +22,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
 import kotlinx.coroutines.flow.SharedFlow
 import kotlinx.coroutines.flow.asStateFlow
 import javax.inject.Inject
+import javax.inject.Singleton
 
 
 /**
@@ -33,6 +34,7 @@ import javax.inject.Inject
  * Note - this class intentionally dumb.  It doesn't understand protobuf framing etc...
  * It is designed to be simple so it can be stubbed out with a simulated version as needed.
  */
+@Singleton
 class RadioInterfaceService @Inject constructor(
     private val context: Application,
     private val dispatchers: CoroutineDispatchers,
@@ -72,8 +74,8 @@ class RadioInterfaceService @Inject constructor(
     init {
         processLifecycle.coroutineScope.launch {
             bluetoothRepository.state.collect { state ->
-                if (state.enabled) {
-                    // startInterface() FIXME no longer safe to call here, crashing SafeBluetooth.asyncConnect
+                if (state.enabled && !isStarted) {
+                    startInterface()
                 } else if (radioIf is BluetoothInterface) {
                     stopInterface()
                 }