C#使用Managed Wifi API连接带密码的SSID .

仲浩旷
2023-12-01
Managed Wifi API的安装和使用可以参考:
http://blog.csdn.net/m593192219/article/details/9363355

上面这篇文章有写了如何连接无密码的SSID,如何连接带密码的SSID就是本文提及的内容。

在MSDN上查找相关的配置,大致了解了下这个格式:

<?xml version="1.0" ?> 
 <WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
  <name>{0}</name> 
 <SSIDConfig>
 <SSID>
  <name>{0}</name> 
  </SSID>
  </SSIDConfig>
  <connectionType>ESS</connectionType> 
  <connectionMode>auto</connectionMode> 
 <MSM>
 <security>
 <authEncryption>
  <authentication>WPAPSK</authentication> 
  <encryption>TKIP</encryption> 
  <useOneX>false</useOneX> 
  </authEncryption>
 <sharedKey>
  <keyType>passPhrase</keyType> 
  <protected>false</protected> 
  <keyMaterial>{1}</keyMaterial> 
  </sharedKey>
  </security>
  </MSM>
  </WLANProfile>


{0}位置填写ssid名称,{1}位置填写密码即可。其他的参数我没有深究,需要注意的是authentication这个节点是加密方式。

然后修改连接的函数如下:

/// <summary>
	/// 连接到未加密的SSID
        /// </summary>
        /// <param name="ssid"></param>
        public WIFISSID ConnectToSSID(string profileName, string key)
        {
            WIFISSID ssid = null;
    
            Wlan.WlanReasonCode code = Wlan.WlanReasonCode.UNKNOWN;

            foreach (WIFISSID s in ssids)
            {
                if (s.SSID == profileName)
                {
                    ssid = s;
                    break;
                }
            }

            if (ssid == null) return null;

            string mac = StringToHex(profileName); // 

            switch (ssid.dot11DefaultAuthAlgorithm)
            {
                case Wlan.Dot11AuthAlgorithm.RSNA_PSK:

                    string trProfileXml1 = "<?xml version=\"1.0\"?><WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\"><name>{0}

</name><SSIDConfig><SSID><name>{0}

</name></SSID></SSIDConfig><connectionType>ESS</connectionType><connectionMode>auto</connectionMode><MSM><security><authEncryption><authentication>WPAPSK</au

thentication><encryption>TKIP</encryption><useOneX>false</useOneX></authEncryption><sharedKey><keyType>passPhrase</keyType><protected>false</protected><keyMa

terial>{1}</keyMaterial></sharedKey></security></MSM></WLANProfile>";
                    string profileXml = string.Format(trProfileXml1, profileName, key);

                    code = ssid.wlanInterface.SetProfile(Wlan.WlanProfileFlags.AllUser, profileXml, true);
                    ssid.wlanInterface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileName);

                    break;
                default:
                    break;
            }      

            return ssid;
        }

这样就可以连接带密码的SSID了。

另外,为了获取SSID的信号强弱使用Wlan.WlanAvailableNetwork类型下的wlanSignalQuality属性可以获取0-100的值来标识WIFI信号强度,但是这个值的变化比较慢,主要是因为WINDOWS的API的限制,如果使用Wlan.WlanBssEntry类型下的rssi属性,可以获取一个负值,这个值可以很精确的获取WIFI的信号强制,但是受制于系统需要高于Vista以上的系统

。WinXP是不支持的。

代码如下:

/// <summary>
        /// 枚举所有无线设备接收到的SSID
        /// </summary>
        public void ScanSSID()
        {
            WlanClient client = new WlanClient();
            foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
            {  
                // Lists all networks with WEP security                                
                Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0);                
                foreach (Wlan.WlanAvailableNetwork network in networks)
                {                    
                    WIFISSID targetSSID = new WIFISSID();
                    
                    targetSSID.wlanInterface = wlanIface;
                    targetSSID.wlanSignalQuality = (int)network.wlanSignalQuality;
                    targetSSID.SSID = GetStringForSSID(network.dot11Ssid);
                    //targetSSID.SSID = Encoding.Default.GetString(network.dot11Ssid.SSID, 0, (int)network.dot11Ssid.SSIDLength);
                    targetSSID.dot11DefaultAuthAlgorithm = network.dot11DefaultAuthAlgorithm;
                    targetSSID.dot11DefaultCipherAlgorithm = network.dot11DefaultCipherAlgorithm.ToString();

                    Wlan.WlanBssEntry[] bsswork = wlanIface.GetNetworkBssList(network.dot11Ssid, network.dot11BssType, network.securityEnabled);//XP系统这句

会报错
                    targetSSID.RSSI = bsswork[0].rssi;

                    ssids.Add(targetSSID);
                }
            }

        } // EnumSSID




 类似资料: