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

jalle007

Active Member
Licensed User
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.
 

bluedude

Well-Known Member
Licensed User
Does not work, errors on WIFI type

Hi,

When I connect it gives me exception errors.

I connect this way:

wifi.connectToSSID (wifi.WIFI_WEP ,"home","test")
 

bluedude

Well-Known Member
Licensed User
Exception details

Below the exception details:

sending message to waiting queue (OnActivityResult)
running waiting messages (1)
main_parsetag (java line: 1169)
java.lang.NullPointerException


at anywheresoftware.b4a.jalle007.wificonnect.WiFiConnect.connectToSSID(WiFiConnect.java:69)
at com.thingstank.apps.senseme.main._parsetag(main.java:1169)
at com.thingstank.apps.senseme.main._barreader_result(main.java:427)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:167)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:155)
at ice.zxing.b4aZXingLib$1.ResultArrived(b4aZXingLib.java:48)
at anywheresoftware.b4a.BA$4.run(BA.java:461)
at anywheresoftware.b4a.BA.setActivityPaused(BA.java:373)
at com.thingstank.apps.senseme.main$ResumeMessage.run(main.java:211)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
java.lang.NullPointerException
** Activity (main) Resume **
 

agraham

Expert
Licensed User
It looks like

List list = wifiManager.getConfiguredNetworks();

returned null as the documentation so helpfully says!

"Upon failure to fetch or when when Wi-Fi is turned off, it can be null."

In the attached version 1.1 I've added a check for that and also an IsWiFiEnabled method that should return True if WiFi is enabled. Please try it.
 

Attachments

jalle007

Active Member
Licensed User
Unfortunately this lib now doesn't work good form me.
It simply wont connect to any type of networks.

@agraham
could you please repost java source here or send it to PM

Thanks

edit:
thorugh log I can see that library executes at
B4X:
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
Log.w("WiFiConnect","Connected!");
showing "connected!" but actually it is not .

The problem I think is that android wifimanager creates duplicate wifi configurations.
although i did not add wifimanage.save() it still remembers all networks every time i connect or fail to connect

anyone with idea ?
 
Last edited:

agraham

Expert
Licensed 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.
 

Attachments

suciwulans

Member
Licensed User
Agraham, do you have a sample projectwhich connect to wifi use WiFiConnect Library?
can you post here the project?
:sign0085:
 

imgsimonebiliato

Well-Known Member
Licensed User
Hello,
I need to create an app that connect the tablet to different WPA or WPA2 networks wifi.

Trying this simple code:

B4X:
Dim w1 AsWiFiConnect
w1.connectToSSID(w1.WIFI_WPA, "test", "test")
If w1.IsWiFiEnabled Then Msgbox("connesso", "")
The application raise me this error
B4X:
PackageAdded: package:it.android.imgspa.and_WifiChange
** Activity (main) Create, isFirst = true **
main_activity_create (B4A line: 33)
w1.connectToSSID(w1.WIFI_WPA, "test", "test")
java.lang.SecurityException: WifiService: Neither user 10087 nor current process has android.permission.CHANGE_WIFI_STATE.
    at android.os.Parcel.readException(Parcel.java:1425)
    at android.os.Parcel.readException(Parcel.java:1379)
    at android.net.wifi.IWifiManager$Stub$Proxy.addOrUpdateNetwork(IWifiManager.java:610)
    at android.net.wifi.WifiManager.addOrUpdateNetwork(WifiManager.java:620)
    at android.net.wifi.WifiManager.addNetwork(WifiManager.java:582)
    at anywheresoftware.b4a.jalle007.wificonnect.WiFiConnect.connectToSSID(WiFiConnect.java:61)
    at it.android.imgspa.and_WifiChange.main._activity_create(main.java:242)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:167)
    at it.android.imgspa.and_WifiChange.main.afterFirstLayout(main.java:89)
    at it.android.imgspa.and_WifiChange.main.access$100(main.java:16)
    at it.android.imgspa.and_WifiChange.main$WaitForLayout.run(main.java:74)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:153)
    at android.app.ActivityThread.main(ActivityThread.java:5086)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
    at dalvik.system.NativeStart.main(Native Method)
java.lang.SecurityException: WifiService: Neither user 10087 nor current process has android.permission.CHANGE_WIFI_STATE.
What's wrong?
 
Last edited:

imgsimonebiliato

Well-Known Member
Licensed User
Add this line to the manifest editor:
B4X:
AddPermission(android.permission.CHANGE_WIFI_STATE)
Thanks Erel,
I noticed that everytime I start the app, it create a new wifi connection every time, how can I prevent it?
Also please, it is possible to create a widget that execute this code?
 

Rick Harris

Well-Known Member
Licensed User
Thanks Erel,
I noticed that everytime I start the app, it create a new wifi connection every time, how can I prevent it?
Also please, it is possible to create a widget that execute this code?
The WiFiConnect library works very well! It is ideal to connect to WPA networks using SSID and password.
Just to comment that Erel's advice in post #16 did not work for me. I had to add the following two lines to the manifest to make WiFiConnect work:
B4X:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
Pity that so often libraries are added without proper example source code.
It took me a while to figure out the correct syntax:

B4X:
    Dim Wifi1 As WiFiConnect
Wifi1.connectToSSID(Wifi1.WIFI_WPA, "[SSID]", "[Password]")
 

jnbarban

Member
Licensed User
Hi everyone, anyone know how to save wifi network ? this code work fine but if you stop Wifi, the network are lost..
 
Top