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 {
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))