File tree Expand file tree Collapse file tree
core/src/androidMain/kotlin Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -46,6 +46,7 @@ import kotlinx.coroutines.flow.update
4646import kotlinx.coroutines.job
4747import kotlinx.coroutines.withContext
4848import kotlin.coroutines.CoroutineContext
49+ import kotlin.coroutines.cancellation.CancellationException
4950
5051private val clientCharacteristicConfigUuid = uuidFrom(CLIENT_CHARACTERISTIC_CONFIG_UUID )
5152
@@ -178,7 +179,13 @@ internal class BluetoothDeviceAndroidPeripheral(
178179 }
179180
180181 override suspend fun disconnect () {
181- _connection ?.bluetoothGatt?.disconnect()
182+ if (state.value is State .Connected ) {
183+ // Disconnect from active connection.
184+ _connection ?.bluetoothGatt?.disconnect()
185+ } else {
186+ // Cancel in-flight connection attempt.
187+ connectAction.cancelAndJoin(CancellationException (NotConnectedException ()))
188+ }
182189 suspendUntil<Disconnected >()
183190 logger.info { message = " Disconnected" }
184191 }
You can’t perform that action at this time.
0 commit comments