wifi scanner broadcast receiver only registered once but continuously returning results

19 Views Asked by At

This is my broadcast receiver which is taken from the wifi manager documentation:

private val wifiScanReceiver = object : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent?) {
        val success = intent?.getBooleanExtra(WifiManager.EXTRA_RESULTS_UPDATED, false) ?: false
        Log.d("WifiManager", "In on receive $success with scan number: $scanId")

        if (success) {
            scanSuccess()
        } else {
            scanFailure()
        }
        // unregisterWifiReceiver()
    }
}

I register the wifi receiver in the on create method, and call start scan each time a button is clicked. However, even when i click the button only once, i get continuous scans returned. This is an issue because I am storing these values in a data base and i get duplicates, although sometimes the level value is different.

The only way i have managed to fix this is to unregister the wifi receiver as soon as it has scanned successfuly, and then to create a receiver each time I click the button. I'm not sure if i would need to just store all of these values - why does the level fluctuate and why does it return so many results? I am using this for wifi fingerprinting so the level would be quite important.

0

There are 0 best solutions below