B4J Question Connect to WIFI (SSID) B4J

yaqoob

Active Member
Licensed User
Longtime User
Hello everyone,
I am writing an application in B4J that reads data from a device connected to local Wifi (not connect to the internet) and sends back the same data to a device connected to the internet through another Wifi. My question is how to connect to a specific Wifi or SSID and disconnect from it and connect to another using B4j. I looked in the Forum, and I could not find a thread about connecting to a WIFI.
Thank you for your support
 

yaqoob

Active Member
Licensed User
Longtime User
Thank you Erel for your reply. I am using a Raspberry Pi to do the job. Is there a way using Raspberry Pi?

Thank you.
 
Upvote 0

Chris2

Active Member
Licensed User
Some time ago I was playing around with a server app that allowed changing the Wifi SSID and password on a Pi.
I made use of jShell and the link that @MathiasM provided above.
This was quite a long time ago and I don't remember how far it got so treat this as untested (amateur) code:
B4X:
Private Sub ChangeWifiSettings (newSSID As String, newPW As String, enabled As Boolean, strCountry As String)
    BuildSupplicant(strCountry, newSSID, newPW) 'build new wpa_supplicant.conf file
    Wait For BuildSupplicant_Complete
    RebootPi   'I think I found other ways of restarting the wifi connection unreliable. You might have better luck.
End Sub

Private Sub BuildSupplicant (strCountry As String, newSSID As String, strPW As String)
    Getpsk(newSSID, strPW)
    Wait For shpsk_Complete 'strPhrase now has encrypted passphrase
    'need to add handling if shpsk_ProcessCompleted success=false

    Dim str As String = $"ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev"$ & CRLF & _
                        $"update_config=1"$ & CRLF & _
                        $"country="$ & strCountry & CRLF & _
                        CRLF & _
                        $"network={"$ & CRLF & _
                        $"        ssid=""$ & newSSID & $"""$ & CRLF & _
                        $"        "$ & strPhrase
    File.WriteString("/etc/wpa_supplicant", "wpa_supplicant.conf", str)
    CallSubDelayed(Me, "BuildSupplicant_Complete")
End Sub

'get encrypted psk for use in wpa_supplicant.conf file
Private Sub Getpsk (strSSID As String, strPW As String)
    Dim str1, str2 As String
    str1 = $"""$ & strSSID & $"""$
    str2 = $"""$ & strPW & $"""$
    sh.Initialize("shpsk", "wpa_passphrase", Array As String(str1, str2))
    sh.WorkingDirectory = "/home/pi"
    sh.Run(-1)
End Sub

Private Sub shpsk_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    If Success And ExitCode = 0 Then
        Log("shpsk success" & CRLF & StdOut)
        Dim idxPSK As Int = StdOut.LastIndexOf($"psk="$)
        strPhrase = StdOut.SubString(idxPSK)    'strPhrase is Global Variable
    Else
        Log("shpsk shell Error: " & CRLF & StdErr)
    End If
    CallSubDelayed(Me, "shpsk_Complete")
End Sub

Private Sub RebootPi
    sh.Initialize("shReboot", "reboot", Array As String("now"))
    sh.WorkingDirectory = "/home/pi"
    sh.Run(-1)
End Sub
 
Upvote 0
Top