Java中如何获取WiFi的名称和信息?


=Start=

缘由:

收集整理一下在Java中如何获取WiFi相关信息的方法,方便需要的时候使用。

正文:

参考解答:

简单来说就是:Java中没有原生的类来实现这个功能(不像获取IP或主机名时还有NetworkInterface这个类可用),可以使用JNI调用本机库或使用Runtime调用系统命令来实现这一底层一点的功能。

Java is not designed to do that kind of things, is hard to implement in a platform-independent way and any hardware-level detail can not be managed in Java by principle.

下面简单记录一下在macOS系统中使用Runtime调用系统命令来实现获取WiFi相关信息的功能:

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class JavaExecCmd {
    public static String execReadToString(String execCommand) throws IOException {
        try (Scanner s = new Scanner(Runtime.getRuntime().exec(execCommand).getInputStream()).useDelimiter("\\A")) {
            return s.hasNext() ? s.next() : "";
        }
    }

    public static Map<String, String> getWifiInfo() {
        Map<String, String> wifiMap = new HashMap<>();
        String macCommand = "/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I";
        String wifiInfo = null;
        try {
            wifiInfo = execReadToString(macCommand);
        } catch (IOException e) {
            e.printStackTrace();
            return wifiMap;
        }
        // System.out.println(wifiInfo);

        for (String line: wifiInfo.split("\n")) {
            if (line.contains(" SSID:")) {
                wifiMap.put("ssid", line.split(":", 2)[1].trim());
            } else if (line.contains("BSSID:")) {
                wifiMap.put("bssid", line.split(":", 2)[1].trim());
            }
        }

        return wifiMap;
    }

    public static void main(String[] args) {
        Map<String, String> result = null;
        result = getWifiInfo();
        System.out.println(result);
    }
}

在Windows上如何希望获取和WiFi相关的信息可以通过如下命令:

> netsh wlan show profiles

> for /f "skip=9 tokens=1,2 delims=:" %i in ('netsh wlan show profiles') do  @echo %j | findstr -i -v echo | netsh wlan show profiles %j key=clear

参考链接:

Java中如何获取WiFi的名称和信息?
https://stackoverflow.com/questions/1419569/wifi-information-in-java
https://notebookbft.wordpress.com/2015/11/25/wifi-information-extraction-java/

Finding SSID of a wireless network with Java
https://stackoverflow.com/questions/5378103/finding-ssid-of-a-wireless-network-with-java

macOS下如何获取WiFi名称等信息
https://stackoverflow.com/questions/4481005/get-wireless-ssid-through-shell-script-on-mac-os-x

https://apple.stackexchange.com/questions/176702/how-do-i-get-the-name-of-the-wifi-network-im-connected-to

https://stackoverflow.com/questions/29201132/get-only-ethernet-mac-address-via-command-prompt

=END=


