Stop waitForConnection when disconnected during connecting
This commit is contained in:
parent
640d1d1626
commit
ccd4d0d550
1 changed files with 12 additions and 1 deletions
|
@ -56,6 +56,8 @@ class Connection(
|
||||||
|
|
||||||
private val bluetoothManager: BluetoothManager? = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager?
|
private val bluetoothManager: BluetoothManager? = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager?
|
||||||
|
|
||||||
|
private var _connectionWaitCond: ConnectionWaitCondition? = null
|
||||||
|
|
||||||
@Volatile
|
@Volatile
|
||||||
var session: Session? = null
|
var session: Session? = null
|
||||||
|
|
||||||
|
@ -65,6 +67,7 @@ class Connection(
|
||||||
@Synchronized
|
@Synchronized
|
||||||
fun connect(connectionWaitCond: ConnectionWaitCondition) {
|
fun connect(connectionWaitCond: ConnectionWaitCondition) {
|
||||||
aapsLogger.debug("Connecting connectionWaitCond=$connectionWaitCond")
|
aapsLogger.debug("Connecting connectionWaitCond=$connectionWaitCond")
|
||||||
|
_connectionWaitCond = connectionWaitCond
|
||||||
podState.connectionAttempts++
|
podState.connectionAttempts++
|
||||||
podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.CONNECTING
|
podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.CONNECTING
|
||||||
val autoConnect = false
|
val autoConnect = false
|
||||||
|
@ -80,6 +83,7 @@ class Connection(
|
||||||
val before = SystemClock.elapsedRealtime()
|
val before = SystemClock.elapsedRealtime()
|
||||||
if (waitForConnection(connectionWaitCond) !is Connected) {
|
if (waitForConnection(connectionWaitCond) !is Connected) {
|
||||||
podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.DISCONNECTED
|
podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.DISCONNECTED
|
||||||
|
_connectionWaitCond = null
|
||||||
throw FailedToConnectException(podDevice.address)
|
throw FailedToConnectException(podDevice.address)
|
||||||
}
|
}
|
||||||
val waitedMs = SystemClock.elapsedRealtime() - before
|
val waitedMs = SystemClock.elapsedRealtime() - before
|
||||||
|
@ -92,6 +96,7 @@ class Connection(
|
||||||
connectionWaitCond.timeoutMs = newTimeout
|
connectionWaitCond.timeoutMs = newTimeout
|
||||||
}
|
}
|
||||||
podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.CONNECTED
|
podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.CONNECTED
|
||||||
|
_connectionWaitCond = null
|
||||||
|
|
||||||
val discoverer = ServiceDiscoverer(aapsLogger, gatt, bleCommCallbacks, this)
|
val discoverer = ServiceDiscoverer(aapsLogger, gatt, bleCommCallbacks, this)
|
||||||
val discovered = discoverer.discoverServices(connectionWaitCond)
|
val discovered = discoverer.discoverServices(connectionWaitCond)
|
||||||
|
@ -200,7 +205,13 @@ class Connection(
|
||||||
// This will be called from a different thread !!!
|
// This will be called from a different thread !!!
|
||||||
override fun onConnectionLost(status: Int) {
|
override fun onConnectionLost(status: Int) {
|
||||||
aapsLogger.info(LTag.PUMPBTCOMM, "Lost connection with status: $status")
|
aapsLogger.info(LTag.PUMPBTCOMM, "Lost connection with status: $status")
|
||||||
// This is called from onConnectionStateChange(), meaning BLE is already disconnected, so need to close gatt
|
// Check if waiting for connection, if so, stop waiting
|
||||||
|
_connectionWaitCond?.stopConnection?.let {
|
||||||
|
if (it.count > 0) {
|
||||||
|
_connectionWaitCond?.stopConnection?.countDown()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// BLE disconnected, so need to close gatt
|
||||||
disconnect(true)
|
disconnect(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue