window下,java连接wifi

原理:

通过Windows命令行中的netsh命令进行连接。

使用命令行执行以下命令:

1.查看已保存的Wi-Fi配置
netsh wlan show profiles

2.随便导出一个常用的。name为要导出的wifi名称  key=clear为隐藏密码
netsh wlan export profile name="WiFiName" folder="C:\" key=clear

3.去wifi设置里把上面选择的wifi 忘记掉

4.导入wifi配置,这一步是让系统记住你要导入的wifi
netsh wlan add profile filename="C:\\WLAN-WiFiName.xml"

5.连接。其实就是我们点击连接wifi这个过程。第3步我们忘记了wifi。第4步通过文件将wifi又记住了
netsh wlan connect name=WiFiName

原理就是这几个步骤

第2步导出的文件解析:

<?xml version="1.0"?>
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
	<name>seven</name><!--wifi名称-->
	<SSIDConfig>
		<SSID>
			<hex>736576656E</hex> <!--wifi名称的16进制字符串-->
			<name>seven</name> <!--wifi名称-->
		</SSID>
	</SSIDConfig>
	<connectionType>ESS</connectionType>
	<connectionMode>manual</connectionMode><!--manual手动连接  auto设置为自动连接 -->
	<MSM>
		<security>
			<authEncryption>
				<authentication>WPA2PSK</authentication>
				<encryption>AES</encryption>
				<useOneX>false</useOneX>
			</authEncryption>
			<sharedKey>
				<keyType>passPhrase</keyType>
				<protected>true</protected><!--这里是开启密码保护-->
				<keyMaterial>128590129075</keyMaterial><!--你的密码的加密后的数据-->
			</sharedKey>
		</security>
	</MSM>
	<MacRandomization xmlns="http://www.microsoft.com/networking/WLAN/profile/v3">
		<enableRandomization>false</enableRandomization>
		<randomizationSeed>79900722</randomizationSeed>
	</MacRandomization>
</WLANProfile>

修改文件为自己所用:

1.将<name>xxx</name>改成自己的wifi名,注意有两个<name>xxx</name>

2.计算出自己的wifi名的hex,将<hex>xxx</hex>改成计算出来的hex。

3.设置连接模式<connectionMode>manual</connectionMode> manual手动连接  auto设置为自动连接。你打开电脑的wifi连接,看到有个自动连接的勾选框,这里的作用就是这个。

4.修改密码设置
<sharedKey>
                <keyType>passPhrase</keyType>
                <protected>true</protected>   改成<protected>false</protected>
                <keyMaterial>128590129075</keyMaterial>改成自己的wifi密码,比如12345678
</sharedKey>

5.执行命令行命令

        1.netsh wlan add profile filename="C:\\修改后的.xml"

        2.netsh wlan connect name=修改后的wifi名称

java代码实现 

public class WiFiConnect {
    public static void main(String[] args) {
		String ssid = "seven"; // Wi-Fi名称
		String password = "12345678"; // Wi-Fi密码
		String connectedMode = "auto";//连接模式 <!--manual手动连接 auto自动连接 -->
		connectToWiFi(ssid, password,connectedMode);
	}

    /*字符串转16进制HEX*/
    public static String stringToHex(String input) {
		StringBuilder hexString = new StringBuilder();
		for (byte b : input.getBytes()) {
			hexString.append(String.format("%02x", b));
		}
		return hexString.toString();
	}

    /**
	 * 连接wifi
	 * @param ssid          wifi名称
	 * @param password      wifi密码
	 * @param connectedMode 连接模式
	 */
	public static void connectToWiFi(String ssid, String password,String connectedMode) {
		try {
            //计算出wifi名称队名的hex
			String ssidHex = StrUtil.stringToHex(ssid);
			//@formatter:off
			String profileXml
			=         "<?xml version=\"1.0\"?>\r\n"
					+ "<WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\">\r\n"
					+ "	<name>"+ssid+"</name>\r\n"
					+ "	<SSIDConfig>\r\n"
					+ "		<SSID>\r\n"
					+ "			<hex>"+ssidHex+"</hex>\r\n"
					+ "			<name>"+ssid+"</name>\r\n"
					+ "		</SSID>\r\n"
					+ "	</SSIDConfig>\r\n"
					+ "	<connectionType>ESS</connectionType>\r\n"
					+ "	<connectionMode>"+connectedMode+"</connectionMode>\r\n"
					+ "	<MSM>\r\n"
					+ "		<security>\r\n"
					+ "			<authEncryption>\r\n"
					+ "				<authentication>WPA2PSK</authentication>\r\n"
					+ "				<encryption>AES</encryption>\r\n"
					+ "				<useOneX>false</useOneX>\r\n"
					+ "			</authEncryption>\r\n"
					+ "			<sharedKey>\r\n"
					+ "				<keyType>passPhrase</keyType>\r\n"
					+ "				<protected>false</protected>\r\n"
					+ "				<keyMaterial>"+password+"</keyMaterial>\r\n"
					+ "			</sharedKey>\r\n"
					+ "		</security>\r\n"
					+ "	</MSM>\r\n"
					+ "	<MacRandomization xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v3\">\r\n"
					+ "		<enableRandomization>false</enableRandomization>\r\n"
					+ "		<randomizationSeed>79900722</randomizationSeed>\r\n"
					+ "	</MacRandomization>\r\n"
					+ "</WLANProfile>\r\n"
					+ "";
			//@formatter:on

			// 将上面一大堆字符串写入到一个文件 写C盘可能会有权限不足的情况
			String filePath = "D:\\WLAN-myWifi.xml";
         
            FileOutputStream fos = new FileOutputStream(filePath);
			fos.write(profileXml.getBytes());
			fos.close();

			// 使用netsh命令配置Wi-Fi
			String connectCommand="netsh wlan add profile filename="\"+filePath+"\"";
			Process addProfileProcess = Runtime.getRuntime().exec(connectCommand);
			addProfileProcess.waitFor();

			// 手动连接Wi-Fi命令
			String autoConnectCommand = "netsh wlan connect name=" + ssid;
			Process connectProcess = Runtime.getRuntime().exec(autoConnectCommand);
			connectProcess.waitFor();
		} catch (IOException | InterruptedException e) {
			e.printStackTrace();
		}
	}
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值