c#使用ManagedWifi查看当前Wifi信号并选择wifi的示例
使用managedwifi查看当前wifi信号并选择wifi
using system;
using system.collections.generic;
using system.linq;
using system.text;
using nativewifi;
namespace wifiexample
{
class wifimanager
{
//cmcc的wifissid
public wifissid cmccwifissid;
public wifimanager()
{
scanssid();
}
/// <summary>
/// 将ssid转化成字符串
/// </summary>
static string getstringforssid(wlan.dot11ssid ssid)
{
return encoding.utf8.getstring(ssid.ssid, 0, (int)ssid.ssidlength);
}
/// <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.dot11defaultauthalgorithm = network.dot11defaultauthalgorithm.tostring();
targetssid.dot11defaultcipheralgorithm = network.dot11defaultcipheralgorithm.tostring();
console.writeline(targetssid.ssid);
if (targetssid.ssid.tolower().equals("cmcc"))
{
cmccwifissid = targetssid;
return;
}
}
}
} // enumssid
/// <summary>
/// 连接到cmcc
/// </summary>
/// <param name="ssid"></param>
public void connecttocmcc()
{
// connects to a known network with wep security
string profilename = cmccwifissid.ssid; // this is also the ssid
console.writeline("profilename" + profilename);
cmccwifissid.wlaninterface.connect(wlan.wlanconnectionmode.profile, wlan.dot11bsstype.any, profilename);
}
/// <summary>
/// 字符串转hex
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string stringtohex(string str)
{
stringbuilder sb = new stringbuilder();
byte[] bystr = system.text.encoding.default.getbytes(str); //默认是system.text.encoding.default.getbytes(str)
for (int i = 0; i < bystr.length; i++)
{
sb.append(convert.tostring(bystr[i], 16));
}
return (sb.tostring().toupper());
}
} // class wifimanager
class wifissid
{
public string ssid = "none";
public string dot11defaultauthalgorithm = "";
public string dot11defaultcipheralgorithm = "";
public bool networkconnectable = true;
public string wlannotconnectablereason = "";
public int wlansignalquality = 0;
public wlanclient.wlaninterface wlaninterface = null;
}
}
```
调用链接cmcc
===
```
wifimanager wm = new wifimanager();
wm.connecttocmcc();
上一篇: Spark on Yarn分析
下一篇: ASP.NET服务器控件的生命周期分析