B4A Library Wifi connect library (WEP,WPA,Open)

I am still missing library which enables me to connect to any type of secured wifi. So I made this code, but unfortunately due my lack of Java knowledge (actually its Eclipse which gives me headache) I am unable to build it.
public void connectToSSID(int tip) {

String networkSSID = txtUser.toString();
String networkPass = txtPass.toString();

WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\""; // Please note the quotes. String
// should contain ssid in quotes

tip = Integer.parseInt(txtTip.toString());

switch (tip) {
case 0: // OPEN
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

case 1: // WEP
conf.wepKeys[0] = "\"" + networkPass + "\"";
conf.wepTxKeyIndex = 0;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
break;

case 2: // WPA
conf.preSharedKey = "\"" + networkPass + "\"";

default:
break;
}

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

// CONNECT
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();

break;
}
}
}

I anybody is willing to join the project I would appreciate it.
 

jmoeller

Member
Licensed User
Longtime User
I am in the same situation as jnbarban - I have a nice little app that connects to a wifi SSID with WPA. However, that connection does not persist. If I go into Wifi, I can then manually save the connection but I need this to happen automatically. Any suggestions?

Jonathan
 

luke2012

Well-Known Member
Licensed User
Longtime User
Thanks for share this! I'm testing it and works fine in a simple way! :)
How to make a disconnect method in order to programmatically disconnect from the current connecion ?
I'm not familiar with developing B4A libraries starting from Java code :( Someone can help?
 

luke2012

Well-Known Member
Licensed User
Longtime User
Hi to all,
starting from my need a B4A friend "Meli Massimo" provided a library update (Beta version) to test with the following methods:

- DisconnectFromSSID (networkSSID as string)
- GetConfiguredNetworks
- GetWiFiEnabled
- SetWiFiEnabled (enabled as boolean)
- GetWiFiState

Anyone wish to test and improve this useful WiFi library ?
I'm trying to implement a network switch using this library.

So I wish to programmatically change the WiFi connection from a SSID to another SSID.

Attached you can find also the java version.
 

Attachments

  • WiFiConnect1.20.zip
    13.6 KB · Views: 553

luke2012

Well-Known Member
Licensed User
Longtime User
Here is the Java. If you improve it but don't want to post it yourself as a library then repost the source here and I will do it. I was thinking of extending this library but I don't have time to look at it at the moment.

Hi @agraham
I and italians Facebook guys (B4A Italia - https://www.facebook.com/groups/598542593522315/) worked on the WiFiConnect Java source and we update it to version 1.59 introducing several new methods (actually the count is about 50 methods) and fixed bugs within existing ConnectToSSID method (see the java source attached).

At the moment I'm testing 2 new methods : "updateInfo" and "getSSID" but in same cases getSSID return the last connected SSID but my device isn't connected to that SSID.

B4A code that I'm using for the tests (PE1 As PhoneEvents):

B4X:
Sub PE1_ConnectivityChanged (NetworkType As String, State As String, Intent As Intent)
    Log("ConnectivityChanged: " & NetworkType & ", state = " & State)
    Log(Intent.ExtrasToString)
   
    If State="CONNECTED" Then
   
        If NetworkType = "WIFI" Then
                   
            Private wfm As WiFiConnect
            wfm.Initialize
           
            wfm.updateInfo
            Private CurrentSSID As String = wfm.getSSID

            ToastMessageShow ("Current SSID = " & CurrentSSID, False)
                   
        End If
    End If
       
End Sub
 

Attachments

  • WiFiConnect_159.zip
    6.6 KB · Views: 459

jeronimovilar

Active Member
Licensed User
Longtime User
Hi,
what is this error:

main_activity_create (java line: 220)
java.lang.NoClassDefFoundError: anywheresoftware.b4a.jalle007.wificonnect.WiFiConnect
at tt.com.main._activity_create(main.java:220)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at tt.com.main.afterFirstLayout(main.java:84)
at tt.com.main.access$100(main.java:16)
at tt.com.main$WaitForLayout.run(main.java:72)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)

Natal - Brazil
 

hongbii khaw

Member
Licensed User
Longtime User
Hi all,
First of all, thanks for the library, it's very useful.
I can connect to WPA/WEP without a problem, but it just cannot connect to an OPEN type of network.
Anyone know what's the problem?
Thank you.
 

rtek1000

Active Member
Licensed User
Longtime User
Hello everyone, this library is a great alternative to MLWIFI / MLSCAN,

I have not previously achieved good results with the MLWIFI / MLSCAN libraries, and recently someone tried to improve MLWIFI for a version 4, but it was unstable in my tests, has moments that work or not, very slow in the search.

So I kept searching until I found this WifiConnect library.

I was very pleased with the response time, it is much faster than the MLSCAN library in network scanning.

I did not find a way to remove the network configuration, someone else said in another post to use a "disableNetwork" function but this does not erase the settings, it just leaves it marked off.

Follows my test app attachment, with it I can add a network similarly to what it was on Android 4.4
(Worked also on Android 7)

Need Dialogs4.01:
https://www.b4x.com/android/forum/threads/dialogs-library.6776/
 

Attachments

  • WiFiConnect_example.zip
    11 KB · Views: 570

DonManfred

Expert
Licensed User
Longtime User
Top