Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monitor bluetooth state before initiating connection #580

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions core/src/androidMain/kotlin/BluetoothDeviceAndroidPeripheral.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,20 @@ import com.juul.kable.logs.Logging.DataProcessor.Operation
import com.juul.kable.logs.detail
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart.UNDISPATCHED
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.job
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.cancellation.CancellationException
Expand Down Expand Up @@ -106,6 +110,7 @@ internal class BluetoothDeviceAndroidPeripheral(

private suspend fun establishConnection(scope: CoroutineScope) {
checkBluetoothAdapterState(expected = STATE_ON)
bluetoothState.watchForDisablingIn(scope)

logger.info { message = "Connecting" }
_state.value = State.Connecting.Bluetooth
Expand Down Expand Up @@ -141,21 +146,21 @@ internal class BluetoothDeviceAndroidPeripheral(
logger.info { message = "Connected" }
_state.value = State.Connected

bluetoothState.watchForDisablingIn(scope)
state.watchForConnectionLossIn(scope)
}

private fun Flow<Int>.watchForDisablingIn(scope: CoroutineScope) =
filter { state -> state == STATE_TURNING_OFF || state == STATE_OFF }
.onEach { state ->
logger.debug {
message = "Bluetooth disabled"
detail("state", state)
private fun Flow<Int>.watchForDisablingIn(scope: CoroutineScope): Job =
scope.launch(start = UNDISPATCHED) {
filter { state -> state == STATE_TURNING_OFF || state == STATE_OFF }
.collect { state ->
logger.debug {
message = "Bluetooth disabled"
detail("state", state)
}
closeConnection()
throw BluetoothDisabledException()
}
closeConnection()
throw BluetoothDisabledException()
}
.launchIn(scope)
}

private fun Flow<State>.watchForConnectionLossIn(scope: CoroutineScope) =
state
Expand Down
21 changes: 11 additions & 10 deletions core/src/appleMain/kotlin/CBPeripheralCoreBluetoothPeripheral.kt
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ internal class CBPeripheralCoreBluetoothPeripheral(
private suspend fun establishConnection(scope: CoroutineScope) {
// Check CBCentral State since connecting can result in an API misuse message.
centralManager.checkBluetoothState(CBManagerStatePoweredOn)
centralManager.delegate.state.watchForDisablingIn(scope)

logger.info { message = "Connecting" }
_state.value = State.Connecting.Bluetooth
Expand Down Expand Up @@ -169,21 +170,21 @@ internal class CBPeripheralCoreBluetoothPeripheral(
logger.info { message = "Connected" }
_state.value = State.Connected

centralManager.delegate.state.watchForDisablingIn(scope)
centralManager.delegate.onDisconnected.watchForConnectionLossIn(scope)
}

private fun Flow<CBManagerState>.watchForDisablingIn(scope: CoroutineScope) =
filter { state -> state != CBManagerStatePoweredOn }
.onEach { state ->
logger.info {
message = "Bluetooth unavailable"
detail("state", state)
scope.launch(start = UNDISPATCHED) {
filter { state -> state != CBManagerStatePoweredOn }
.collect { state ->
logger.info {
message = "Bluetooth unavailable"
detail("state", state)
}
closeConnection()
throw ConnectionLostException("$this $state")
}
closeConnection()
throw ConnectionLostException("$this $state")
}
.launchIn(scope)
}

private fun Flow<NSUUID>.watchForConnectionLossIn(scope: CoroutineScope) =
filter { identifier -> identifier == cbPeripheral.identifier }
Expand Down