首先,是windows系统。

然后,java代码可以使用命令行连接wifi,并且判断连接是否成功。

java代码如下:


import java.io.*;
import java.nio.charset.StandardCharsets;

public class wifiMain {

    public static boolean connectToSecuredWifi(String ssid, String password, String charset, long sleepTime, String judgeStr) throws Exception {
        // 1. 生成 Wi-Fi 配置 XML
        String xmlContent = generateWifiXml(ssid, password);
        String xmlPath = "C:\\example\\wifi.xml";
        try (Writer writer = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(xmlPath), StandardCharsets.UTF_8))) {
            writer.write(xmlContent);
        }

        // 2. 添加 Wi-Fi 配置
        Process addProfile = Runtime.getRuntime().exec("netsh wlan add profile filename=\"" + xmlPath + "\"");
        addProfile.waitFor();

        // 3. 连接 Wi-Fi
        Process connect = Runtime.getRuntime().exec("netsh wlan connect name=\"" + ssid + "\"");
        String line;
        BufferedReader infoReader = new BufferedReader(new InputStreamReader(connect.getInputStream(), charset));
        while ((line = infoReader.readLine()) != null) {
            //System.out.println(line);
        }
        BufferedReader errorReader = new BufferedReader(new InputStreamReader(connect.getErrorStream(), charset));
        while ((line = errorReader.readLine()) != null) {
            //System.out.println(line);
        }


        connect.waitFor();
        //状態                   : 認証中
        //状態                   : 接続されました
        Thread.sleep(sleepTime);
        return checkLink(charset, judgeStr);
    }

    private static String generateWifiXml(String ssid, String password) {
        return "<?xml version=\"1.0\"?>\n" +
                "<WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\">\n" +
                "  <name>" + ssid + "</name>\n" +
                "  <SSIDConfig>\n" +
                "    <SSID>\n" +
                "      <name>" + ssid + "</name>\n" +
                "    </SSID>\n" +
                "  </SSIDConfig>\n" +
                "  <connectionType>ESS</connectionType>\n" +
                "  <connectionMode>manual</connectionMode>\n" +
                "  <MSM>\n" +
                "    <security>\n" +
                "      <authEncryption>\n" +
                "        <authentication>WPA2PSK</authentication>\n" +
                "        <encryption>AES</encryption>\n" +
                "        <useOneX>false</useOneX>\n" +
                "      </authEncryption>\n" +
                "      <sharedKey>\n" +
                "        <keyType>passPhrase</keyType>\n" +
                "        <protected>false</protected>\n" +
                "        <keyMaterial>" + password + "</keyMaterial>\n" +
                "      </sharedKey>\n" +
                "    </security>\n" +
                "  </MSM>\n" +
                "</WLANProfile>";
    }

    public static boolean checkLink(String charset, String judgeStr) throws Exception{
        Process result = Runtime.getRuntime().exec("netsh wlan show interfaces");
        String line;
        StringBuilder infoSb = new StringBuilder();
        BufferedReader infoReader = new BufferedReader(new InputStreamReader(result.getInputStream(), charset));
        while ((line = infoReader.readLine()) != null) {
            infoSb.append(line).append("\r\n");
            //System.out.println(line);
        }
        BufferedReader errorReader = new BufferedReader(new InputStreamReader(result.getErrorStream(), charset));
        while ((line = errorReader.readLine()) != null) {
            //System.out.println(line);
        }
        result.waitFor();
        //System.out.println(infoSb.toString());
        if(infoSb.toString().contains(judgeStr)){
            return true;
        }else{
            return false;
        }

    }

    public static void main(String[] args) throws Exception {
        String wifiName = "my-wifi-localhost";         //Wi-Fi 名
        //可以写多个密码
        String[] passwords = {"12345", "123456"};   // 密码
        //间隔时间,直接判断是否连接成功,容易出现连接中,并不能知道是否连接成功。 需要等会
        long sleepTime = 2000;
        //判断连接成功的关键字,看自己是什么语言,可能是success、连接成功等
        String judgeStr = "接続されました";
        //AES加密的wifi,具体看xml
        //TODO 如果是其他加密方法,不是AES,需要改上面的xml
        for (int i = 0; i < passwords.length; i++) {
            //注意字符格式,本人电脑是这个格式,一般用GBK,UTF-8
            if(connectToSecuredWifi(wifiName, passwords[i], "SHIFT_JIS", sleepTime, judgeStr)){
                System.out.println("连接成功");
                break;
            }else{
                System.out.println("连接失败");
            }
        }
    }
}

说明:
可以用多个密码试连,不过一个就需要sleep 2秒,似乎不太实用。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