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,385
  • ABWifi2.zip
    491.3 KB · Views: 699
  • ABWifi2.01.zip
    491 KB · Views: 1,827
Last edited:

Anawratha

Member
Licensed User
Longtime User
Hi, alwaysbusy (...and all others...)!
I tried to implement your lib [1.2] (nice one!!) into my project. Seems to work ok, but:
I can't use your descibed function "ABGetLastKnownLocation" - it's not present. There is only the function "GetLastKnownLocation". If I take this one, I'am just getting a Java-Error: java.lang.NullPointer.Exception.
I'm using B4A 1.5 .
Can anybody help me, please?
:sign0085:
 

droman

Member
Licensed User
Longtime User
Hi, alwaysbusy (...and all others...)!
I tried to implement your lib [1.2] (nice one!!) into my project. Seems to work ok, but:
I can't use your descibed function "ABGetLastKnownLocation" - it's not present. There is only the function "GetLastKnownLocation". If I take this one, I'am just getting a Java-Error: java.lang.NullPointer.Exception.
I'm using B4A 1.5 .
Can anybody help me, please?
:sign0085:

I have the same problem :BangHead:
 

bluedude

Well-Known Member
Licensed User
Longtime User
alwaysbusy,

Just a small question. Does the Location stuff use pure CellTower or does it also make a post to a hidden Google Api for looking up the celltower?

Cheers,
 

Inman

Well-Known Member
Licensed User
Longtime User
Hi,
the lib seems to work fine - the only issue I have is the IsConnected property. It does not seem to work - always reports false (using SE Experia X10).

The only solution thus far is to look at the State property (UNINITIALIZED / COMPLETED) strings to figure out if the device is connected to wifi or not.

Maybe I am using the lib wrong???

Anyway, thanx for a great lib!!!!

Regards,
M. le Roux

Just to give a heads up that this logic is not working in Android 4.0 (Ice Cream Sandwich). It always says WiFi state is COMPLETED even when on 3G. The solution using Phone library, mentioned in the thread below, works fine though.

http://www.basic4ppc.com/forum/basic4android-updates-questions/11904-detecting-wifi-connection.html
 

Feri

Member
Licensed User
Longtime User
Null pointer exception

I tried this library with the example, but at
B4X:
Msgbox ("" & ResLocation.Latitude & " - " & ResLocation.Longitude, "")
line I get Java null pointer exception. Another interesting thing that I cannot enable Wifi in emulator, I got "Error" even if I enable data connection with F8. But I should not get null pointer exception even with disabled Wifi, right? (As there is an error handling routine.)
 

Feri

Member
Licensed User
Longtime User
I tried this library with the example, but at
B4X:
Msgbox ("" & ResLocation.Latitude & " - " & ResLocation.Longitude, "")
line I get Java null pointer exception. Another interesting thing that I cannot enable Wifi in emulator, I got "Error" even if I enable data connection with F8. But I should not get null pointer exception even with disabled Wifi, right? (As there is an error handling routine.)

Nobody experienced this problem?
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
How can you detect if wifi is actually on?
I don't want to use the hotspot count = 0 cause that can introduce false negatives.

I dont want to use the wifitoggle lib cause the blackberry playbook doesnt allow it
I dont want to use the phone library cause that adds another permission and people are paranoid about that

B4X:
Sub isWIFI_enabled As Boolean 
   Dim temp As ABWifi 
   Return temp.ABLoadWifi 
End Sub

Wont return false if wifi isnt on

EDIT: Turns out the phone lib doesnt add a permission. It worked fine
 
Last edited:

joolzg

Member
Licensed User
Longtime User
No WIFI but is there a lib that allows access to the WIRED interface that you find on these cheap chinese boxes?

I need to check if WIRED or WIFI is available, and then get the MAC of the one that is connected?

joolz
 
Last edited:

alexposta2

Member
Licensed User
Longtime User
icecream issue

Hi all,
I've a tablet with 4.0.3 android version.
With this os the localization with abwifi don't seem to work

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

This sub is not invoked but
B4X:
myWifi.ABLoadWifi()
   reswf = myWifi.ABGetLocation("myWifi")

reswf return true

same code on android 3.2 work fine
any idea?


b4a 1.9

thank you
Alessio
 

CharlesIPTI

Active Member
Licensed User
Longtime User
Fail

Does anyone see a glaring error here?? Because Assuredly ONE of my wifi connections IS connected but the results of this little method all say no connection is actually connected:

Sub WiFiTwo

Dim r As Boolean
Dim a As Int
Dim nFo As ABWifiInfo
r = myWifi.ABLoadWifi()

nFo = myWifi.ABGetCurrentWifiInfo()
Msgbox("Connected 0 :: " & nFo.IsConnected, "")

