Android Question please recommend a stable library to reconnect WiFi

oceanwanderlust

Member
Licensed User
Longtime User
I have an unattended Kiosk which occasionally looses its WiFi connection, so I need to force it back onto the network. I've tried MLwifi and ABwifi without success. I figured that before I tried yet another library, such as Wifi Connect, I'd ask...

What's the best/recommended WiFi library for enabling/disabling Wifi, and connecting to an already saved network?

Wifi Connect - looks like a possibility, and recent activity, but not officially supported and thread seems sparse:
https://www.b4x.com/android/forum/threads/wifi-connect-library-wep-wpa-open.27254/

MLwifi - currently even the example does not work:
https://www.b4x.com/android/forum/threads/simple-wifi-library.38601/

ABwifi - does not allow me to connect:
https://www.b4x.com/android/forum/threads/wifi-library.8613/

Edit: clarified title of thread.
 
Last edited:

oceanwanderlust

Member
Licensed User
Longtime User
"occasionally" is approximately every 10 days or so, at best. I certainly agree, that it is caused by distance / interference. When the issue occurs I have to send a tech out to the device to simply turn wifi off and then turn it back on. They don't even have to reselect the network... I don't understand why Android just gives up reconnecting to the preferred network, but figure I should just be able to force it programmatically if I find the right library.
 
Upvote 0

oceanwanderlust

Member
Licensed User
Longtime User
MLWiFi and ABWifi showed that the IP address had resorted to 0.0.0.0 but also that WiFi was not connected, so I don't think that simply renewing the DHCP lease will help. I'm willing to try, but think that's the wrong layer.
 
Upvote 0

oceanwanderlust

Member
Licensed User
Longtime User
It's a dynamic (unmanaged) DHCP. I'd expect to see 0.0.0.0 if a device does not have a DHCP lease or WiFi connection, though internally it may be null. It's definitely more reasonable than Windows random 169 link local address.
 
Upvote 0

giga

Well-Known Member
Licensed User
Longtime User
Agree, 0.0.0.0 would be expected if it's not connected to the wifi and the IP is lost via DHCP. Windows 169.... are ridiculous because people get confused and think its valid.
If you check the lease expiration time on the router and/or set the kiosk to a static IP on the high side to avoid conflicts. Does it maintain a connection?

Also ping the kiosk from another computer and see the response. drops, high rates etc...
 
Upvote 0

oceanwanderlust

Member
Licensed User
Longtime User
I don't control or care about the network. My device *should* be extremely resilient to network or server downtime. Whenever we check manually, it is always possible to immediately reconnect to the network, so it should be possible to do it programmatically. ABwifi is not powerful enough, MLwifi is broken, and I didn't feel like wasting my time with yet another library without asking someone to point me at one which works. I just need to be able to turn off wifi, turn on wifi, and (re)connect to a saved network,.


btw, my kiosk is extremely low bandwidth; one webpage with one button.
 
Upvote 0

oceanwanderlust

Member
Licensed User
Longtime User
I wrote an example app using Toggle, but it simply turned off and on the wifi, and it never reconnected to the saved network. grrrrrrrrrr.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Documentation for the Wifi API is here, which you can access using JavaObject.

For instance, this code should reconnect to the currently active access point if it is disconnected.

B4X:
    Dim Context As JavaObject
    Context.InitializeContext
    Dim WIFI As JavaObject = Context.RunMethod("getSystemService",Array("wifi"))
    WIFI.RunMethod("reconnect",Null)

This may be all you need to do. I can't test it as I don't have your set-up. But it would be worth a try.

You could perhaps run this once a day, which would do nothing if it's already connected.

You also need to add

B4X:
AddPermission("android.permission.CHANGE_WIFI_STATE")

in the Manifest editor.
 
Upvote 0

oceanwanderlust

Member
Licensed User
Longtime User
Steve!!!! Your Java example above works! 5sec after I ran the toggle library, I ran your code and it correctly reconnected to WiFi.

Since I was disconnected, I'm unsure what "currently active access point" is, but in my test, it got the correct one. It looks like it's time to step back into the world of Java instead of completely relying on Basic.

THANKYOU!

Edit: 'clink, clink' in the donation jar
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You're welcome.

The documentation is vague on what is the current active access point, I read it as the one the device was last connected to. I don't know what would happen if you run it on a device that has not been connected, or the connection has been manually terminated. Hopefully it will just do nothing.

The Google API's are huge and most users won't need to venture into them. Erel has done a great job of including most the relevant parts into the B4x core libraries. There are times when some of us need to venture deeper into the available functions, which is the reason Erel gave us JavaObject.

User developed library's provide more of the non core B4x functionality, sometimes they offer far more than is required to accomplish a simple task, and in the process of making the library light and accessible to all, some methods are omitted.

If you know Java, this is a great way to cherry pick some of the methods from API's without having to create a whole wrapper library. Even if you don't know Java, there is a minimal learning curve to be able to use a few methods effectively.
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
Steve!!!! Your Java example above works! 5sec after I ran the toggle library, I ran your code and it correctly reconnected to WiFi.

I have (had) this same issue...

My devices are mobile (in vehicles). Sometimes, when they drive into the area where the AP exists, they would (automatically) attempt to connect with AP. If they were on the fringe, it may or may not connect successfully. When not (authentication failed), the OS (or wifi module) would NOT try again! So, you bring up connections in setup, touch the failed connection and BOOM - Connected! (silly android - not making a second (or multiple) attempts...)
It will connect to the last connected AP (SSID). In my case - there is only 1.

With JavaObject code in place, just before I need to send data (at shift end), I call it to ensure there is a connection. The stub test app works fine, we shall see about the production app - which I expect will work fine as well.

No need for the (outdated and unsupported) Toggle lib - it doesn't do anything here... (whatever happened to X_X - must have found a new calling?)..

Thanks @stevel05 for pointing this out. Plagued me for months...
 
Upvote 0
Top