B4A Library Wifi Library

UPDATE: ABWifi 2.x

Note: ABWifi 2.x is NOT compatible with ABWifi 1.x. ABWifi 1.x was one of the first libraries I wrote for B4A. It was reported that there were some problems with timing etc. It did also some stuff that I needed for a personal project (like location finding), but there are way better libraries for that available now.

So I decided to rewrite ABWifi, now using the correct way to read the wifis around you, and ONLY doing that.

Functions:

Initialize("eventName")
Initialize ABWifi

StartWifi

Start ABWifi. Use GetNewReadingWifi() to get one reading
The event FoundReadings() will be raised containing the current Wifi infos
If WIFI was not enabled on the device, it will be enabled now.

GetNewReadingWifi
Get a new reading. Only one reading will be done, so you have to call it again
in the FoundReadings() event if you want continious readings.

StopWifi

Stop ABWifi
If WIFI was not enabled on the device, it will be disabled now.

Events:

FoundReadings(readings As List)
A list of ABWifiReading objects with the WIFIs around you

Objects:


ABWifiReading
One reading containing:
TimeStamp (Removed in 2.01, still in 2.0)
BSSID
Level
Frequency
SSID
Capabilities
IsConnected
FullString
IsBestSignal

Example:
Is included in the zip file with the library files


---------------------------------------------------------------------------------------------
OLD: 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:
B4X:
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:

B4X:
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:

B4X:
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:

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

Attachments

  • ABWifi 1.2.zip
    10.9 KB · Views: 3,646
  • ABWifi2.zip
    491.3 KB · Views: 937
  • ABWifi2.01.zip
    491 KB · Views: 2,160
Last edited:

corwin42

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

Inman

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

alexb

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

Attachments

  • Wifi&SoftGPS_TestApp.zip
    5.2 KB · Views: 755
Last edited:

alwaysbusy

Expert
Licensed User
Longtime 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:

B4X:
   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:

B4X:
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:

mistermentality

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

the AbWiFi object in the WiFi library is declared as an Activity object. This is not necessary as it does not interact with UI elements but this prevents its use in a Service. It needs recompilation by the author as a non-Activity object.

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
 

mistermentality

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

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

Dave
 

CharlesR

Member
Licensed User
Longtime User
Is there anyway to convert the MacAdress string which is returned by the library to a decimal number please?
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
If wifi is enabled but not connected, I get a null pointer on initialize
 
Last edited:

Brad

Active Member
Licensed User
Longtime User
What accuracy do you get? with my moto droid it's been off about 1 mile.
 
Top