How to determine a wifi channel number used by wifi ap/network?

You can express the conversion using a couple of formulas, one for each band. The function returns the channel of the given frequency or -1 in case that the frequency is not a valid wifi frequency (2.4GHz and 5GHz).

public static int convertFrequencyToChannel(int freq) {
    if (freq >= 2412 && freq <= 2484) {
        return (freq - 2412) / 5 + 1;
    } else if (freq >= 5170 && freq <= 5825) {
        return (freq - 5170) / 5 + 34;
    } else {
        return -1;
    }
}

It is a compact way to do the same.


According to Radio-Electronics.com, channel number is truly related with frequency.

CHA LOWER   CENTER  UPPER
NUM FREQ    FREQ    FREQ
    MHZ     MHZ     MHZ
  1 2401    2412    2423
  2 2406    2417    2428
  3 2411    2422    2433
  4 2416    2427    2438
  5 2421    2432    2443
  6 2426    2437    2448
  7 2431    2442    2453
  8 2436    2447    2458
  9 2441    2452    2463
 10 2446    2457    2468
 11 2451    2462    2473
 12 2456    2467    2478
 13 2461    2472    2483
 14 2473    2484    2495

For Android, ScanResult contains the frequency of the channel.


@SuppressWarnings("boxing")
private final static ArrayList<Integer> channelsFrequency = new ArrayList<Integer>(
        Arrays.asList(0, 2412, 2417, 2422, 2427, 2432, 2437, 2442, 2447,
                2452, 2457, 2462, 2467, 2472, 2484));

public static Integer getFrequencyFromChannel(int channel) {
    return channelsFrequency.get(channel);
}

public static int getChannelFromFrequency(int frequency) {
    return channelsFrequency.indexOf(Integer.valueOf(frequency));
}

According to standard [802.11-2012], there is a simpler way to work out channel number from frequency. Specifically,

channel_center_frequency = channel_starting_frequency + 5 * channel_number

For 5G band, channel_number = 0, 1, ..., 200; channel_starting_frequency = 5000 MHz.

For 2.4G band, channel_number = 1, 2, ..., 13; channel_starting_frequency = 2047 MHz.

The list of all channel frequencies can be found at WiFi channels


Translating this into code - refer to iw source:

int ieee80211_frequency_to_channel(int freq)
{
    if (freq == 2484)
        return 14;

    if (freq < 2484)
        return (freq - 2407) / 5;

    return freq/5 - 1000;
}

Tags:

C

Android

Wifi