Skip to content

Commit c5a9030

Browse files
authored
Merge pull request #245 from NordicSemiconductor/bugfix/scope
Bugfix: Internal scope created immediately
2 parents 4882eeb + 707bffb commit c5a9030

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

client-core/src/main/java/no/nordicsemi/kotlin/ble/client/internal/CentralManagerImpl.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ import kotlinx.coroutines.flow.launchIn
4040
import kotlinx.coroutines.flow.onCompletion
4141
import kotlinx.coroutines.flow.onEach
4242
import kotlinx.coroutines.launch
43+
import kotlinx.coroutines.newCoroutineContext
4344
import kotlinx.coroutines.withContext
4445
import no.nordicsemi.kotlin.ble.client.CentralManager
4546
import no.nordicsemi.kotlin.ble.client.Peripheral
4647
import no.nordicsemi.kotlin.ble.client.ScanResult
4748
import no.nordicsemi.kotlin.ble.core.Manager
4849
import 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

Comments
 (0)