我使用NativeWifi API连接到特定的无线网络,但我似乎得到了配置文件已损坏的错误。我尝试连接而不覆盖配置文件,但这也不起作用。我知道我的ssid和密码是正确的。我认为我正在阅读的Mac是不正确的,但此时我已经尝试了所有我能想到的mac。如果有人能指出我正确的方向,我将不胜感激。
大部分代码都是我在网上找到的,但我找不到为什么这个有用的解决方案对我不起作用。请记住,我想要建立的连接是我的手机热点。
仅供参考:我已经尝试了所有重复的问题建议,但解决方案对我不起作用。
WlanClient client = new WlanClient();
foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
{
if (wlanIface.CurrentConnection.profileName == "AndroidAP")
{
if (wlanIface.CurrentConnection.isState == Wlan.WlanInterfaceState.Connected)
{
Console.WriteLine("Already connected to AndroidAP");
Console.ReadKey();
}
}
// Retrieves XML configurations of existing profiles.
// This can assist you in constructing your own XML configuration
// (that is, it will give you an example to follow).
foreach (Wlan.WlanProfileInfo profileInfo in wlanIface.GetProfiles())
{
string name = profileInfo.profileName; // this is typically the network's SSID
string xml = wlanIface.GetProfileXml(profileInfo.profileName);
}
string mac = String.Empty;
foreach (Wlan.WlanBssEntry network2 in wlanIface.GetNetworkBssList())
{
byte[] macAddr = network2.dot11Bssid;
if (GetStringForSSID(network2.dot11Ssid) == "AndroidAP")
{
for (int i = 0; i < macAddr.Length - 1; i++)
{
mac += macAddr[i].ToString("x2").PadLeft(2, '0').ToUpper();
}
// Connects to a known network with WEP security
string profileName = "AndroidAP"; // this is also the SSID
string key = "password";
string profileXml = 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><MSM><security><authEncryption><authentication>open</authentication><encryption>WEP</encryption><useOneX>false</useOneX></authEncryption><sharedKey><keyType>networkKey</keyType><protected>false</protected><keyMaterial>{2}</keyMaterial></sharedKey><keyIndex>0</keyIndex></security></MSM></WLANProfile>", profileName, mac, key);
//wlanIface.SetProfile(Wlan.WlanProfileFlags.AllUser, profileXml, true);
wlanIface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileName);
}
}
}
答案 0 :(得分:0)
根据SSIDConfig
您不应在<hex></hex>
元素中提供mac地址,而应提供<name></name>
的十六进制表示。
另请注意(来自上面的链接):
虽然hex和name元素是可选的,但至少有一个hex或name元素必须作为SSID元素的子元素出现。