ThanosFisherman / WifiUtils

Easily Connect to WiFi Networks
Apache License 2.0
731 stars 188 forks source link

Android 9 throttling #118

Open fernando-s97 opened 2 years ago

fernando-s97 commented 2 years ago

image

In Android 9, when I execute the code below 5 times , in the 5th attempt, I won't be able to connect. That's because every time I make a connection, a scan is triggered. Why's that? Is it really necessary?

WifiUtils.withContext(requireContext())
            .connectWith("ssid", "pass")
            .setTimeout(5000)
            .onConnectionResult(object : ConnectionSuccessListener {
                override fun success() {
                    isConnected = true
                    Toast.makeText(activity, "Connect success!", Toast.LENGTH_SHORT)
                        .show()
                }

                override fun failed(errorCode: ConnectionErrorCode) {
                    Toast.makeText(
                        activity,
                        "Failed to connect: $errorCode",
                        Toast.LENGTH_SHORT
                    ).show()
                }
            })
            .start()
ThanosFisherman commented 2 years ago

Hello,

At the moment WiFIUtils indeed performs a scan before connecting to a wifi and there is currently no way to override this behavior.

This was a design choice back in the day when android wouldn't impose throttling on wifi scanning.

It is a good idea to deprecate that behavior now. Hopefully I will get around to it as soon as I find some time. Pull requests are also welcome.

fernando-s97 commented 2 years ago

Got it! I'll see if I can work on that and make a PR

ansuman87 commented 2 years ago

Is it not possible to use the old scan results instead of scanning all the time? I mean if the scan fails then provide the old scan results. This is what the official document says, how to perform a network scan.

vanenshi commented 2 years ago

Got it! I'll see if I can work on that and make a PR

any progress yet?

fernando-s97 commented 2 years ago

@vanenshi I ended up not being to take a look at it, and I think I won't be to do it in the next 1 or 2 months