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.
 

bluedude

Well-Known Member
Licensed User
Longtime 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
Longtime 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
Longtime 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

  • WiFiConnect1.1.zip
    2.7 KB · Views: 1,254

jalle007

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

Attachments

  • WiFiConnect1.1.zip
    1.1 KB · Views: 667

suciwulans

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

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
I'm sorry but I'm not familiar with this library. Any code that doesn't require an activity can be implemented in a widget service.

Thanks Erel
 

jnbarban

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

eynysman

New Member
Licensed User
Longtime User
hello

do not speak English, have little programming experience and ask for help

I'm doing a custom wifi network manager

I took example of how to connect on a wifi network and could

to disconnect I'm having problems

I am using the following example


Dim mywifi As VBConnect

Dim Wifi1 As WiFiConnect


Sub WiFiAdd_Click

Wifi1.connectToSSID(Wifi1.WIFI_WPA, “SSID1”, “123456789”)

End Sub


Sub WiFidisconnect_Click

mywifi.RemoveAllSavedNetworks ‘remove all saved networks End Sub


the doubt is, how to disconnect only one specific network, example: "Wifi1. disconnectToSSID ("SSID1") just unplugging "SSID1" network

clean out a Saved on my list of wi-fi network

Hold and thank you in advance

Eynysman Felix Pacheco

Cabo Frio – Rio de Janeiro - Brasil
 
Top