使用ManagedWifi连接到c#中的wifi

时间:2014-11-12 12:43:07

标签: c# wifi

我正在尝试从我的代码连接到wifi网络。我发现Managedwifi是一个好方法。我用它来扫描wifi网络,这很好。但问题是我无法连接到网络,我无法在互联网上找到示例代码。我用Google搜索但没有结果! 请帮我连接到网络! 这是尝试过的代码!

static void Main(string[] args)
    {
        WlanClient client = new WlanClient();
        foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
        {
            // Lists all available networks
            Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0);
            foreach (Wlan.WlanAvailableNetwork network in networks)
            {
                Console.WriteLine("Found network with SSID {0} || Secured : {1}.", GetStringForSSID(network.dot11Ssid),network.securityEnabled);
            }
            //wlanIface.DeleteProfile("Xperia Arc");
            //string profileName = GetStringForSSID(networks[0].dot11Ssid); // this is also the SSID
            string profileName = "Xperia Arc";
            string mac = "52544131303235572D454137443638";
            string key = "9090090900";
            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);
            try
            {
                wlanIface.SetProfile(Wlan.WlanProfileFlags.AllUser, profileXml, true);
                wlanIface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileName);
            }
            catch (Exception)
            {
                Console.WriteLine("Error Occured!");
                //throw;
            }
            while (wlanIface.InterfaceState.ToString()=="Associating")
            {
                ;
            }
            Console.WriteLine(wlanIface.InterfaceState.ToString() + wlanIface.);
            //Console.WriteLine(wlanIface.CurrentConnection.profileName +" "+ wlanIface.InterfaceState.ToString());
        }

    }

    static string GetStringForSSID(Wlan.Dot11Ssid ssid)
    {
        return Encoding.ASCII.GetString(ssid.SSID, 0, (int)ssid.SSIDLength);
    }

1 个答案:

答案 0 :(得分:3)

有关ManagedWifi的一般用法,请参阅:C# Connect to Wifi Network with Managed Wifi API

关于配置文件xml中“mac”的问题:它的ssid为十六进制表示。

您的代码有多个问题:

  • 不要在所有接口上运行并连接到同一个wifi网络,只需使用第一个可用的接口
  • 等待接口状态“关联”的while循环不起作用,因为接口通常仍处于“Disconnected”状态。所以最好等到“已连接”或超时。

所以你的代码应该是这样的:

static void Main(string[] args)
    {
        WlanClient client = new WlanClient();
        WlanClient.WlanInterface wlanIface = client.Interfaces.FirstOrDefault();

        if(wlanIface == null)
        {
            Console.WriteLine("No Wifi Interface available!");
            throw new Exception("No Wifi Interface available!");
        }

        // Lists all available networks
        Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0);
        foreach (Wlan.WlanAvailableNetwork network in networks)
        {

            Console.WriteLine("Found network with SSID {0} || Secured : {1}.", GetStringForSSID(network.dot11Ssid),network.securityEnabled);
        }

        string profileName = "Xperia Arc";
        string ssid = profileName;
        byte[] ssidBytes = Text.Encoding.Default.GetBytes(ssid);
        string ssidHex = BitConverter.ToString(ssidBytes);
        ssidHex = ssidHex.Replace("-", "");
        string key = "9090090900";

        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>", ssid, ssidHex, key);

        try
        {
            wlanIface.SetProfile(Wlan.WlanProfileFlags.AllUser, profileXml, true);
            wlanIface.Connect(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileName);
        }
        catch (Exception)
        {
            Console.WriteLine("Error Occured!");
            //throw;
        }

        int timeout = 3000;
        while ((wlanIface.InterfaceState.ToString()<>"Connected") && (timeout >= 0))
        {
               System.Threading.Thread.Sleep(500);
               timeout -= 500;
        }

        Console.WriteLine(wlanIface.InterfaceState.ToString() + wlanIface);
        //Console.WriteLine(wlanIface.CurrentConnection.profileName +" "+ wlanIface.InterfaceState.ToString());
    }