diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/ServiceDiscoverer.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/ServiceDiscoverer.kt index cd35a45bdb..ca371fafac 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/ServiceDiscoverer.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/ServiceDiscoverer.kt @@ -27,9 +27,13 @@ class ServiceDiscoverer( fun discoverServices(connectionWaitCond: ConnectionWaitCondition): Map { logger.debug(LTag.PUMPBTCOMM, "Discovering services") bleCallbacks.startServiceDiscovery() - val discover = gatt.discoverServices() - if (!discover) { - throw ConnectException("Could not start discovering services`") + try { + val discover = gatt.discoverServices() + if (!discover) { + throw ConnectException("Could not start discovering services`") + } + } catch (ex: SecurityException) { + throw ConnectException("Missing bluetooth permission") } connectionWaitCond.timeoutMs?.let { bleCallbacks.waitForServiceDiscovery(it) diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/callbacks/BleCommCallbacks.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/callbacks/BleCommCallbacks.kt index 83ee2346b4..3724341c89 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/callbacks/BleCommCallbacks.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/callbacks/BleCommCallbacks.kt @@ -40,7 +40,7 @@ class BleCommCallbacks( if (newState == BluetoothProfile.STATE_CONNECTED && status == BluetoothGatt.GATT_SUCCESS) { 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. disconnectHandler.onConnectionLost(status) } diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Connection.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Connection.kt index bfb565830c..358c87a4c6 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Connection.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/session/Connection.kt @@ -62,6 +62,7 @@ class Connection( @Volatile var msgIO: MessageIO? = null + @Synchronized fun connect(connectionWaitCond: ConnectionWaitCondition) { aapsLogger.debug("Connecting connectionWaitCond=$connectionWaitCond") podState.connectionAttempts++ @@ -118,6 +119,7 @@ class Connection( dataBleIO.readyToRead() } + @Synchronized fun disconnect(closeGatt: Boolean) { aapsLogger.debug(LTag.PUMPBTCOMM, "Disconnecting closeGatt=$closeGatt") podState.bluetoothConnectionState = OmnipodDashPodStateManager.BluetoothConnectionState.DISCONNECTED