如何使用wpf中的简单wifi连接到wifi?

时间:2017-07-10 10:47:05

标签: c# wpf

我正在尝试使用simplewifi连接到wifi。目前,我尝试过:

Wifi wifi = new Wifi();

// get list of access points
IEnumerable<AccessPoint> accessPoints = wifi.GetAccessPoints();

// for each access point from the list
foreach (AccessPoint ap in accessPoints)
{
    Console.WriteLine("ap: {0}\r\n", ap.Name);
    //check if SSID is desired
    if (ap.Name.StartsWith("z"))
    {
        //verify connection to desired SSID
        Console.WriteLine("connected: {0}, password needed: {1}, has profile: {2}\r\n", ap.Name, ap.IsConnected, ap.HasProfile);
        if (!ap.IsConnected)
        {
            //connect if not connected
            Console.WriteLine("\r\n{0}\r\n", ap.ToString());
            Console.WriteLine("Trying to connect..\r\n");

            AuthRequest authRequest = new AuthRequest(ap);
            authRequest.Password = "123456789";
            var x=ap.Connect(authRequest);

        }
    }
}

see the change in zshnu after I tried this code

如果我将密码硬编码为

,我无法传递密码
var password="abc123"

如何传递密码并连接?

此外,即使我必须连接到没有密码的wifi,connect方法也会返回false

2 个答案:

答案 0 :(得分:2)

您需要设置authRequest.Password = "yourpassword";

ap.connect(authRequest);

之前

https://github.com/DigiExam/simplewifi

添加以下console.write()

放在档案顶部

using SimpleWifi;

然后在你的函数中

// Wifi object
Wifi wifi = new Wifi();

// get list of access points
IEnumerable<AccessPoint> accessPoints = wifi.GetAccessPoints();

    // for each access point from list
foreach (AccessPoint ap in accessPoints){
    Console.WriteLine("ap: {0}\r\n", ap.Name);
    //check if SSID is desired
    if (ap.Name.StartsWith("ardrone_")){
        //verify connection to desired SSID
        Console.WriteLine("connected: {0}, password needed: {1}, has profile: {2}\r\n", ap.Name, ap.IsConnected, ap.HasProfile);
        if (!ap.IsConnected){
            //connect if not connected
            Console.WriteLine("\r\n{0}\r\n", ap.ToString());
            Console.WriteLine("Trying to connect..\r\n");
            AuthRequest authRequest = new AuthRequest(ap);
            ap.Connect(authRequest);
        }
    }
}

答案 1 :(得分:2)

在AccessPoint.Connect方法的参数中添加overwriteProfile设置为“ true”可以为我解决问题。

F#示例:

open SimpleWifi

[<EntryPoint>]
let main argv =
    let wifi = Wifi()
    let accessPoint = wifi.GetAccessPoints() 
                      |> Seq.find (fun (i:AccessPoint) -> i.Name = "Tpkl6" )

    let try1 = accessPoint.Connect( AuthRequest(accessPoint, Password="12345678"), true )
    let try2 = accessPoint.Connect( AuthRequest(accessPoint, Password="markvirchenko20"), true )

    printfn "%b\n%b" try1 try2
    0 // return an integer exit code

顺便说一句,您在注释中询问了ispasswordvalid函数:

我断开了所有互联网的连接并尝试连接,ispasswordvalid验证为true,但连接失败

ispasswordvalid实际上不会检查密码是否正确,即是否与wifi上设置的密码匹配。它只是检查密码是否具有有效的结构,例如至少8个符号长度。