在 JAVA 中查找 WIFI 状态

Find WIFI State in JAVA

在我的 java 应用程序中,我想查看 wifi 处于什么状态(例如 1 bar、2 bar 或无 wifi)

我想知道如何在本机 Java 中找到计算机的 wifi 状态,或者通过使用 ping/pong 应用程序,或者我是否必须解析 wifi 的状态直接申请

如有任何帮助,我们将不胜感激!

P.S。我没有使用 android

如果你想覆盖Windows平台,那么你可以使用这样的小方法:

public int getWirelessStrength() {
    // The returned integer value is in relation to strength percent.
    // If 75 is returned then the wireless signal strength is at 75%.
    List<String> list = new ArrayList<>();
    int signalStrength = 0;
    String cmd = "netsh wlan show interfaces"; 
    try {
        Process p = Runtime.getRuntime().exec("cmd /c " + cmd);
        p.waitFor();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
            String line;
            while ((line = reader.readLine()) != null) {
                list.add(line);
            }
        }
        if (p.isAlive()) { p.destroy(); }

        // Get the Signal Strength.
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).trim().toLowerCase().startsWith("signal")) {
                String[] ss = list.get(i).split(":");
                if(ss.length == 2) {
                    signalStrength = Integer.parseInt(ss[1].replace("%","").trim());
                }
                break;
            }
        }
    }
    catch (IOException | InterruptedException ex) { 
        Logger.getLogger("getWirelessStrength()").log(Level.SEVERE, null, ex);
    }
    return signalStrength;
}

以上方法利用了WindowsNetsh Command-line Utility to acquire the desired information. The returned integer result is a percent of signal strength so, if 75 is returned then the WiFi signal strength is 75%. If you want to do something similar for Unix or Linux then you can use the iwlist or iwconfig command and parse out the required signal strength in a similar fashion. For the MAC I believe you would need to use the Airport Utility.