Android Question Inline Java code

Rusty

Well-Known Member
Licensed User
Longtime User
I'm trying to sign into a (known) hidden SSID with this java code:
B4X:
...
Sub Process_Globals
...
Private NativeMe As JavaObject
...
            Dim Credentials(2) As String
            Credentials(0) = SSID
            Credentials(1) = Pass
            NativeMe.InitializeContext
            NativeMe.RunMethod("setWifiConfig", Credentials)    'run the Java method
...

#If JAVA
public void setwificonfig(string ssid, string sharedkey) {
            wificonfiguration conf = new wificonfiguration();
    conf.ssid = "\"" + ssid + "\"";   // please note the quotes. string should contain ssid in quotes

    conf.presharedkey = "\"" + sharedkey + "\"";

    conf.hiddenssid = true;
    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);
    conf.allowedprotocols.set(wificonfiguration.protocol.rsn);
    conf.allowedprotocols.set(wificonfiguration.protocol.wpa);

    wifimanager wifimanager = (wifimanager)context.getsystemservice(context.wifi_service);

    wifimanager.addnetwork(conf);

    list<wificonfiguration> list = wifimanager.getconfigurednetworks();
    for (wificonfiguration i : list) {
        if (i.ssid != null && i.ssid.equals("\"" + ssid + "\"")) {

            wifimanager.disconnect();

            wifimanager.enablenetwork(i.networkid, true);

            wifimanager.reconnect();

            wifimanager.saveconfiguration();
            break;
        }
    }
}
#End If
found here on stack overflow.
When I compile it, I am getting this error:
B4A Version: 7.30
Parsing code. (0.08s)
Compiling code. (0.26s)
Compiling layouts code. (0.01s)
Organizing libraries. (0.00s)
Generating R file. (0.13s)
Compiling debugger engine code. (6.43s)
Compiling generated Java code. Error
B4A line: 454
End Sub
javac 1.8.0_51
src\talkingsurvey\b4a\surveydeploy\main.java:2329: error: cannot find symbol
public void setwificonfig(string ssid, string sharedkey) {
^
symbol: class string
location: class main

Can someone assist me with this? I'm not sure what is wrong with it...
Thanks in advance,
Rusty
 

DonManfred

Expert
Licensed User
Longtime User
Dim Credentials(2) As String
Credentials(0) = SSID
Credentials(
1) = Pass
NativeMe.InitializeContext
NativeMe.RunMethod(
"setWifiConfig", Credentials)
is not the same as

B4X:
 setwificonfig(string ssid, string sharedkey)

The later one expects TWO Strings. But you are giving one Array.

Rewrite the java method to expect a array and split it in the java method into two strings....
 
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Is there a way to pass it two parameters?
I tried
B4X:
Setwificonfig(SSID, PSWD)
and it didn't like that either...?
Thanks for the help, Don...:)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
and it didn't like that either...?
It must work....
B4X:
    Dim Credentials(2) As String
    Credentials(0) = "SSSS"
    Credentials(1) = "PPPP"

    nativeMe.RunMethod("methodName", Credentials)

B4X:
public void methodName(String s1,String s2){
    BA.Log(s1 + " " + s2);
}

SSSS PPPP
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
There are SOME wrong propertynames in your code. After 4 or 5 fixed i gave up.
Find the rest by yourself....

B4X:
    Dim Credentials(2) As String
    Credentials(0) = $""SSSS""$ ' Add the quote in B4A....
    Credentials(1) = "PPPP"

Note that Objects and propertynames in Java are casesensitive... Starting with WifiConfiguration instead of wificonfiguration!
Paramtype in Java is String, not string.
and so on....

B4X:
import android.net.wifi.WifiConfiguration;
public void setwificonfig(String ssid, String sharedkey) {
    WifiConfiguration conf = new WifiConfiguration();
    conf.SSID = ssid;   // please note the quotes. string should contain ssid in quotes
    conf.preSharedKey = "\"" + sharedkey + "\"";

    conf.hiddenSSID = true;
    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);
    conf.allowedprotocols.set(WifiConfiguration.protocol.rsn);
    conf.allowedprotocols.set(WifiConfiguration.protocol.wpa);

    wifimanager wifimanager = (wifimanager)context.getsystemservice(context.wifi_service);

    wifimanager.addnetwork(conf);

    list<wificonfiguration> list = wifimanager.getconfigurednetworks();
    for (wificonfiguration i : list) {
        if (i.ssid != null && i.ssid.equals("\"" + ssid + "\"")) {

            wifimanager.disconnect();

            wifimanager.enablenetwork(i.networkid, true);

            wifimanager.reconnect();

            wifimanager.saveconfiguration();
            break;
        }
    }
}
 
Upvote 0

wes58

Active Member
Licensed User
Longtime User
You could try this code:
B4X:
Sub ConnectToWifi
    Dim ssid, pass As String
    Dim jo As JavaObject
    ssid = "your ssid"
    pass = "your password"
    jo.InitializeContext
    jo.RunMethod("setwificonfig", Array(ssid, pass))
End Sub

#If JAVA
import java.util.List;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.content.Context;
import android.util.Log;

     public void setwificonfig(String ssid, String sharedkey) {
        WifiConfiguration conf = new WifiConfiguration();
        conf.SSID = ssid;   // please note the quotes. string should contain ssid in quotes
        conf.preSharedKey = "\"" + sharedkey + "\"";

        conf.hiddenSSID = true;
        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);
        conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        Context context = this.getApplicationContext();
        WifiManager wifimanager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);

        wifimanager.addNetwork(conf);

        List<WifiConfiguration> list = wifimanager.getConfiguredNetworks();
//        Log.w("B4A", "size "+list.size());
//        Log.w("B4A", list.toString());
        for (WifiConfiguration i : list) {
            if (i.SSID != null && i.SSID.equals("\"" + ssid + "\"")) {

                wifimanager.disconnect();

                wifimanager.enableNetwork(i.networkId, true);

                wifimanager.reconnect();

                wifimanager.saveConfiguration();
                break;
            }
        }
    }
#End If
Compare your code with mine and you will understand what Don said about "case sensitivity"
My advise for future:
1. If you copy java code, make sure you do it exactly as the source is. As Don said, it is case sensitive
2. Copy you java code into Eclipse java project and you will get hints about imports that have to be added and any other errors.

I hope that it helps
 
Upvote 0
Top