From 222fa4f2952d6d7c1831cdfbaa7336bc5c73b67a Mon Sep 17 00:00:00 2001 From: Alwen Tiu Date: Fri, 18 Dec 2020 15:22:33 +1100 Subject: [PATCH] Testing a proof-of-concept fix for the duplicate GATT service issue. - In ConcreteBLETransmitter.kt, make sure services are cleard and GATT server closed (if it exists) - In BluetoothMonitoringService.kt, make sure no duplicate SensorArray objects are created. DISCLAIMER: This modification is meant to demonstrate a diagnosis of the root cause of the issue; it is not meant as a definitive fix. --- .../sensor/ble/ConcreteBLETransmitter.kt | 12 +++++++++- .../services/BluetoothMonitoringService.kt | 24 ++++++++++++++----- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/au/gov/health/covidsafe/sensor/ble/ConcreteBLETransmitter.kt b/app/src/main/java/au/gov/health/covidsafe/sensor/ble/ConcreteBLETransmitter.kt index 4a331d3..000c7d2 100644 --- a/app/src/main/java/au/gov/health/covidsafe/sensor/ble/ConcreteBLETransmitter.kt +++ b/app/src/main/java/au/gov/health/covidsafe/sensor/ble/ConcreteBLETransmitter.kt @@ -81,10 +81,20 @@ class ConcreteBLETransmitter( override fun bleTimer(now: Long) { if (!isSupported || bluetoothStateManager.state() == BluetoothState.poweredOff) { if (advertLoopState != AdvertLoopState.stopped) { + // [AT] Clear services and close GATT server, if it exists + try { + if (bluetoothGattServer != null) { + logger.debug("[AT] bleTimer, non-null gatt server, stopping GATT server") + bluetoothGattServer!!.clearServices() + bluetoothGattServer!!.close() + } + } catch (e: Throwable) { + logger.fault("[AT] bleTimer, failed to stop GATT server", e) + } advertiseCallback = null bluetoothGattServer = null state(now, AdvertLoopState.stopped) - logger.debug("advertLoopTask, stop advert (advert={}ms)", timeSincelastStateChange(now)) + logger.debug("[AT] advertLoopTask, BluetoothState.powerdOff, stop advert (advert={}ms)", timeSincelastStateChange(now)) } return } diff --git a/app/src/main/java/au/gov/health/covidsafe/services/BluetoothMonitoringService.kt b/app/src/main/java/au/gov/health/covidsafe/services/BluetoothMonitoringService.kt index 01fc13c..728d789 100644 --- a/app/src/main/java/au/gov/health/covidsafe/services/BluetoothMonitoringService.kt +++ b/app/src/main/java/au/gov/health/covidsafe/services/BluetoothMonitoringService.kt @@ -11,6 +11,7 @@ import android.content.IntentFilter import android.location.LocationManager import android.os.Build import android.os.PowerManager +import android.util.Log import androidx.annotation.Keep import androidx.appcompat.app.AppCompatActivity import androidx.lifecycle.LifecycleService @@ -93,12 +94,16 @@ class BluetoothMonitoringService : LifecycleService(), CoroutineScope, SensorDel } fun teardown() { - commandHandler.removeCallbacksAndMessages(null) + //commandHandler.removeCallbacksAndMessages(null) - Utils.cancelBMUpdateCheck(this.applicationContext) - Utils.cancelNextScan(this.applicationContext) - Utils.cancelNextAdvertise(this.applicationContext) - sensor?.stop() + //Utils.cancelBMUpdateCheck(this.applicationContext) + //Utils.cancelNextScan(this.applicationContext) + //Utils.cancelNextAdvertise(this.applicationContext) + + //[AT] + Log.i(TAG, "[AT] teardown, do nothing") + + //sensor?.stop() } private fun setupNotifications() { @@ -263,8 +268,15 @@ class BluetoothMonitoringService : LifecycleService(), CoroutineScope, SensorDel } fun sensorStart() { - if (broadcastMessage != null) { + // [AT] Don't create a new sensor, to avoid creating multiple advertisements/duplicate GATT services + Log.i(TAG, "[AT] sensorStart") + + if (broadcastMessage != null && sensor == null) { + streetPassRecordStorage = StreetPassRecordStorage(applicationContext) + + Log.i(TAG, "[AT] creating a new SensorArray") + sensor = SensorArray(applicationContext, this) getAppDelegate().sensor()?.add(this) // Sensor will start and stop with Bluetooth power on / off events