Merge pull request #1838 from avereha/avereha/fix-disconnect

fix disconnect
This commit is contained in:
Milos Kozak 2022-06-19 21:53:42 +02:00 committed by GitHub
commit b09dd0ed1d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 4 deletions

View file

@ -27,10 +27,14 @@ class ServiceDiscoverer(
fun discoverServices(connectionWaitCond: ConnectionWaitCondition): Map<CharacteristicType, BluetoothGattCharacteristic> { fun discoverServices(connectionWaitCond: ConnectionWaitCondition): Map<CharacteristicType, BluetoothGattCharacteristic> {
logger.debug(LTag.PUMPBTCOMM, "Discovering services") logger.debug(LTag.PUMPBTCOMM, "Discovering services")
bleCallbacks.startServiceDiscovery() bleCallbacks.startServiceDiscovery()
try {
val discover = gatt.discoverServices() val discover = gatt.discoverServices()
if (!discover) { if (!discover) {
throw ConnectException("Could not start discovering services`") throw ConnectException("Could not start discovering services`")
} }
} catch (ex: SecurityException) {
throw ConnectException("Missing bluetooth permission")
}
connectionWaitCond.timeoutMs?.let { connectionWaitCond.timeoutMs?.let {
bleCallbacks.waitForServiceDiscovery(it) bleCallbacks.waitForServiceDiscovery(it)
} }

View file

@ -40,7 +40,7 @@ class BleCommCallbacks(
if (newState == BluetoothProfile.STATE_CONNECTED && status == BluetoothGatt.GATT_SUCCESS) { if (newState == BluetoothProfile.STATE_CONNECTED && status == BluetoothGatt.GATT_SUCCESS) {
connected.countDown() connected.countDown()
} }
if (newState == BluetoothProfile.STATE_DISCONNECTED && status != BluetoothGatt.GATT_SUCCESS) { if (newState == BluetoothProfile.STATE_DISCONNECTED) {
// If status == SUCCESS, it means that we initiated the disconnect. // If status == SUCCESS, it means that we initiated the disconnect.
disconnectHandler.onConnectionLost(status) disconnectHandler.onConnectionLost(status)
} }

View file

@ -62,6 +62,7 @@ class Connection(
@Volatile @Volatile
var msgIO: MessageIO? = null var msgIO: MessageIO? = null
@Synchronized
fun connect(connectionWaitCond: ConnectionWaitCondition) { fun connect(connectionWaitCond: ConnectionWaitCondition) {
aapsLogger.debug("Connecting connectionWaitCond=$connectionWaitCond") aapsLogger.debug("Connecting connectionWaitCond=$connectionWaitCond")
podState.connectionAttempts++ podState.connectionAttempts++
@ -118,6 +119,7 @@ class Connection(
dataBleIO.readyToRead() dataBleIO.readyToRead()
} }
@Synchronized
fun disconnect(closeGatt: Boolean) { fun disconnect(closeGatt: Boolean) {
aapsLogger.debug(LTag.PUMPBTCOMM, "Disconnecting closeGatt=$closeGatt") aapsLogger.debug(LTag.PUMPBTCOMM, "Disconnecting closeGatt=$closeGatt")
podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.DISCONNECTED podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.DISCONNECTED