DigiExam / simplewifi

.NET 4.5 library to manage wifi connections in Windows - written in C#
GNU Lesser General Public License v3.0
131 stars 82 forks source link

example in vb.net #24

Open ghost opened 7 years ago

ghost commented 7 years ago

hello does anyone have the example in vb.net, please? thanks and regards

marcosdvpereira commented 7 years ago

hi hope this helps.

Public Class Wifi

    Public Shared Event ConnectionStatusChanged(isConnected As Boolean)

    Public Shared ReadOnly Property IsConnected() As Boolean
        Get
            Return _wifi.ConnectionStatus = SimpleWifi.WifiStatus.Connected
        End Get
    End Property

    Public Shared Function GetAccessPoints() As List(Of SimpleWifi.AccessPoint)
        Return _wifi.GetAccessPoints().
            OrderByDescending(Function(ap) ap.SignalStrength).
            ToList()
    End Function

    Public Shared Function Connect()
        If IsConnected Then Return True

        For Each ap In GetAccessPoints().Where(Function(x) x.HasProfile)
            If Connect(ap, New SimpleWifi.AuthRequest(ap)) Then Return True
        Next

        Return False
    End Function

    Public Shared Function Connect(ap As SimpleWifi.AccessPoint, ar As SimpleWifi.AuthRequest)
        Disconnect()

        AddHandler _wifi.ConnectionStatusChanged, AddressOf Wifi_ConnectionStatusChanged

        Return ap.Connect(ar)
    End Function

    Public Shared Sub Disconnect()
        _wifi.Disconnect()

        RemoveHandler _wifi.ConnectionStatusChanged, AddressOf Wifi_ConnectionStatusChanged
    End Sub

    Private Shared _wifi As SimpleWifi.Wifi =
        New SimpleWifi.Wifi()

    Private Shared Sub Wifi_ConnectionStatusChanged(sender As Object, e As SimpleWifi.WifiStatusEventArgs)
        RaiseEvent ConnectionStatusChanged(e.NewStatus = SimpleWifi.WifiStatus.Connected)
    End Sub

End Class
ghost commented 7 years ago

thanks a lot. but... how to use it? why 2 connect funcions? thanks and regards

marcosdvpereira commented 7 years ago

no problem you can catch some ideas from here

Sub Main()
    'try to connect to a known network
    Wifi.Connect()

    'get all avaliable networks to choose witch one you will connect
    Dim access_points = Wifi.GetAccessPoints()

    'imagine you choose the first one
    Dim access_point = access_points.First()
    Dim auth_access = New SimpleWifi.AuthRequest(access_point)

    If auth_access.IsPasswordRequired Then
        If Not access_point.HasProfile Then
            'TODO: request password, then
            auth_access.Password = "typed password"
        End If
    End If

    'try connect with given password
    If Not Wifi.Connect(access_point, auth_access) Then
        'if not, delete profile to request new attempt
        access_point.DeleteProfile()
    End If
End Sub
ghost commented 7 years ago

Thanks I got errors... "ConnectionStatus" is not a member of Wifi... Any suggestion? Thanks and regards

marcosdvpereira commented 7 years ago

Could you paste the exception?

ghost commented 7 years ago

The error is while compiling.

marcosdvpereira commented 7 years ago

Send me an example or attach your project.

ghost commented 7 years ago

i attach the project rename from ".png" to ."zip". i don't know why i can't send the ".zip". sorry for that. thanks and regards

simplewifi zip

marcosdvpereira commented 7 years ago

vb is not case sensitive, so you have to change your project name. in c# "simpleWifi" and "SimpleWifi" are two namespaces, but in vb is one. try not create projects with the same name as the libraries you use.

ghost commented 7 years ago

hi thanks, but the name is not involved in the problem... i think the problem is from declarations...

marcosdvpereira commented 7 years ago

change your assembly name and root namespace in project properties and the problem is fixed.

ghost commented 7 years ago

Yes! ;-) Fixed. I will try now to play with. Thanks a lot.

ghost commented 7 years ago

Another question: How can i know if i have internet connection? If i can reach, by example, www.google.com? thanks in advance.

marcosdvpereira commented 7 years ago

glad to be helpful, but try to not run from the main subject, if so, go to forums or ask google. :)

Dim isNetworkAvailable = My.Computer.Network.IsAvailable
Dim isInternetAvailable = My.Computer.Network.Ping("http://google.com")
ghost commented 7 years ago

ok. thanks a lot! Regards

ghost commented 7 years ago

Hi all. When the Wifi.Connect(access_point, auth_access) is called with wrong password, the app hangs completly while the timeout is reached. Is there any way to avoid app hangs while wait for the timeout? Or any way to set the timeout? Thanks

marcosdvpereira commented 7 years ago

hi, i didn't test it, but try it:

...
access_point.ConnectAsync(auth_access, onConnectComplete:=AddressOf OnConnectComplete)
...
Private Sub OnConnectComplete(isConnected As Boolean)

End Sub
ghost commented 7 years ago

Yes! Thanks!

shahrulamir15 commented 2 years ago

Hi, I already populate the wifi list into combo box, but how to connect the network. Below load the list of wifi

` Private Sub loadWifi() listWifi.Items.Clear() Dim accessPoints As IEnumerable(Of AccessPoint) = Wifi.GetAccessPoints()

    For Each ap As AccessPoint In accessPoints
        listWifi.Items.Add(ap.Name)
    Next

    Dim selectedAP As AccessPoint = listWifi.SelectedItem

End Sub`

And below I try to connect but not working

` Private Sub connectTo()

    Dim selectedAP As AccessPoint = listWifi.SelectedItem
    Dim authRequest As AuthRequest = New AuthRequest(selectedAP)
    Wifi.Connect(selectedAP, authRequest)

End Sub`

Thanks you @marcosdvpereira