如何确定 wifi AP/网络使用的 wifi 通道号?

发布于 2024-10-28 22:07:29 字数 210 浏览 5 评论 0原文

我发现几个 Android wifi 应用程序(WiFi 管理器、WiFi 分析器)除了 BSSID/SSID 等之外还显示 WiFi 网络的通道号。但我找不到有关它们如何执行此操作的任何信息。我唯一知道的是我可以获得一些 wifi 频率。也许他们确定了与该频率相对应的频道?有没有办法在android中检测wifi网络的通道?当然,这个信息没什么大不了的,没有它我也可以生活:)但我仍然很好奇......

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

吝吻 2024-11-04 22:07:29

根据无线电电子学。 com,频道数确实与频率相关。

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

对于 Android,ScanResult 包含频道的频率。

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.

撧情箌佬 2024-11-04 22:07:29
@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));
}
@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));
}
自此以后,行同陌路 2024-11-04 22:07:29

您可以使用几个公式来表达转换,每个公式一个。该函数返回给定频率的频道,如果该频率不是有效的 WiFi 频率(2.4GHz 和 5GHz),则返回 -1。

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;
    }
}

这是执行相同操作的紧凑方法。

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.

梦途 2024-11-04 22:07:29

根据标准[802.11-2012],有一种更简单的方法可以从频率计算出通道数。具体来说,

channel_center_Frequency = Channel_starting_Frequency + 5 * Channel_number

对于 5G 频段,
频道号 = 0, 1, ..., 200; channel_starting_Frequency = 5000 MHz。

对于2.4G频段,
频道号 = 1, 2, ..., 13; channel_starting_Frequency = 2047 MHz。

所有频道频率的列表可以在 WiFi 频道 中找到


将其转换为代码 - 请参阅 iw 来源:

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

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

    return freq/5 - 1000;
}

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;
}
夜雨飘雪 2024-11-04 22:07:29

添加到@artm的回答,这里是在Linux源代码中找到的更新的算法:

int ieee80211_freq_khz_to_channel(u32 freq)
{
    /* TODO: just handle MHz for now */
    freq = KHZ_TO_MHZ(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 < 5925)
        return (freq - 5000) / 5;
    else if (freq == 5935)
        return 2;
    else if (freq <= 45000) /* DMG band lower limit */
        /* see 802.11ax D6.1 27.3.22.2 */
        return (freq - 5950) / 5;
    else if (freq >= 58320 && freq <= 70200)
        return (freq - 56160) / 2160;
    else
        return 0;
}

来源:https://github.com/torvalds/linux/blob/master /net/wireless/util.c#L141,函数名称:ieee80211_freq_khz_to_channel

Adding to the answer by @artm, here is a more updated algorithm found in the Linux source code:

int ieee80211_freq_khz_to_channel(u32 freq)
{
    /* TODO: just handle MHz for now */
    freq = KHZ_TO_MHZ(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 < 5925)
        return (freq - 5000) / 5;
    else if (freq == 5935)
        return 2;
    else if (freq <= 45000) /* DMG band lower limit */
        /* see 802.11ax D6.1 27.3.22.2 */
        return (freq - 5950) / 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#L141, function name: ieee80211_freq_khz_to_channel

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文