Liberty Tom
11/04/2024, 4:45 AMTimeoutCancellationException
until blockConnect
running end.
private suspend fun realConnectBluetoothDevice(): Boolean {
if (bluetoothCommConnection.isConnected) {
return true
}
try {
return withTimeout(5000L) {
for (i in 1..5) {
L.e("the $i times to connect bluetooth device")
if (blockConnect()) {
return@withTimeout true
}
if (i < 5) {
continue
}
}
throw CommConnectionException("connect bluetooth device failed")
}
} catch (e: TimeoutCancellationException) {
bluetoothCommConnection.closeImmediately()
throw e
}
}
private suspend fun blockConnect() = withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
try {
bluetoothCommConnection.open()
bluetoothCommConnection.isConnected
} catch (e: CommConnectionException) {
false
}
}
Youssef Shoaib [MOD]
11/04/2024, 4:49 AM