项目介绍:
1.使用的SimpleWifi开源库
2.需要在程序里边进行WIFI网络管理
3.英文的WIFI网络名正常连接
问题: 1、中文名称的WIFI 无法正常显示(显示乱码)
2、中文名称的WIFI无法正常连接
运行环境:
1. Windows 10 专业版
2. .NET Framework 4.6
首先来说下中文名称无法显示的问题,其实就是编码的原因。
查看源码就发现 使用的Encoding.ASCII 编码,显示中文修改为Encoding.UTF8编码即可。
修改前:
public string Name
{
get
{
return Encoding.ASCII.GetString(_network.dot11Ssid.SSID, 0, (int)_network.dot11Ssid.SSIDLength);
}
}
修改后:
public string Name
{
get
{
return Encoding.UTF8.GetString(_network.dot11Ssid.SSID, 0, (int)_network.dot11Ssid.SSIDLength).Trim();
}
}
到此中文名称显示的已经能解决,但会发现虽然中文的WIFI能显示出来了,但会出现连接不上的问题,账号密码都是正确的,这个问题真的困扰了我不少时间。
其他情况都正常,并且WIN10自身操作系统是支持中文WIFI的连接的,所以理应是支持的。
连接过的WIFI都会生成WLANProfile的xml文件,查阅了微软的相关文档
WPA2-Personal profile sample - Win32 apps | Microsoft Docs
下边是WLANProfile的模板
<?xml version="1.0" encoding="US-ASCII"?>
<WLANProfile
xmlns="https://www.microsoft.com/networking/WLAN/profile/v1">
<name>SampleWPA2PSK</name>
<SSIDConfig>
<SSID>
<name>SampleWPA2PSK</name>
</SSID>
</SSIDConfig>
<connectionType>ESS</connectionType>
<connectionMode>auto</connectionMode>
<autoSwitch>false</autoSwitch>
<MSM>
<security>
<authEncryption>
<authentication>WPA2PSK</authentication>
<encryption>AES</encryption>
<useOneX>false</useOneX>
</authEncryption>
</security>
</MSM>
</WLANProfile>
项目也是按照模板去生成并保存文件的,但发现仍然不能连接中文的WIFI,这又是为何呢?
直到查看了SSID相关的资料
SSID (SSIDConfig) Element - Win32 apps | Microsoft Docs
文章中提及 SSID下有两个节点hex和name,模板中只显示一个name
hex有啥用的呢,官方是这么解释的:
Remarks
Although the hex and name elements are optional, at least one hex or name element must appear as a child of the SSID element.
When profile information is converted to an SSID, the hex element is converted to the SSID (if present) and the name element is ignored. If the hex element is not present, the name element is converted to an SSID using Unicode to ASCII conversion.
When an SSID is stored in a profile, the hex element is always generated. The name element is only generated if the both the ASCII to Unicode conversion of the SSID and the XML profile generation are successful. Some information from the original SSID may be lost when it is converted to a name.
大致意思就是 hex和name必须要有一个,如果存在hex,则忽略name;如果不存在hex,则用name进行ASCII编码生成SSID去连接。
看到这就基本明白了,如果没有hex的话,对中文的支持是非常不友好的,那么解决思路就出来了,我们把hex加上去不就好了。
以下是部分修改的代码:
//名称要用UTF-8进行编码,才不会显示乱码
string name = Encoding.UTF8.GetString(network.dot11Ssid.SSID, 0, (int)network.dot11Ssid.SSIDLength).Trim();
//此处生成SSID的hex
string hex = GetHexString(network.dot11Ssid.SSID);
//修改对应模板文件的保存字符,把hex都添加进去
switch (network.dot11DefaultCipherAlgorithm)
{
case Dot11CipherAlgorithm.None:
template = GetTemplate("OPEN");
profile = string.Format(template, name, hex);
break;
case Dot11CipherAlgorithm.WEP:
template = GetTemplate("WEP");
profile = string.Format(template, name, hex, password);
break;
case Dot11CipherAlgorithm.CCMP:
if (authAlgo == Dot11AuthAlgorithm.RSNA)
{
template = GetTemplate("WPA2-Enterprise-PEAP-MSCHAPv2");
profile = string.Format(template, name, hex);
}
else // PSK
{
template = GetTemplate("WPA2-PSK");
profile = string.Format(template, name, hex, password);
}
break;
case Dot11CipherAlgorithm.TKIP:
#warning Robin: Not sure WPA uses RSNA
if (authAlgo == Dot11AuthAlgorithm.RSNA)
{
template = GetTemplate("WPA-Enterprise-PEAP-MSCHAPv2");
profile = string.Format(template, name, hex);
}
else // PSK
{
template = GetTemplate("WPA-PSK");
profile = string.Format(template, name, hex, password);
}
break;
default:
throw new NotImplementedException("Profile for selected cipher algorithm is not implemented");
}
最后在修改对应的XML模板文件
以下只举一个例子了:
<?xml version="1.0" encoding="utf-8"?>
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
<name>{0}</name>
<SSIDConfig>
<SSID>
<hex>{1}</hex>
<name>{0}</name>
</SSID>
</SSIDConfig>
<connectionType>ESS</connectionType>
<connectionMode>auto</connectionMode>
<autoSwitch>false</autoSwitch>
<MSM>
<security>
<authEncryption>
<authentication>WPA2PSK</authentication>
<encryption>AES</encryption>
<useOneX>false</useOneX>
</authEncryption>
<sharedKey>
<keyType>passPhrase</keyType>
<protected>false</protected>
<keyMaterial>{2}</keyMaterial>
</sharedKey>
</security>
</MSM>
</WLANProfile>
<!--
0 = Name
1 = HexName
2 = Key
-->
到此中文SSID连接的问题都解决啦。
还有一个WIFI列表刷新的问题,有解决思路,详情请查看: