问题
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...
回答1:
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.
回答2:
@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));
}
回答3:
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.
回答4:
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;
}
回答5:
Adding to the answer by @artm, here is a more updated algorithm found in the Linux source code:
int ieee80211_frequency_to_channel(int freq)
{
/* see 802.11 17.3.8.3.2 and Annex J */
if (freq == 2484)
return 14;
else if (freq < 2484)
return (freq - 2407) / 5;
else if (freq >= 4910 && freq <= 4980)
return (freq - 4000) / 5;
else if (freq < 5945)
return (freq - 5000) / 5;
else if (freq <= 45000) /* DMG band lower limit */
/* see 802.11ax D4.1 27.3.22.2 */
return (freq - 5940) / 5;
else if (freq >= 58320 && freq <= 70200)
return (freq - 56160) / 2160;
else
return 0;
}
Source: https://github.com/torvalds/linux/blob/master/net/wireless/util.c#L110
来源:https://stackoverflow.com/questions/5485759/how-to-determine-a-wifi-channel-number-used-by-wifi-ap-network