1. Download the free trial version

Library Wifi Library

Discussion in 'Additional libraries, classes and official updates' started by alwaysbusy, Apr 7, 2011.

  1. alwaysbusy

    alwaysbusy Active Member Licensed User

    Wifi Library 1.2

    This library allows you to get Wifi information.

    [Version 1.1] ABWifi is no longer an Activity object.
    [Version 1.2] Correction in getting the current location
    I think (because of my limited knowledge of java then), there was a bug that made it always jump to LastKnown location.

    There is now a seperate function to get the LastKnown location from the cache.

    One other thing. I thought back then it did GSM triangulation, but it does not. This is not done by Wifi, but by cell tower location and ergo does not belong in this library.

    Functions and objects:

    ABLoadWifi() as String

    Loads the Wifi information.

    Example:
    Code:
    Sub Process_Globals
       
    Dim myWifi As ABWifi
    End Sub

    Sub Globals
        
    Dim Button1 As Button
    End Sub

    Sub Button1_Click
        
    Dim r As Boolean
        
    Dim a As Int
        r = myWifi.ABLoadWifi()
        
    If r = True Then
           
    Msgbox (myWifi.ABGetCurrentWifiInfo().SSID, "")
           
    For a = 0 To myWifi.ABNumberOfAvailableWifiNetworks - 1
             
    Msgbox (myWifi.ABGetWifiNetwork(a).SSID & " " & myWifi.ABGetWifiNetwork(a).Level, "")
           
    Next
        
    Else
           
    Msgbox(myWifi.ABWifiLastError,""
        
    End If
    End Sub
    ABWifiInfo Object

    Returns an ABWifiInfo Object.

    Properties:
    (Some properties are only filled for the connected Wifi, see ABGetCurrentWifiInfo())

    BSSID (String)
    MacAddress (String)
    NetworkId (Integer)
    Rssi (Integer)
    SSID (String)
    HiddenSSID (Boolean)
    FullString (String)
    Level (Integer)
    IsConnected (Boolean)
    Frequency (Integer)
    Capabilities (String)
    LinkSpeed (Integer)
    State (String)

    ABGetCurrentWifiInfo() AS ABwifiInfo

    Returns the current connected ABWifiInfo after using the ABLoadWifi() function.

    See ABLoadWifi() for an example.

    ABGetWifiNetwork(Index as integer) As ABWifiInfo

    Returns an ABWifiInfo object that are found by using the ABLoadWifi() function.

    See ABLoadWifi() for an example.

    ABNumberOfAvailableWifiNetworks() as Integer

    Returns the number of available Wifi Networks

    ABGetLocation(eventName as String) As Boolean

    Gets the current location of the device by using the GPS or Wifi signal.

    Example:

    Code:
    Sub Process_Globals
       
    Dim myWifi As ABWifi
    End Sub

    Sub Globals
       
    Dim Button1 As Button
    End Sub

    Sub Button1_Click
       
    Dim r As Boolean
       r = myWifi.ABGetLocation(
    "myWifi")   
       
    If r = False Then
          
    Msgbox (myWifi.ABWifiLastError(), "")
       
    End If
    End Sub

    Sub myWifi_FoundLocation ()
       
    Dim ResLocation As ABFoundLocation
       resLocation = myWifi.ABGetFoundLocation()
       
    Msgbox ("" & ResLocation.Latitude & " - " & ResLocation.Longitude, "")
    End Sub
    ABGetLastKnownLocation(eventName as String) As Boolean

    Gets the last known location of the device by looking in the cache

    Example:

    Code:
    Sub Process_Globals
       
    Dim myWifi As ABWifi
    End Sub

    Sub Globals
       
    Dim Button1 As Button
    End Sub

    Sub Button1_Click
       
    Dim r As Boolean
       r = myWifi.ABGetLastKnownLocation(
    "myWifi")   
       
    If r = False Then
          
    Msgbox (myWifi.ABWifiLastError(), "")
       
    End If
    End Sub

    Sub myWifi_FoundLocation ()
       
    Dim ResLocation As ABFoundLocation
       resLocation = myWifi.ABGetFoundLocation()
       
    Msgbox ("" & ResLocation.Latitude & " - " & ResLocation.Longitude, "")
    End Sub


    ABGetFoundLocation() AS ABFoundLocation

    returns the current ABFoundLocation by using the ABGetLocation() and ABGetLastKnownLocation() function.

    see ABGetLocation() for an example.

    ABFoundLocation Object

    Returns an ABFoundLocation object. This contains the Latitude and Longitude found by using the Wifi signal.

    ABWifiLastError() as String

    returns the last error message if any

    Example:

    Code:
    If myWifi.ABWifiLastError <> "" Then
       
    Msgbox(myWifi.ABWifiLastError,"")
    Else
       
    Msgbox("everything ok""")
    End If
     

    Attached Files:

    Last edited: Sep 13, 2011
  2. Erel

    Erel Administrator Staff Member Licensed User

  3. corwin42

    corwin42 Well-Known Member Licensed User

    Great, thank you very much.

    Can you please send Cableguy a PM with all your libraries so he can include them in his Library Version Listing Thread?
     
  4. XverhelstX

    XverhelstX Well-Known Member Licensed User

    Very nice AlwaysBusy!

    I will definately use it!

    Thanks a lot!

    XverhelstX
     
  5. Inman

    Inman Well-Known Member Licensed User

    Great library. Makes the job a lot easier.

    One question. Does ABGetLocation work if the user is not connected to wifi, just the cell tower alone?
     
  6. alwaysbusy

    alwaysbusy Active Member Licensed User

    Yes it should. But be aware this is also the least reliable way.
     
  7. moster67

    moster67 Well-Known Member Licensed User

    Thank you.
     
  8. splatt

    splatt Active Member Licensed User

    :sign0188:

    Great Library!
     
  9. TOB

    TOB Member Licensed User

    JUST what I needed - Thanks so much! :sign0098:
     
  10. redrocks

    redrocks Member Licensed User

    Good job.Thanks a lot.:sign0089:
     
  11. alexb

    alexb Member Licensed User

    Great library but I see some strange results

    :sign0085:

    Hi Alwaysbusy, thanks for this great library. I am new to this forum and I have started only recently to work with B4A but I wanted to try your library right away. ANYWAY, GREETINGS TO EVERYBODY HERE IN THE FORUM!

    Right now I am most interested to get the location info thanks to your library into the "FindMe" app posted here on the user app forum as well. So, I have put your sample code in a small test app first and I was quite surprised that the VERY SAME TEST APP produces different results on different Android devices: on an Archos 70 it produces the right values for long/lat and there is no error message whatsoever. But on an Archos 101 the myWifi.ABGetLocation produces always "FALSE" and there is no error message (empty string). Other Wifi information works though. I checked the settings option "Use Wireless networks for location determination..." but it is checked. (Btw: if uncheck you get "No Provider Found" as error message. Could you programmatically verify and check/enable this option with an library update?).

    Q1: Do you have any idea what is going on here? Why do I see different behaviour and why do I have an empty error string on the non-working device. Both Archos devices are practically identical apart from the different screen size, they are both running on the same wireless network and are connected to Internet. There is no GPS onboard which might interfere.

    Q2: If I switch on the "good" device (A70) the 'Airplane mode' on I would expect that I cannot retrieve anymore information on network and location. Instead I still get the information - even after having killed/restarted my test app. Only after a reboot of Android I get correct info 'no network'.

    Q3: Does this LIB uses anything (a service, website etc.) from GOOGLE or does it only work on Google authorized SmartPhones??? I ask because on the Archos 70 tablet (not a phone), I have the Android Market installed and running. The Google Map application works there too but on the bigger Archos 101 I have nothing from Google...

    Greetings!

    alex
     

    Attached Files:

    Last edited: Apr 14, 2011
  12. XverhelstX

    XverhelstX Well-Known Member Licensed User

    I always have a No Provider Found message?
    How does this come?

    XverhelstX
     
  13. alwaysbusy

    alwaysbusy Active Member Licensed User

    Sorry for the delay in my answer.

    The lib needs GPS or NETWORK (Network is Wifi or GSM). If none is available you'll get 'No provider Found'.

    I'll check them by doing this:

    Code:
    lm = (LocationManager) ba.context.getSystemService("location");
            //exceptions will be thrown 
    if provider is not permitted.
            
    try{
              gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
               }
    catch(Exception ex){
                  //System.out.println(
    "error gps!");
               }
            
    try{
            network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
               }
    catch(Exception ex){
                  //System.out.println(
    "error netwerk!");
               }

            //don
    't start listeners if no provider is enabled
            if(!gps_enabled && !network_enabled)
            {
               lastError=
    "No Provider Found"
               
    return false;
            }
    to get the location, i'll use:

    Code:
    if(gps_enabled)
                gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if(network_enabled)
                net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    According to some info I found on the net, android does keep track of your last location and if no way is found to update your location, it returns this last location.
    This last location is reset by a reboot.

    I do think google is used in some way by calling this getLastKnownLocation() function so this may explain why Archos has problem with it.

    Remember you have to see this as a last way to get your location. First always try the GPS lib to get an accurate location. I've got reasonable succes using it, but sometimes it is way off (like 500m).
    I'm guessing this is the way Google maps finds your location if no gps is found. I'm getting similar results anyway.
     
    Last edited: Apr 19, 2011
  14. mistermentality

    mistermentality Active Member Licensed User

    Just wanted to say thank you for the library :)

    I mentioned a problem I was having with it, because it is an activity object, in a different thread and it was pointed out that the problem was because

    I have found a way around it for the one feature I needed to access from the library but thought I would mention it here as I am sure being able to use the library in a service would make it even more useful to more people.

    Dave
     
    TedDog likes this.
  15. alwaysbusy

    alwaysbusy Active Member Licensed User

    :sign0161:

    Woops, I had taken the framework of another library i've written and forgot to remove the Activity part. Thanks to mistermentality for pointing that out :)

    The library is now updated to 1.1 in the first post.
     
  16. mistermentality

    mistermentality Active Member Licensed User

    Thank you, I can confirm that it works in a service now as well as an activity :)

    Dave
     
  17. CharlesR

    CharlesR Member Licensed User

    Is there anyway to convert the MacAdress string which is returned by the library to a decimal number please?
     
  18. NeoTechni

    NeoTechni Well-Known Member Licensed User

    If wifi is enabled but not connected, I get a null pointer on initialize
     
    Last edited: Jun 6, 2011
  19. NeoTechni

    NeoTechni Well-Known Member Licensed User

    Can you give an example?
     
  20. Brad

    Brad Active Member Licensed User

    What accuracy do you get? with my moto droid it's been off about 1 mile.
     

Share This Page

Loading...