From 61c137b22b31320add52bfdb32ddba090fa5c082 Mon Sep 17 00:00:00 2001 From: Andrei Vereha Date: Thu, 14 Oct 2021 21:28:31 +0200 Subject: [PATCH] don't crash when bluetooth is not available --- .../dash/driver/comm/OmnipodDashBleManagerImpl.kt | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/OmnipodDashBleManagerImpl.kt b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/OmnipodDashBleManagerImpl.kt index a70ff39266..fdf7fa7bfb 100644 --- a/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/OmnipodDashBleManagerImpl.kt +++ b/omnipod-dash/src/main/java/info/nightscout/androidaps/plugins/pump/omnipod/dash/driver/comm/OmnipodDashBleManagerImpl.kt @@ -30,9 +30,8 @@ class OmnipodDashBleManagerImpl @Inject constructor( ) : OmnipodDashBleManager { private val busy = AtomicBoolean(false) - private val bluetoothManager: BluetoothManager = - context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager - private val bluetoothAdapter: BluetoothAdapter = bluetoothManager.adapter + private val bluetoothAdapter: BluetoothAdapter? + get() = (context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager?)?.adapter private var connection: Connection? = null private val ids = Ids(podState) @@ -123,7 +122,8 @@ class OmnipodDashBleManagerImpl @Inject constructor( val podAddress = podState.bluetoothAddress ?: throw FailedToConnectException("Missing bluetoothAddress, activate the pod first") - val podDevice = bluetoothAdapter.getRemoteDevice(podAddress) + val podDevice = bluetoothAdapter?.getRemoteDevice(podAddress) + ?: throw ConnectException("Bluetooth not available") val conn = connection ?: Connection(podDevice, aapsLogger, context, podState) connection = conn @@ -193,7 +193,9 @@ class OmnipodDashBleManagerImpl @Inject constructor( aapsLogger.info(LTag.PUMPBTCOMM, "Starting new pod activation") emitter.onNext(PodEvent.Scanning) - val podScanner = PodScanner(aapsLogger, bluetoothAdapter) + val adapter = bluetoothAdapter + ?: throw ConnectException("Bluetooth not available") + val podScanner = PodScanner(aapsLogger, adapter) val podAddress = podScanner.scanForPod( PodScanner.SCAN_FOR_SERVICE_UUID, PodScanner.POD_ID_NOT_ACTIVATED @@ -201,7 +203,7 @@ class OmnipodDashBleManagerImpl @Inject constructor( podState.bluetoothAddress = podAddress emitter.onNext(PodEvent.BluetoothConnecting) - val podDevice = bluetoothAdapter.getRemoteDevice(podAddress) + val podDevice = adapter.getRemoteDevice(podAddress) val conn = Connection(podDevice, aapsLogger, context, podState) connection = conn conn.connect(ConnectionWaitCondition(timeoutMs = 3 * Connection.BASE_CONNECT_TIMEOUT_MS))