preface

Most smart home devices can only connect to 2.4g wireless networks due to stability, cost, and other reasons. Therefore, we need to judge whether the Wi-Fi is 2.4g, 5G or 2.4G+5G dual-band when using APP to realize the distribution network. In other words, most smart home devices cannot connect to pure 5G Wi-Fi. If the user’s mobile phone is connected to pure 5G Wi-Fi, a prompt message needs to be given to the user. 2.4g +5G dual-band does not need prompting.

Many people must think, “What’s so hard about this?” There must be attributes in the scanned Wi-Fi results. It’s not as simple as that. One of our testers asked this question on the zen path, and two of our Android and IOS team members who are responsible for this feature left the bug hanging on the zen path for a few months, saying there’s really no way to tell directly.

I was not so busy today and I thought of the problem, and I thought I would see if I could work it out. After searching the data, it is indeed possible to determine whether it is 2.4g or 5G by scanning frequency, but it is not possible to determine whether it is 2.4g +5G dual frequency. I don’t believe it. I just googled it and found there was no solution!

After an afternoon of analysis after finally solved! In fact, it was not difficult to find this problem, but at the beginning, like the other two partners, I was not familiar with the Wi-Fi frequency band, so my thinking did not change. I always wanted to judge whether it was 2.4g +5G dual-band by the information returned by a certain Wi-Fi, which was not feasible. Later, I will analyze why it was not feasible.

A, spectrum

When we go shopping for a router on Taobao, we often find a router with a title that includes dual-band, which in this case means that the router can operate two bands, the 2.4ghz band and the 5GHz band. The specific differences are as follows:

  • 2.4 GHz frequency band

    The network access rate in the 2.4GHz band is 72M. The wi-fi protocol corresponds to 802.11n. It is characterized by low rate, less channel interference, strong ability to pass through walls, and long coverage distance. Frequencies between 2400 and 2500, not including head and tail.

  • 5 GHZ frequency band

    The network access rate in the 5GHz band is 433 mbit/s. The wi-fi protocol corresponds to 802.11ac. It is characterized by high rate, low interference channel, poor ability to pass through walls, and relatively close coverage distance. Frequencies are between 4900 and 5900, not including heads and tails.

If the router has the dual-band function enabled, the router will emit both 2.4ghz and 5GHz networks, but they do display the same SSID (Wi-Fi name) on the phone, so we may connect to 2.4ghz or 5GHz. Which one to connect to depends on the current environment, network conditions and other reasons.

2. Code analysis

See what I’ve highlighted above? Here’s the break! Dual-band routers emit networks in both bands but display the same SSID (Wi-Fi name). If the wi-fi name on our router is “wildma_wifi,” then only one wi-fi name will be displayed on our phone, not two wi-fi names.

And then when we connect, we print it out with the code, which is also only one frequency. As follows:

val wifiManager = applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
val wifiInfo = mWifiManager.connectionInfo
val frequency = wifiInfo.frequency
Copy the code

You get frequency between 2400 and 2500, or between 4900 and 5900. That way, you can only tell if the Wi-Fi is 2.4g or 5G, not 2.4g +5G dual-band.

So if you don’t understand the dual frequency, then your mind is going to stay there and not get the answer. After we understand the dual-band, we know that the dual-band router will emit the network of two bands at the same time, so we can traverse the scanned Wi-Fi. If the Wi-Fi of the same SSID appears twice and the frequency is different, the connected Wi-Fi is 2.4g +5G dual-band. The code is as follows:

class MainActivity : AppCompatActivity() {

    varmWifiScanResults = HashMap<String, WifiScanResult? > ()/** * Processing scan results **@paramScanResults set of scanResults */
    private fun handleScanResult(scanResults: List<ScanResult>) {
        for (i in scanResults.indices) {
            val scanResult = scanResults[i]
            val ssid = scanResult.SSID
            if(! mWifiScanResults.containsKey(ssid)) { mWifiScanResults[ssid] = WifiScanResult() } mWifiScanResults[ssid]? .setFrequency(scanResult.frequency) } } }Copy the code
class WifiScanResult {
    // Whether it is 2.4g
    private var is24G = false

    // Whether it is 5G
    private var is5G = false

    /** * Set frequency **@paramFrequency frequency * /
    fun setFrequency(frequency: Int) {
        if (frequency in 2401.2499.) {
            is24G = true
        }
        if (frequency in 4901.5899.) {
            is5G = true}}}Copy the code

Complete source code:Wifi

Third, summary

A problem may not be that difficult, but if you don’t understand the knowledge, your thinking can’t change and you’ll be stuck at a certain point.

About me

I’m Wildma, a CSDN certified blogger, a good writer for brief programmers, and good at screen adaptation. If the article is helpful to you, a “like” is the biggest recognition to me!