Added extra checks and logging
This commit is contained in:
parent
aadf40b6fb
commit
4a1eded8e5
2 changed files with 20 additions and 8 deletions
|
@ -125,10 +125,6 @@ class BLEComm @Inject internal constructor(
|
|||
.build()
|
||||
val filters = mutableListOf<ScanFilter>()
|
||||
|
||||
|
||||
isConnected = false
|
||||
isConnecting = true
|
||||
|
||||
// Find our Medtrum Device!
|
||||
filters.add(
|
||||
ScanFilter.Builder().setDeviceName("MT").build()
|
||||
|
@ -158,16 +154,16 @@ class BLEComm @Inject internal constructor(
|
|||
return false
|
||||
}
|
||||
|
||||
isConnected = false
|
||||
isConnecting = true
|
||||
if (mDevice != null && mDeviceSN == deviceSN) {
|
||||
// Skip scanning and directly connect to gatt
|
||||
aapsLogger.debug(LTag.PUMPBTCOMM, "Skipping scan and directly connecting to gatt")
|
||||
isConnecting = true
|
||||
connectGatt(mDevice!!)
|
||||
} else {
|
||||
// Scan for device
|
||||
aapsLogger.debug(LTag.PUMPBTCOMM, "Scanning for device")
|
||||
mDeviceSN = deviceSN
|
||||
isConnecting = true
|
||||
startScan()
|
||||
}
|
||||
|
||||
|
@ -180,8 +176,14 @@ class BLEComm @Inject internal constructor(
|
|||
private fun connectGatt(device: BluetoothDevice) {
|
||||
// Reset sequence counter
|
||||
mWriteSequenceNumber = 0
|
||||
mBluetoothGatt =
|
||||
if (mBluetoothGatt == null) {
|
||||
mBluetoothGatt =
|
||||
device.connectGatt(context, false, mGattCallback, BluetoothDevice.TRANSPORT_LE)
|
||||
} else {
|
||||
// Already connected?, this should not happen force disconnect
|
||||
aapsLogger.error(LTag.PUMPBTCOMM, "connectGatt, mBluetoothGatt is not null")
|
||||
disconnect("connectGatt, mBluetoothGatt is not null")
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
|
@ -200,8 +202,10 @@ class BLEComm @Inject internal constructor(
|
|||
SystemClock.sleep(100)
|
||||
}
|
||||
if (isConnected) {
|
||||
aapsLogger.debug(LTag.PUMPBTCOMM, "Connected, disconnecting")
|
||||
mBluetoothGatt?.disconnect()
|
||||
} else {
|
||||
aapsLogger.debug(LTag.PUMPBTCOMM, "Not connected, closing gatt")
|
||||
close()
|
||||
isConnected = false
|
||||
mCallback?.onBLEDisconnected()
|
||||
|
@ -212,6 +216,7 @@ class BLEComm @Inject internal constructor(
|
|||
@Synchronized fun close() {
|
||||
aapsLogger.debug(LTag.PUMPBTCOMM, "BluetoothAdapter close")
|
||||
mBluetoothGatt?.close()
|
||||
SystemClock.sleep(100)
|
||||
mBluetoothGatt = null
|
||||
}
|
||||
|
||||
|
|
|
@ -72,6 +72,11 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
|||
@Inject lateinit var pumpSync: PumpSync
|
||||
@Inject lateinit var dateUtil: DateUtil
|
||||
|
||||
companion object {
|
||||
// TODO: Test and further increase?
|
||||
private const val COMMAND_TIMEOUT_SEC: Long = 60
|
||||
}
|
||||
|
||||
val timeUtil = MedtrumTimeUtil()
|
||||
|
||||
private val disposable = CompositeDisposable()
|
||||
|
@ -272,10 +277,12 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
|||
|
||||
/** BLECommCallbacks */
|
||||
override fun onBLEConnected() {
|
||||
aapsLogger.debug(LTag.PUMPCOMM, "<<<<< onBLEConnected")
|
||||
currentState.onConnected()
|
||||
}
|
||||
|
||||
override fun onBLEDisconnected() {
|
||||
aapsLogger.debug(LTag.PUMPCOMM, "<<<<< onBLEDisconnected")
|
||||
currentState.onDisconnected()
|
||||
}
|
||||
|
||||
|
@ -358,7 +365,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
|||
|
||||
fun waitForResponse(): Boolean {
|
||||
val startTime = System.currentTimeMillis()
|
||||
val timeoutMillis = T.secs(45).msecs()
|
||||
val timeoutMillis = T.secs(COMMAND_TIMEOUT_SEC).msecs()
|
||||
while (!responseHandled) {
|
||||
if (System.currentTimeMillis() - startTime > timeoutMillis) {
|
||||
// If we haven't received a response in the specified time, assume the command failed
|
||||
|
|
Loading…
Reference in a new issue