B4A Library Wifi Manager Library

For one of my projects I created a little library to have access to 3 wifi manager functions:

Inquire the state of wifi (switched on or off), switch wifi on or off and start a wifi scan.

Here is a sample how you can use it:

Dim jhw As JhWifi

' Initialize wifi
jhw.Initialize()

' Is wifi enabled, if not enable it
If jhw.Enabled = False Then
jhw.Enabled = True
End If
' Start wifi scan
jhw.startscan()
' Disable wifi
jhw.Enabled = False

Please use it for whatever you need it.

With kindest regards
Joerg
 

Attachments

  • Jhwifi.1.2.zip
    4.2 KB · Views: 2,093
Last edited:

myriaddev

Active Member
Licensed User
Longtime User
Nice Lib!

I wrote a simple app using your lib.
My question JoeHil, your startscan() function,
will it cause the Android to switch to the
stronger SSID if known to Android ?

Thanks, Jerry
 

mc73

Well-Known Member
Licensed User
Longtime User
Very good!
I'm wondering whether it would be possible to add a feature for selecting a predefined network and/or specify ip/gate/dns at 'advanced settings'.
 

joehil

Member
Licensed User
Longtime User
Hi Jerry,

no, startscan() does not automatically connect you to the strongest network. But I added 3 commands that will help to do that and posted a new lib. Here is my sample code:

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim jhw As JhWifi
   Dim timer1 As Timer
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
   jhw.Initialize()
   jhw.startscan()
   ' Scan takes some time, so wait a minute
   Log("Scan started")
   timer1.Initialize("timer1", 60000)
   timer1.Enabled = True
End Sub

Sub Activity_Resume

End Sub

Sub timer1_Tick
   Dim n As Int
   ' Find strongest known network
   n=jhw.StrongestKnownNetwork
   Log("Strongest Network: " & n)
   ' Connect to network
   jhw.enableNetwork(n, True)
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

The command StrongestKnownNetwork finds the strongest network that you configured.

The command enableNetwork connects to the network whose ID you give as a parameter.

There is one more command that I did not use in the sample:
findConfiguredNetwork(SSID)
It will give you the netWorkID of a network, if you give it the SSID of the network.

I hope this helps!

With kindest regards
Joerg
 
Last edited:

joehil

Member
Licensed User
Longtime User
Hi mc73,

use my new library (Version 1.1) and try this code:

Dim jhw As JhWifi
Dim n As Int
jhw.Initialize()
n=jhw.findConfiguredNetwork("yourchoice")
jhw.enableNetwork(n, True)

It is supposed to connect you to the network whose SSID is yourchoice.

Good Luck!

With kindest regards
Joerg
 

mc73

Well-Known Member
Licensed User
Longtime User
Hi mc73,

use my new library (Version 1.1) and try this code:



It is supposed to connect you to the network whose SSID is yourchoice.

Good Luck!

With kindest regards
Joerg

Hello and thank you!
Unfortunately it didn't work for me, since the network I use is password protected. Any idea on how to do this? (I mean, since I have network's password). Furthemore, any idea on editing and activating static IP?
 

joehil

Member
Licensed User
Longtime User
Hi mc73,

sorry my sample code does not work for you!

I also use networks with a password and it works. I do it this way:

1. I connect to all networks that I want to use manually once. I will have to enter the password then. The password and all other parameters are stored this way.

2. Then I use the code I gave you as a sample to connect to one special network. I do not need to reenter the password then.

If this does not work for you, then I might not fully understand your problem.

With kindest regards
Joerg
 

mc73

Well-Known Member
Licensed User
Longtime User
Perhaps it works this way, I will try it, thank you!
 

myriaddev

Active Member
Licensed User
Longtime User
Wow! Thanks!

I tested your sample code v1.1 at home with one
network with known password and several unknown
networks with unknown passwords, it worked GREAT!
Tomorrow, I will test your lib v1.1 with 5 different
connected known networks with known passwords,
and several unknown networks with unknown
passwords (I do not want to connect to them).
I will let you know.

When you are in Denver, Colorado, USA, contact me,
and I will buy you a dinner. Thank you, Jerry
 

rfresh

Well-Known Member
Licensed User
Longtime User
@joehil

Thanks for the great library. I was wondering if you could add a command to allow us to control the wifi sleep policy? I have a need to be able to set it to "never sleep". Thanks...
 

joehil

Member
Licensed User
Longtime User
@rfresh:

I am not aware of any wifi sleep policies. What do you mean by wifi sleep?

With kindest regards
Joerg
 

rfresh

Well-Known Member
Licensed User
Longtime User
On my Droid2 under settings->WiFi if I press the menu key there is an Advanced menu selection. There is the WiFi sleep policy: (1) when screen turns off (2) never when plugged in (3) Never

I'd like to be able to set it to one of those 3 settings via your library if possible.
 

mc73

Well-Known Member
Licensed User
Longtime User
On my Droid2 under settings->WiFi if I press the menu key there is an Advanced menu selection. There is the WiFi sleep policy: (1) when screen turns off (2) never when plugged in (3) Never

I'd like to be able to set it to one of those 3 settings via your library if possible.
This would be nice, for another reason too. I observed that in some (more or less) android devices, this setting is missing from the menu screen.
 

joehil

Member
Licensed User
Longtime User
Ok, now I see what you mean. I do not have these settings on my phone.

Will add those commands. Please give me some days...

With kindest regards
Joerg
 

joehil

Member
Licensed User
Longtime User
I have added the following variables in version 1.2:

Wifi_Sleep_Policy
Wifi_Use_Static_IP
Wifi_Static_IP
Wifi_Static_DNS1
Wifi_Static_DNS2
Wifi_Static_Gateway
Wifi_Static_Network

You can get and set those variables.

Please tell me whether this will suit your needs.

With kindest regards
Joerg
 

JorgeMC

Member
Licensed User
Longtime User
I have added the following variables in version 1.2:

Wifi_Sleep_Policy
Wifi_Use_Static_IP
Wifi_Static_IP
Wifi_Static_DNS1
Wifi_Static_DNS2
Wifi_Static_Gateway
Wifi_Static_Network

You can get and set those variables.

Please tell me whether this will suit your needs.

With kindest regards
Joerg

Thank you !
 

rfresh

Well-Known Member
Licensed User
Longtime User
Thank you so much...I will try them out this evening...!!!
 

mc73

Well-Known Member
Licensed User
Longtime User
Very-very useful, in my opinion of course. Thank you, I will try them and give you my feedback apart from my thanks!
 
Top