Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leaked scan callback when app be killed by task manager #139

Open
doanpt opened this issue Jun 10, 2023 · 0 comments
Open

Leaked scan callback when app be killed by task manager #139

doanpt opened this issue Jun 10, 2023 · 0 comments

Comments

@doanpt
Copy link

doanpt commented Jun 10, 2023

Hi,
I'm using your lib to build an android app,
In my app, I start scanScan when application lifecycle is onStart and stopScan when application lifecycle is onStop as below.

override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) =
    when (event) {
        Lifecycle.Event.ON_START -> {
            Timber.d("onStateChanged onStart")
            dependencies.bleLockModule.onStart()
        }
        Lifecycle.Event.ON_STOP -> {
            Timber.d("onStateChanged onStop")
            dependencies.bleLockModule.onStop()
        }
        else -> {
            Timber.d("onStateChanged $event")
        }
    }
override fun startScan(scanAggressively: Boolean) {
    if (scanCallback == null) {
        val callback = BleScanCallBack(scope, _resultsFlow)
        scanCallback = callback
        runCatching {
            scanner.startScan(emptyList(), buildScanSettings(scanAggressively), callback)
            restartScanning()
            Timber.d("BLE scan started")
        }.onFailure {
            Timber.e("Start scan failed: $it")
            callback.onScanFailed(BleError.ScanErrorCode.Unknown.value)
        }
    }
}

override fun stopScan() {
    scanCallback?.let {
        scanner.stopScan(it)
        Timber.d("BLE scan stop scanning")
        scanCallback = null
    } ?: Timber.d("Ignore stop scan due to callback null")
}

I found it working fine, but when app killed by task manager(recents tab), I see that I leaked the scan callback due to onStop not being called. after I tried to close/-reopen my app more that 32 times, android os throw error: "Unable to register GATT client, MAX client reached: 32"
so cloud you please share me the best practice to call stopScan when app is being killed by task manager

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant