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=

声明: 除非注明,ixyzero.com文章均为原创,转载请以链接形式标明本文地址,谢谢!
https://ixyzero.com/blog/archives/4808.html

《Java中如何获取WiFi的名称和信息?》上的5个想法

  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是没有密码的。
    `

发表评论

电子邮件地址不会被公开。 必填项已用*标注