B4A Library Wifi Manager Library

Discussion in 'Additional libraries, classes and official updates' started by joehil, Apr 22, 2012.

  1. joehil

    joehil Member Licensed User

    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
     

    Attached Files:

    Last edited: May 5, 2012
  2. Erel

    Erel Administrator Staff Member Licensed User

    Thank you for sharing this library. I've created a wiki account for you. If possible please add this library to the list:
    Libraries - Basic4android Wiki
     
  3. myriaddev

    myriaddev Active Member Licensed 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
     
  4. mc73

    mc73 Well-Known Member Licensed 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'.
     
  5. joehil

    joehil Member Licensed 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:

    Code:
    '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: May 6, 2012
  6. joehil

    joehil Member Licensed 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
     
  7. mc73

    mc73 Well-Known Member Licensed User

    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?
     
  8. joehil

    joehil Member Licensed 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
     
  9. mc73

    mc73 Well-Known Member Licensed User

    Perhaps it works this way, I will try it, thank you!
     
  10. myriaddev

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

    myriaddev Active Member Licensed User

    Further testing

    Seems to work very well! Thanks JoeHil.
    Jerry
     
  12. rfresh

    rfresh Well-Known Member Licensed 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...
     
  13. joehil

    joehil Member Licensed User

    @rfresh:

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

    With kindest regards
    Joerg
     
  14. rfresh

    rfresh Well-Known Member Licensed 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.
     
  15. mc73

    mc73 Well-Known Member Licensed User

    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.
     
  16. joehil

    joehil Member Licensed 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
     
  17. joehil

    joehil Member Licensed 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
     
  18. JorgeMC

    JorgeMC Member Licensed User

    Thank you !
     
  19. rfresh

    rfresh Well-Known Member Licensed User

    Thank you so much...I will try them out this evening...!!!
     
  20. mc73

    mc73 Well-Known Member Licensed User

    Very-very useful, in my opinion of course. Thank you, I will try them and give you my feedback apart from my thanks!
     
Loading...