Ive discovered that several android wifi-apps (WiFi Manager, WiFi Analyzer) shows a channel number of WiFi network additionally to BSSID/SSID etc. But I can't find any info on how they do it. The only thing I know is I can get some wifi frequency. Maybe they determine a channel corresponding to that frequency? Is there a way to detect channel of wifi network in android at all? Of course this info is not a big deal and I can live without it :) but still i'm curious...
According to Radio-Electronics.com, channel number is truly related with frequency.
CHA LOWER CENTER UPPER
NUM FREQ FREQ FREQ
MHZ MHZ MHZ
1 2 401 2 412 2 423
2 2 404 2 417 2 428
3 2 411 2 422 2 433
4 2 416 2 427 2 438
5 2 421 2 432 2 443
6 2 426 2 437 2 448
7 2 431 2 442 2 453
8 2 436 2 447 2 458
9 2 441 2 452 2 463
10 2 451 2 457 2 468
11 2 451 2 462 2 473
12 2 456 2 467 2 478
13 2 461 2 472 2 483
14 2 473 2 484 2 495
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));
}
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 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;
}
来源:https://stackoverflow.com/questions/5485759/how-to-determine-a-wifi-channel-number-used-by-wifi-ap-network