Merge pull request #730

don't crash when bluetooth is not available
This commit is contained in:
Milos Kozak 2021-10-14 21:41:09 +02:00 committed by GitHub
commit 009b3467a5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -30,9 +30,8 @@ class OmnipodDashBleManagerImpl @Inject constructor(
) : OmnipodDashBleManager { ) : OmnipodDashBleManager {
private val busy = AtomicBoolean(false) private val busy = AtomicBoolean(false)
private val bluetoothManager: BluetoothManager = private val bluetoothAdapter: BluetoothAdapter?
context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager get() = (context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager?)?.adapter
private val bluetoothAdapter: BluetoothAdapter = bluetoothManager.adapter
private var connection: Connection? = null private var connection: Connection? = null
private val ids = Ids(podState) private val ids = Ids(podState)
@ -123,7 +122,8 @@ class OmnipodDashBleManagerImpl @Inject constructor(
val podAddress = val podAddress =
podState.bluetoothAddress podState.bluetoothAddress
?: throw FailedToConnectException("Missing bluetoothAddress, activate the pod first") ?: 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 val conn = connection
?: Connection(podDevice, aapsLogger, context, podState) ?: Connection(podDevice, aapsLogger, context, podState)
connection = conn connection = conn
@ -193,7 +193,9 @@ class OmnipodDashBleManagerImpl @Inject constructor(
aapsLogger.info(LTag.PUMPBTCOMM, "Starting new pod activation") aapsLogger.info(LTag.PUMPBTCOMM, "Starting new pod activation")
emitter.onNext(PodEvent.Scanning) 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( val podAddress = podScanner.scanForPod(
PodScanner.SCAN_FOR_SERVICE_UUID, PodScanner.SCAN_FOR_SERVICE_UUID,
PodScanner.POD_ID_NOT_ACTIVATED PodScanner.POD_ID_NOT_ACTIVATED
@ -201,7 +203,7 @@ class OmnipodDashBleManagerImpl @Inject constructor(
podState.bluetoothAddress = podAddress podState.bluetoothAddress = podAddress
emitter.onNext(PodEvent.BluetoothConnecting) emitter.onNext(PodEvent.BluetoothConnecting)
val podDevice = bluetoothAdapter.getRemoteDevice(podAddress) val podDevice = adapter.getRemoteDevice(podAddress)
val conn = Connection(podDevice, aapsLogger, context, podState) val conn = Connection(podDevice, aapsLogger, context, podState)
connection = conn connection = conn
conn.connect(ConnectionWaitCondition(timeoutMs = 3 * Connection.BASE_CONNECT_TIMEOUT_MS)) conn.connect(ConnectionWaitCondition(timeoutMs = 3 * Connection.BASE_CONNECT_TIMEOUT_MS))