@@ -40,12 +40,14 @@ import kotlinx.coroutines.flow.launchIn
4040import kotlinx.coroutines.flow.onCompletion
4141import kotlinx.coroutines.flow.onEach
4242import kotlinx.coroutines.launch
43+ import kotlinx.coroutines.newCoroutineContext
4344import kotlinx.coroutines.withContext
4445import no.nordicsemi.kotlin.ble.client.CentralManager
4546import no.nordicsemi.kotlin.ble.client.Peripheral
4647import no.nordicsemi.kotlin.ble.client.ScanResult
4748import no.nordicsemi.kotlin.ble.core.Manager
4849import no.nordicsemi.kotlin.ble.core.exception.ManagerClosedException
50+ import kotlin.coroutines.EmptyCoroutineContext
4951
5052/* *
5153 * Base implementation of [CentralManager].
@@ -67,13 +69,12 @@ abstract class CentralManagerImpl<
6769 protected val scope: CoroutineScope ,
6870): CentralManager <ID , P , EX , F , SR > {
6971 private var closeJob: Job
70- private lateinit var internalScope: CoroutineScope
72+ private var internalScope = CoroutineScope (scope.newCoroutineContext( EmptyCoroutineContext ))
7173
7274 init {
7375 // Make sure the Central Manager gets closed when its scope gets cancelled.
7476 // This coroutine gets cancelled when close() is called or when the scope gets cancelled.
75- closeJob = scope.launch {
76- internalScope = this
77+ closeJob = internalScope.launch {
7778 try { awaitCancellation() }
7879 finally { withContext(NonCancellable ) { close() } }
7980 }
0 commit comments