Android Question Inline Java Code for connect wifi with ssid and password programmatic.

dragonguy

Active Member
Licensed User
Longtime User
i write app when user click the button, mobile phone will auto connect to specific ssid and password.
below is my code:
B4X:
#MultiDex: True 'original app need this

Sub Globals
    Dim NativeMe As JavaObject
    Public pwd As String = "xxxxxx"
    Public wifissid As String = "xxxxxxxx"
End Sub

Sub Activity_Create(FirstTime As Boolean)
    NativeMe.InitializeContext
End Sub

Sub btn_Click
    NativeMe.Runmethod("ConnectToNetworkWPA",Array(wifissid,pwd))
End Sub

#If Java
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.app.Activity;
import android.content.Context;
import java.util.List;

public void ConnectToNetworkWPA(String networkSSID, String password)
    {
        try {
            WifiConfiguration conf = new WifiConfiguration();
            conf.SSID = "\"" + networkSSID + "\"";   // Please note the quotes. String should contain SSID in quotes

            conf.preSharedKey = "\"" + password + "\"";

            conf.status = WifiConfiguration.Status.ENABLED;
            conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
            conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
            conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
            conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);

            //Log.d("connecting", conf.SSID + " " + conf.preSharedKey);

            WifiManager wifiManager = (WifiManager) this.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            wifiManager.addNetwork(conf);

            //Log.d("after connecting", conf.SSID + " " + conf.preSharedKey);



            List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
            for( WifiConfiguration i : list ) {
                if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
                    wifiManager.disconnect();
                    wifiManager.enableNetwork(i.networkId, true);
                    wifiManager.reconnect();
                    //Log.d("re connecting", i.SSID + " " + conf.preSharedKey);

                    break;
                }
            }


            //WiFi Connection success, return true
            //return true;
        } catch (Exception ex) {
            //System.out.println(Arrays.toString(ex.getStackTrace()));
            //return false;
        }
    }
#End If

when i click the button,i get the error meesage below:
B4X:
java.lang.RuntimeException: Method: ConnectToNetworkWPA not found in: androidx.multidex.MultiDexApplication
    at anywheresoftware.b4j.object.JavaObject$MethodCache.getMethod(JavaObject.java:366)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:119)
    at com.hirise.contactlesselevator.starter$ResumableSub_udp_start.resume(starter.java:373)
    at com.hirise.contactlesselevator.starter._udp_start(starter.java:342)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
    at anywheresoftware.b4a.keywords.Common$11.run(Common.java:1179)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7397)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)

Please help look in the code where is wrong, i really grateful for your help.
Thank you.
 
Last edited:

MicroDrie

Well-Known Member
Licensed User
Longtime User
Please help look in the code where is wrong
Any person with a security background will point out that passwords written in plain text can cause third parties to read them. Therefore, this is not allowed because this weakness can be exploited by hackers.
 
Upvote 0

dragonguy

Active Member
Licensed User
Longtime User
Any person with a security background will point out that passwords written in plain text can cause third parties to read them. Therefore, this is not allowed because this weakness can be exploited by hackers.

i know, this esp8266 access point, don't have any info to hack. We also share the ssid and password to end user too.
i want let user click the button to connect to esp 8266.
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…