您的位置:首页 > 理论基础 > 计算机网络

使用ManagedWifi连接无线网络

2016-05-26 11:17 363 查看

自windows vista和windows XP SP2以后的版本,微软支持使用Native
wifi API来连接和管理无线网络。不过官方文档中有句话:Ad hoc mode might not be available in future versions of Windows. Starting with Windows 8.1 and Windows Server 2012 R2, use Wi-Fi Direct instead.高级模式将不被支持。从Windows
8.1和Windows Server 2012 R2开始,请使用Wi-Fi Direct替代。这个我们暂时先不用理会。

Native wifi API可以的下载地址:http://download.csdn.net/detail/libby1984/9529224

下面是代码:

扫描无线网络,首先实例化一个WlanClient的实例,遍历该实例中所有的WlanInterface。每一个WlanInterface 就是一个无线网络连接。然后遍历每个连接下搜索到的所有无线网络。如果WlanInterface的InterfaceState属性值为Connected,那么就表示该无线网络连接是当前连接的连接,如果想具体知道连接的是哪个无线网络,可以查看WlanInterface实例的CurrentConnection属性。下面代码中的WIFISSID类是记录netwoek参数的类,可以根据需要自己定义。

/// <summary>
/// 枚举所有无线设备接收到的SSID
/// </summary>
private List<WIFISSID> ScanSSID()
{
WlanClient client = new WlanClient();
List<WIFISSID> wifiList = new List<WIFISSID>();
string conectedNetworkName = string.Empty;
foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
{
List<string> profileNames = new List<string>();
// Lists all networks with WEP security
Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0);
foreach (Wlan.WlanAvailableNetwork network in networks)
{
if (wlanIface.InterfaceState == Wlan.WlanInterfaceState.Connected && wlanIface.CurrentConnection.isState == Wlan.WlanInterfaceState.Connected)
{
conectedNetworkName = wlanIface.CurrentConnection.profileName;
}

WIFISSID targetSSID = new WIFISSID();
if (network.networkConnectable)
{
targetSSID.SSID = network.dot11Ssid;
if (string.IsNullOrEmpty(network.profileName))
{
targetSSID.profileNames = GetStringForSSID(network.dot11Ssid);
}
else
{
targetSSID.profileNames = network.profileName;
}

if (!profileNames.Contains(targetSSID.profileNames))
{
profileNames.Add(targetSSID.profileNames);
targetSSID.wlanInterface = wlanIface;
targetSSID.wlanSignalQuality = (int)network.wlanSignalQuality;
targetSSID.dot11DefaultAuthAlgorithm = network.dot11DefaultAuthAlgorithm;
targetSSID.dot11DefaultCipherAlgorithm = network.dot11DefaultCipherAlgorithm.ToString();
targetSSID.securityEnabled = network.securityEnabled;
wifiList.Add(targetSSID);
if (!string.IsNullOrEmpty(conectedNetworkName) && conectedNetworkName.Equals(network.profileName))
{
targetSSID.connected = true;
}
else
{
targetSSID.connected = false;
}
}
}
}
}

return wifiList;
}

public class WIFISSID
{
        public string profileNames;
        public Wlan.Dot11Ssid SSID;
        public NativeWifi.Wlan.Dot11AuthAlgorithm dot11DefaultAuthAlgorithm;
        public string dot11DefaultCipherAlgorithm = "";
        public bool networkConnectable = true;
        public string wlanNotConnectableReason = "";
        public int wlanSignalQuality = 0;
        public WlanClient.WlanInterface wlanInterface = null;
        public bool securityEnabled;
        public bool connected = false;
} 


寻找当前连接的网络:
public static string GetCurrentConnection()
{
WlanClient client = new WlanClient();
foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
{
Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0);
foreach (Wlan.WlanAvailableNetwork network in networks)
{
if (wlanIface.InterfaceState == Wlan.WlanInterfaceState.Connected && wlanIface.CurrentConnection.isState == Wlan.WlanInterfaceState.Connected)
{
return wlanIface.CurrentConnection.profileName;
}
}
}

return string.Empty;
}


连接到加密的无线网络
/// <summary>
/// 连接到加密的SSID
/// </summary>
/// <param name="ssid"></param>
private WIFISSID ConnectToSSID(WIFISSID ssid, string key)
{
if (ssid == null)
{
return null;
}

string profileXml = string.Empty;
switch (ssid.dot11DefaultAuthAlgorithm)
{
case Wlan.Dot11AuthAlgorithm.RSNA_PSK:
profileXml = string.Format("<?xml version=\"1.0\" encoding=\"US-ASCII\"?><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><autoSwitch>false</autoSwitch>  <MSM>    <security>      <authEncryption>        <authentication>WPA2PSK</authentication>        <encryption>AES</encryption>        <useOneX>false</useOneX>      </authEncryption>      <sharedKey>        <keyType>passPhrase</keyType>        <protected>false</protected>        <keyMaterial>          {1}        </keyMaterial>      </sharedKey>    </security>  </MSM></WLANProfile>", ssid.SSID, key);
break;
case Wlan.Dot11AuthAlgorithm.IEEE80211_Open:
profileXml = string.Format("<?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><MSM><security><authEncryption><authentication>open</authentication><encryption>WEP</encryption><useOneX>false</useOneX></authEncryption><sharedKey><keyType>networkKey</keyType><protected>false</protected><keyMaterial>{1}</keyMaterial></sharedKey><keyIndex>0</keyIndex></security></MSM></WLANProfile>", ssid.SSID, key);
break;
case Wlan.Dot11AuthAlgorithm.WPA:
profileXml = string.Format("<?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><autoSwitch>false</autoSwitch><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>", ssid.SSID, key);
break;
default:
break;
}

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


连接到未加密的无线网络、
/// <summary>
/// 连接到未加密的SSID
/// </summary>
/// <param name="ssid"></param>
public void ConnectToSSID(WIFISSID ssid)
{
// Connects to a known network with WEP security
string profileName = ssid.profileNames;
string mac = this.StringToHex(profileName);

string myProfileXML = string.Format("<?xml version=\"1.0\"?><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>manual</connectionMode><MSM><security><authEncryption><authentication>open</authentication><encryption>none</encryption><useOneX>false</useOneX></authEncryption></security></MSM></WLANProfile>", profileName, mac);
ssid.wlanInterface.SetProfile(Wlan.WlanProfileFlags.AllUser, myProfileXML, true);
ssid.wlanInterface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileName);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: