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.
This commit is contained in:
Alwen Tiu 2020-12-18 15:22:33 +11:00
parent 2ef00979e5
commit 222fa4f295
2 changed files with 29 additions and 7 deletions

View file

@ -81,10 +81,20 @@ class ConcreteBLETransmitter(
override fun bleTimer(now: Long) { override fun bleTimer(now: Long) {
if (!isSupported || bluetoothStateManager.state() == BluetoothState.poweredOff) { if (!isSupported || bluetoothStateManager.state() == BluetoothState.poweredOff) {
if (advertLoopState != AdvertLoopState.stopped) { 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 advertiseCallback = null
bluetoothGattServer = null bluetoothGattServer = null
state(now, AdvertLoopState.stopped) 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 return
} }

View file

@ -11,6 +11,7 @@ import android.content.IntentFilter
import android.location.LocationManager import android.location.LocationManager
import android.os.Build import android.os.Build
import android.os.PowerManager import android.os.PowerManager
import android.util.Log
import androidx.annotation.Keep import androidx.annotation.Keep
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.LifecycleService import androidx.lifecycle.LifecycleService
@ -93,12 +94,16 @@ class BluetoothMonitoringService : LifecycleService(), CoroutineScope, SensorDel
} }
fun teardown() { fun teardown() {
commandHandler.removeCallbacksAndMessages(null) //commandHandler.removeCallbacksAndMessages(null)
Utils.cancelBMUpdateCheck(this.applicationContext) //Utils.cancelBMUpdateCheck(this.applicationContext)
Utils.cancelNextScan(this.applicationContext) //Utils.cancelNextScan(this.applicationContext)
Utils.cancelNextAdvertise(this.applicationContext) //Utils.cancelNextAdvertise(this.applicationContext)
sensor?.stop()
//[AT]
Log.i(TAG, "[AT] teardown, do nothing")
//sensor?.stop()
} }
private fun setupNotifications() { private fun setupNotifications() {
@ -263,8 +268,15 @@ class BluetoothMonitoringService : LifecycleService(), CoroutineScope, SensorDel
} }
fun sensorStart() { 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) streetPassRecordStorage = StreetPassRecordStorage(applicationContext)
Log.i(TAG, "[AT] creating a new SensorArray")
sensor = SensorArray(applicationContext, this) sensor = SensorArray(applicationContext, this)
getAppDelegate().sensor()?.add(this) getAppDelegate().sensor()?.add(this)
// Sensor will start and stop with Bluetooth power on / off events // Sensor will start and stop with Bluetooth power on / off events