If r = True Then
Msgbox (myWifi.ABGetCurrentWifiInfo().SSID, "")

For a = 0 To myWifi.ABNumberOfAvailableWifiNetworks - 1

Msgbox(myWifi.ABGetWifiNetwork(a).SSID & " " &
myWifi.ABGetWifiNetwork(a).Level & " " &
myWifi.ABGetWifiNetwork(a).IsConnected, "")

Next
Else
Msgbox(myWifi.ABWifiLastError,"")
End If
End Sub
 

CharlesIPTI

Active Member
Licensed User
Longtime User
Is Connected Broken ??

IS this more appropriate then ??? I had hoped I could check the Is Connected..
PHP:
    Sub WiFiThree 

      Dim r As Boolean
                 Dim a As Int      
      Dim nFo As ABWifiInfo             
      
    r = myWifi.ABLoadWifi()            
      nFo = myWifi.ABGetCurrentWifiInfo()                  
      Msgbox(nFo, "")
      
    If nFo <> Null Then
        Msgbox ("Apparently it is connected", "PLPLP")
        
    Else
       Msgbox ("Apparently NO connection is active ", "PLPLP")
    End If
End Sub
 

flyingbag

Member
Licensed User
Longtime User
Excellent Job AlwaysBusy - great library ! This was exactly what i wanted :) Thanks so much!
 

CharlesIPTI

Active Member
Licensed User
Longtime User
Three Months ago WiFi Connected

Soooo I guess I should FRESH post the question again Maybe ??

Assuredly ONE of my wifi connections IS connected but the results of this little method all say no connection is actually connected:

B4X:
Sub WiFiTwo

Dim r As Boolean
Dim a As Int
Dim nFo As ABWifiInfo
r = myWifi.ABLoadWifi()

nFo = myWifi.ABGetCurrentWifiInfo()
Msgbox("Connected 0 :: " & nFo.IsConnected, "")

If r = True Then
Msgbox (myWifi.ABGetCurrentWifiInfo().SSID, "")

For a = 0 To myWifi.ABNumberOfAvailableWifiNetworks - 1

Msgbox(myWifi.ABGetWifiNetwork(a).SSID & " " &
myWifi.ABGetWifiNetwork(a).Level & " " &
myWifi.ABGetWifiNetwork(a).IsConnected, "")

Next
Else
Msgbox(myWifi.ABWifiLastError,"")
End If
End Sub
 

petr4ppc

Well-Known Member
Licensed User
Longtime User
b4ppc 1.8 versus basic4ppc 2.50 and abwifi

Dear friends, please very much I need advice.

I had before a few days basic4ppc version 1.8 and I have try this:

Sub Button1_Click
r = myWifi.ABLoadWifi()

If r = True Then
If myWifi.ABGetCurrentWifiInfo().NetworkId >=0 Then
Msgbox (myWifi.ABGetCurrentWifiInfo().NetworkId ,"wifi info")
Else
Msgbox(myWifi.ABGetCurrentWifiInfo().NetworkId ,"wifi info")
End If
Else
Msgbox (myWifi.ABGetCurrentWifiInfo().NetworkId ,"ABLOADWifi errorl")
end If
End Sub

when I have tested it on AVD on PC which was connected via LAN to internet and with basic4ppc 1.8, then I have got results as in real android device, the same results, it does it mean myWifi.ABGetCurrentWifiInfo().NetworkId = 0

now I have version basic4ppc 2.50 I am using the same script and in AVD I become only result myWifi.ABGetCurrentWifiInfo().NetworkId=-1 . But I have PC connected to internet, everything is the same..script, connectiong to internet, only version and computer is new.

The reason is version of BASIC4PPC?

Because when I got myWifi.ABGetCurrentWifiInfo().NetworkId=-1 in AVD and then I install the same program to real device with wifi, then I have right results, it does it mean: myWifi.ABGetCurrentWifiInfo().NetworkId=0 ...

Why with BASIC4PPC v2.50 in AVD I have not right results and only in real device I have good results, please?

Thank you very much, have a good day
p4ppc

My English is not good, I know :sign0013:
 
Last edited:

GiovanniPolese

Active Member
Licensed User
Longtime User
AbWiFi lib

Hi, everybody. I have two doubts, using ABWiFi library. The first is not a doubt, actually, but just experimental evidence. Infact it seems that it is needed a delay of at least two seconds to detect Position. I mean that while ABLoadWifi answers true at lower time rates, the FoundLocation sub is not fired at rates less that two seconds, more or less. (If I am right, of course). The second is a real doubt: I didn't understand whether the library uses the GPS sensor, while connected to WiFi, or not. In my experiments, it seems that coordinates of Gps and WiFi are nearly always the same. Thanks in advance.
 
Top