解决SimpleWifi无法连接中文SSID(中文WIFI名称)问题的过程记录

项目介绍:

        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列表刷新的问题,有解决思路,详情请查看:

WIFI列表 刷新问题 解决思路 C#_Rotion_深的博客-CSDN博客

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rotion_深

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值