B4J Question How can i get wifi ssid ?

Erel

B4X founder
Staff member
Licensed User
Longtime User
Windows only solution, tested on a single computer:
B4X:
Wait For (FindSSID) Complete (Result As String)
Log("SSID: " & Result)


Sub FindSSID As ResumableSub
   Dim shl As Shell
   shl.Initialize("shl", "cmd", Array("/c", "netsh", "wlan", "show", "networks"))
   shl.Run(-1)
   Wait For shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
   If Success And ExitCode = 0 Then
       Dim m As Matcher = Regex.Matcher2("SSID 1\s*:\s*(.*)$", Regex.MULTILINE, StdOut)
       If m.Find Then
           Return m.Group(1)
       End If
   End If
   Return ""
End Sub
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
Hi to all
although it is a simple code and I don't know if a library has been created for it in the meantime, I post Erel's code modified to have a map with all SSIDs
Of course you can do better.

B4X:
Wait For (FindSSID) Complete (Result As Map)
If Result.IsInitialized then
    .......
End If

Sub FindSSID As ResumableSub
   Dim shl As Shell
   Dim ms As Map
   shl.Initialize("shl", "cmd", Array("/c", "netsh", "wlan", "show", "networks"))
   shl.Run(-1)
   Wait For shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
   If Success And ExitCode = 0 Then
        ms.Initialize
        Dim dati() As String = Regex.Split(CRLF, StdOut)
        For i = 0 To dati.Length-1
            Dim m As Matcher = Regex.Matcher("SSID \d*\s*:\s*(.*)$", dati(i))
            If m.Find Then
                ms.Put(m.Match.SubString2(0,m.Match.IndexOf(":")).Trim,m.Match.SubString(m.Match.IndexOf(":")+1).Trim)
            End If
        Next
   End If
   Return ms
End Sub
 
Last edited:
Upvote 0

Magma

Expert
Licensed User
Longtime User
OK...

and before someone ask how to see passwords of already connected/saved wifi...

that is the cmd command will give us the passwords (where profile name - the name of network):
netsh wlan show profile name="NETWORK" key=clear
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
Hi
I needed more data and fixed the code a bit, now without the use of Regex.Matcher

B4X:
Sub FindWiFi
    Wait For (FindSSID) Complete (Result As Map)
    If Result.IsInitialized Then
        For i = 0 To Result.Size-1
            Dim m As Map = Result.Get(Result.GetKeyAt(i))
            Log(Result.GetKeyAt(i))
            Log(m.Get("name"))
            Log(m.Get("autentication"))
            Log(m.Get("signal"))
            Log(m.Get("frequency"))
            Log(m.Get("channel"))
            Log("----------------")
        Next
    End If
End Sub

Sub FindSSID As ResumableSub
    Dim shl As Shell
    Dim ms, mdati As Map
    shl.Initialize("shl", "cmd", Array("/c", "netsh", "wlan", "show", "networks", "mode=", "Bssid"))
    shl.Run(-1)
    Wait For shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    If Success And ExitCode = 0 Then
        ms.Initialize
        Dim dati() As String = Regex.Split(Chr(10) & Chr(13), StdOut)
        For i = 0 To dati.Length-1
            Dim subdati() As String = Regex.Split(CRLF, dati(i))
            If subdati.Length>0 Then
                If subdati(1).StartsWith("SSID ") Then
                    mdati.Initialize
                    mdati.Put("name", subdati(1).SubString(subdati(1).IndexOf(":")+1).Trim)
                    mdati.Put("autentication", subdati(3).SubString(subdati(3).IndexOf(":")+1).Trim)
                    mdati.Put("signal", subdati(6).SubString(subdati(6).IndexOf(":")+1).Trim)
                    mdati.Put("frequency", subdati(7).SubString(subdati(7).IndexOf(":")+1).Trim)
                    mdati.Put("channel", subdati(8).SubString(subdati(8).IndexOf(":")+1).Trim)
                    ms.Put(subdati(1).SubString2(0,subdati(1).IndexOf(":")).Trim,mdati)
                End If
            End If
        Next
    End If
    Return ms
End Sub
1704031472529.png
 
Last edited:
Upvote 0
Top