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:

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
Thanks, for the library!

Please, I need to get the MAC address of my tablet, but if I set this

B4X:
dim edMac as edittext
dim ab as ABWifiInfo

edMac.Text = AB.MacAddress

don't work. Why?
 
Last edited:

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
The logs are empty? If you are using B4A-Bridge with Android 4.1+ devices then the logs will only appear in debug mode.

Hi,
it's empty the Log(AB.MacAddress)
 

jefflynn1974

Member
Licensed User
Longtime User
Hi!

I have a question. I would like to use this libary to get location by wifi wihout using GPS. Is it possible somehow? I would like to avoid turn the GPS on.

Thanks!
 

appie21

Active Member
Licensed User
Longtime User
Hi
I have a question about your tutorial
if I do not run code line 'if I rem this line the program not work why? Msgbox (myWifi.ABGetCurrentWifiInfo().SSID, "")

the button click not work correct

What do I wrong


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
      'if I rem this line the program not work why?  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
 

peacemaker

Expert
Licensed User
Longtime User
myWifi.ABGetWifiNetwork(id).MacAddress is always empty string. Under Android 4.2.1 and 4.0.3.
Is it known ?
It's for others WiFi APs around, that are all password protected.

Bug ?
 

Avansys

Member
Licensed User
Longtime User
Can this library return the info of each network saved in the device?
 

peacemaker

Expert
Licensed User
Longtime User
I could collect max these:
B4X:
current_wifi_ap:"Pmaker.biz"
newtwork=GPRS
CID=1636
LAC=6315
network cells (ID,signal_strength): (ArrayList) [1631,13, 2613,9, 2614,8, 1654,6, 1634,6, 2618,5]
APN=Megafon internet, MCC=250, MNC=02
datastate=DISCONNECTED
SIM card line number=
SIM card serial number=897010270107082139ff
IP=192.168.1.33
WiFi IP=192.168.1.33
WiFi networks around:4
WiFi points (ID, signal_strength)Pmaker.biz, -61
MacAddress=
WiFi points (ID, signal_strength)NETGEAR, -62
MacAddress=
WiFi points (ID, signal_strength)admin, -62
MacAddress=
WiFi points (ID, signal_strength)DIR-300NRU, -91
MacAddress=

MACs are anyway empty.
 

Steve Miller

Active Member
Licensed User
Longtime User
The IsConnected connected property doesn't work. I'm connected to my home wifi network (writing this post from my tablet now, so obviously I'm connected to my network, which is connected to the internet), but the wifi library IsConnected property is displaying False.

I've seen others stating the same issue. Is this ever going to be fixed, or do we need to find another method to determine if we are connected to wifi?
I need to be able to determine if I am connected to wifi, what the SSID is of the connection is, and what the strength of the signal is.
 

Peter Simpson

Expert
Licensed User
Longtime User
Hello all,
I had the exact same issue as post #67, I also noticed the same thing. Basically it looks like the For Loop isn't getting enough time to find all the WiFi networks available.

My solution was to add a timer that waited for a couple of seconds before executing the For Loop. My timer solution works perfectly but it's not really and correct solution to the issue mentioned in post #67 :(
 

Dadeda

Member
Licensed User
Longtime User
I'm trying to use this lib to get my location (lat/long) from wifi and not gps but it simply does not work, i always get a null value from ABgetFoundLocation.
I got wifi working for other subs, so i know that it is connected, can anyone help me out? i really need a way to get lat/long coordinates from wifi without using gps.
 

antonomase

Active Member
Licensed User
Longtime User
Like other people, the flag Isconnected does not work.

How can we test if the device is or not connected to a wifi router ?

I try the State value, but it is always COMPLETED event if the wifi is desactivated on the device.

Is it reliable to use LinkSpeed or Frequency to test if the device is or not connected to a router ?

Thanks
 
Last edited:

woniol

Active Member
Licensed User
Longtime User
Hi,
I get this with ABWifi 2.0 example (real device with Android 4.1.2):
B4X:
Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:com.ab.abwifidemo
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Get new reading
java.lang.NoSuchFieldError: android.net.wifi.ScanResult.timestamp
    at com.AB.ABWifi.ABWifi$2.onReceive(ABWifi.java:171)
    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:755)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4867)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
    at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Resume **
 

woniol

Active Member
Licensed User
Longtime User
I use API 21:
C:\Program Files (x86)\Android\android-sdk\platforms\android-21\android.jar
 
Top