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