《 “Java中如何获取WiFi的名称和信息?” 》 有 9 条评论

  1. Understanding the Network Terms SSID, BSSID, and ESSID
    https://www.juniper.net/documentation/en_US/junos-space-apps/network-director3.7/topics/concept/wireless-ssid-bssid-essid.html
    `
    An SSID is the Name of a Network. (SSID简单理解:WIFI名称)

    BSSIDs Identify Access Points and Their Clients. (BSSID简单理解:WIFI/AP的MAC地址)

    An ESS Consists of BSSs. (ESS是Extended basic Service Set的缩写,是一个比较抽象的概念,它实际上就和SSID相同)

    用戏称来说,bssid就是具体的某个连锁店编号(001)或地址,ssid就是连锁店的名字或者照片,essid就是连锁店的总公司或者招牌or品牌。一般ssid和essid都是相同的。
    `
    SSID、BSSID、ESSID区别?
    https://www.zhihu.com/question/24362037

    https://zh.wikipedia.org/wiki/%E6%9C%8D%E5%8A%A1%E9%9B%86_(%E6%97%A0%E7%BA%BF%E5%B1%80%E5%9F%9F%E7%BD%91)

  2. 请问下,通过 WifiManager 获取到的 wifi 加密信息,都是什么含义?
    https://www.v2ex.com/t/696069
    https://cyberforat.boum.org/HowtoConnect/HOWTOconnect.txt
    `
    so, here we have a lot of informations we need:

    – SSID is the name of the connection
    – channel is the channel on which the connections is broadcasting
    – BSSID is the MAC adress of your neighbour’s router
    – RSSI is the quality of the signal you receive from the router
    – Capability tells you if a network is open (ESS, ESS Short…) or crypted (ESS WEP)

    first, look at the capability: if it says WEP or WPA or some oscure settings, than we will not be able to connect (wait for the next workshop on “cracking WEP keys”).
    than, look at the RSSI: to be able to connect it MUST BE LOWER THAN 90! (89 is also good!)
    if the Capability is ESS, than let’s try to connect: (just type: wl join, and the SSID of where you whant to connect)
    即,当 Capability 那里的值为 ESS 时,表明此WIFI是没有密码的。
    `

  3. `
    简单总结就是:
    BSSID 是特定无线网络的标识符,而 MAC 地址是特定硬件的标识符(比如手机的MAC地址是手机的网卡的MAC地址,入网所需的唯一标识符)。

    There is a MAC address (MAC address) for APs (Access Points) in Infrastructure BSS networks and a BSSID for Independent BSS and ad hoc networks that is assigned randomly.
    基础设施 BSS 网络中的接入点(AP)有一个 MAC 地址(MAC 地址),独立 BSS 和临时(ad-hoc)网络则有一个随机分配的 BSSID。
    `

    The BSSID: The Unique Identifier For An Access Point
    https://bostinnovation.com/the-bssid-the-unique-identifier-for-an-access-point/
    `
    In computer networking, the basic service set identifier (BSSID) is the unique identifier attached to an access point (AP). The BSSID is used to specify a particular AP when multiple APs share the same basic service area. In 802.11 networks, the BSSID is derived from the MAC address of the AP. The BSSID is usually the MAC address of the radio interface that is used to communicate with stations in the BSS. In an infrastructure BSS, the BSSID is typically the MAC address of the AP. In an IBSS (ad hoc) network, the BSSID is generated by the software and is typically based on the MAC address of the station’s radio interface. The BSSID is used to identify a BSS. In an infrastructure BSS, the BSSID is used to identify an AP. In an IBSS, the BSSID is used to identify the IBSS. The BSSID is also used to form the 802.11 MAC address of an AP. The MAC address of an AP is derived from the BSSID and the MAC address of the radio interface used to communicate with the AP.
    在计算机网络中,基本服务集标识符(BSSID)是连接到接入点(AP)的唯一标识符。当多个接入点共享同一基本服务区时,BSSID 用于指定特定的接入点。在 802.11 网络中,BSSID 源自接入点的 MAC 地址。BSSID 通常是用于与 BSS 中的站点通信的无线电接口的 MAC 地址。在基础设施 BSS 中,BSSID 通常是 AP 的 MAC 地址。在 IBSS(ad hoc)网络中,BSSID 由软件生成,通常基于站点无线接口的 MAC 地址。BSSID 用于识别 BSS。在基础设施 BSS 中,BSSID 用于识别 AP。在 IBSS 中,BSSID 用于识别 IBSS。BSSID 还用于形成 AP 的 802.11 MAC 地址。AP 的 MAC 地址由 BSSID 和用于与 AP 通信的无线电接口的 MAC 地址得出。

    The BSSID is a unique identifier derived from the AP’s MAC address, which is incremented by the last number added to the AP’s MAC address in the last octet of the MAC address.
    BSSID 是源自 AP MAC 地址的唯一标识符,它以 MAC 地址最后一个八位位组中添加到 AP MAC 地址的最后一个数字递增。

    This is most often assigned to each NIC on a case-by-case basis.
    这通常是根据具体情况分配给每个 NIC 的。

    There is some confusion about whether a BSSID and MAC address are the same thing. A BSSID is the identifier for a specific wireless network, while a MAC address is the identifier for a specific piece of hardware. So while they are both unique identifiers, they are not the same thing.
    关于 BSSID 和 MAC 地址是否相同,存在一些混淆。BSSID 是特定无线网络的标识符,而 MAC 地址是特定硬件的标识符。因此,虽然它们都是唯一标识符,但并不是一回事。
    `

    Computer terms unwrapped: What is BSSID?
    计算机术语解读:什么是 BSSID?
    https://www.atera.com/blog/computer-terms-unwrapped-what-is-bssid/
    `
    For an admin – it’s important to stay on top of the BSSIDs in your environment, but for your end-users, they will probably only care about SSID. An end user will not notice when their BSSID changes, which could happen simply from moving from one part of a building to another – and it won’t have any impact on connectivity.
    对于管理员来说,了解环境中的 BSSID 是很重要的,但对于最终用户来说,他们可能只关心 SSID。终端用户不会注意到他们的 BSSID 发生了变化,而这种变化可能仅仅发生在从建筑物的一个区域移动到另一个区域的过程中,而且不会对连接性产生任何影响。

    As an administrator of an IT network, you need to think further than SSIDs, and consider BSSIDs as well. This is because inside each WLAN, there will be more than one access point. The BSSIDs are used to identify the access points and keep them separate from one another. While the SSID is the name of the network itself, the BSSID is the AP MAC address for each access point. While the user has no idea when they move from one BSS to another, (which as we said, will happen automatically without any noticeable change or impact to connectivity and availability) an administrator will want to stay on top of this information. This is because one part of the network could have too much traffic, or you might need to find a specific client.
    作为 IT 网络的管理员,您需要考虑的不仅仅是 SSID,还要考虑 BSSID。这是因为在每个 WLAN 中,都会有一个以上的接入点。BSSID 用于识别接入点,并将它们彼此分开。SSID 是网络本身的名称,而 BSSID 则是每个接入点的 AP MAC 地址。当用户从一个 BSS 转移到另一个 BSS 时,他们并不知道(正如我们所说,这将自动发生,不会对连接性和可用性产生任何明显的变化或影响),而管理员则希望随时掌握这些信息。这是因为网络的某个部分可能有太多的流量,或者您可能需要找到某个特定的客户端。
    `

    How is the BSSID derived from the Access Point ethernet MAC address?
    BSSID是如何从接入点以太网MAC地址派生的?
    https://community.arubanetworks.com/community-home/artificial-intelligence-tech-corner/viewdocument?DocumentKey=542aae7d-2473-47b0-8551-5481f125154f&CommunityKey=39a6bdf4-2376-46f9-853a-49420d2d0caa&tab=librarydocuments

  4. `
    # see help info
    $ /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport help

    # see
    $ /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport prefs
    `

    airport WiFi monitoring
    https://www.real-world-systems.com/docs/airport.1.html

    Make MacOS roam to the strongest wifi signal
    https://jay.gooby.org/2021/01/14/make-os-x-roam-to-the-strongest-wifi-signal
    `
    # Make a local bin folder if you haven’t already
    mkdir -p ~/bin

    # Symlink ~/bin/airport to the actual airport command
    ln -s /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport ~/bin/

    # Add ~/bin to your $PATH and add to your bash profile
    echo “export PATH=$PATH:~/bin” >> ~/.bash_profile

    # Finally, just run it, so you can call the airport alias
    # in your current bash session
    export PATH=$PATH:~/bin

    # Now you can use the airport command, to e.g. scan for networks
    airport -s

    # switch to the strongest signal
    sudo airport prefs joinMode=Strongest

    # check
    airport prefs joinMode
    `

  5. How to enable faster WiFi Roaming with Mac OS X & Airport base stations
    http://apple.stackexchange.com/a/144832
    `
    $ airport -s # Scanning
    $ airport prefs # Display airport preferences
    $ airport prefs joinMode=Strongest # Join mode
    $ airport prefs JoinModeFallback=KeepLooking

    # restore/还原设置
    $ airport prefs joinMode=Ranked
    $ airport prefs JoinModeFallback=Prompt
    `

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注