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,647
  • ABWifi2.zip
    491.3 KB · Views: 938
  • ABWifi2.01.zip
    491 KB · Views: 2,161
Last edited:

Firpas

Active Member
Licensed User
Longtime User
The "StartWifi" method of ABWifi 2.x fails when wifi is disable.
This is the error:

java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.WIFI_STATE_CHANGED flg=0x4000010 (has extras) } in com.AB.ABWifi.ABWifi$1@42228ef8

at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:782)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5603)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.AB.ABWifi.ABWifi$1.onReceive(ABWifi.java:132)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:772)
... 9 more


And the manifest include this two lines:

AddPermission(android.permission.ACCESS_WIFI_STATE)
AddPermission(android.permission.CHANGE_WIFI_STATE)

Any idea???

Thanks in advance
 

alwaysbusy

Expert
Licensed User
Longtime User
Kind of a weird error. StartWifi should just return false in the lib when _wifi == null (causing the java.lang.NullPointerException error later in the BroadcastReceiver). I'll look into it.

Anyhow the two lines:
AddPermission(android.permission.ACCESS_WIFI_STATE)
AddPermission(android.permission.CHANGE_WIFI_STATE)

Should not be added as they are already added in the library
 

alwaysbusy

Expert
Licensed User
Longtime User
Here is a temporary new version. There is also a new demo project, as there were some changes (extra event). Check that one out first before trying it in your project. This is a hard one to debug as I'm connected via wifi to the device, and it's just that connection that must be disabled to reproduce the problem.

Changes (so far)

Sub Activity_Resume
' start ABWifi
If wifi.StartWifi() Then ' use the boolean to see if it's ready to do a GetNewReadingWifi(), if false, you'll have to wait for the WifiEnabled() event
' Get a reading
wifi.GetNewReadingWifi
End If
End Sub

' new
Sub wifi_WifiEnabled(IsEnabled As Boolean)
If IsEnabled Then
wifi.GetNewReadingWifi
Else
ToastMessageShow("Failed to enable Wifi", True)
End If
End Sub
 

Attachments

  • ABWifi2.02.zip
    359.6 KB · Views: 339

Firpas

Active Member
Licensed User
Longtime User
Once again thanks for your quick response.
I tried the new library and now produces a different error:

** Activity (scanwifi) Create, isFirst = true **
** Activity (scanwifi) Resume **
Get new reading
Enabled: true

java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS flg=0x4000010 } in com.AB.ABWifi.ABWifi$2@4222b850

at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:782)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5603)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.AB.ABWifi.ABWifi$2.onReceive(ABWifi.java:236)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:772)
... 9 more
 

Firpas

Active Member
Licensed User
Longtime User
One question ...
If I start a timer (10 sec) in the "_WifiEnabled" event, and then call "_GetNewReadingWifi", works well.

I hope this comment will help you.

Thanks for your cooperation again.
 

Firpas

Active Member
Licensed User
Longtime User
One suggestion ...
It would be very useful to give the library a pair of methods to connect to a ABWifiReading:

ABWifiConnect (SSID as string) as boolean
ABWifiConnect2 (BSSID as string) as boolean

It's just a suggestion ;)
 
Last edited:

alwaysbusy

Expert
Licensed User
Longtime User
Ok, another version. I'm afraid we are going to to some trial and error because I'm unable to reproduce the problem here on my devices.
Not having to use a timer is the reason I've started rewriting the lib, so using one is not a solution I'm afraid. It appears in the last version (2.02) it was now the var CurrConn that somehow became null just one line after it was checked:

Lib java code:

if (CurrConn != null) {
if (result.BSSID.compareTo(CurrConn.getBSSID) == 0) { <== null pointer exception
reading.IsConnected = true;
}
}

Added some more checks in this one:

if (CurrConn != null) {
String resBSSID = result.BSSID;
if (currBSSID!=null && resBSSID != null) {
if (resBSSID.compareTo(currBSSID) == 0) {
reading.IsConnected = true;
}
}
}

Can you also tell me the device and the Android version you are using?

I'm not going to add any more functionality until that nasty one is solved. Also, I believe connecting to a Wifi is already in another lib?
 

Attachments

  • ABWifiTest2.03.zip
    359.7 KB · Views: 556

Firpas

Active Member
Licensed User
Longtime User
Here is my devices and Androd version:

Tablets

Samsung Galaxy Tab 4 (GT-N8000) - Android 4.4.2
Asus Fonepad 7 (K013) - Android 4.4.2

Smartphones

Samsung Galalaxy S4 - Android 4.4.2
Samsung Galaxy Trend - Android 4.0.4
HTC WildFire - Androd 2.2.1

Now i'm going to test the ne versión 2.03 and i´back.

Best regards
 

Firpas

Active Member
Licensed User
Longtime User
Ok, I tried the new version 2.03 on some of my devices (both tablets and S$) and now works fine.

I still think that adding a method to connect to a specific wifi via SSID or BSSID will be very useful.

Thank you very much for your cooperation. :)
 

ibra939

Active Member
Licensed User
Longtime User
wonderful tool
 

FrankBerra

Active Member
Licensed User
Longtime User
After some headaches using ABWifi V 2.01 i found this bug fix! It works!

Here the results using this codes starting with wifi powered off:
B4X:
wifi.Initialize("wifi")
wifi.StartWifi
wifi.GetNewReadingWifi

With ABW 2.01
Nexus 5 (5.0.1): no problems at all (Wifi Activation is sooo quick!)
Samsung Galaxy S2 (4.1.2): crashes (probably because WiFi activation is so slow compared to Nexus 5 ?!)

With ABW 2.03
No problems on both devices

Thank You!
You should update the original thread: https://www.b4x.com/android/forum/threads/wifi-library.8613/
 

Objects for Android

Member
Licensed User
Longtime User
In the latest reply there is a mention of ABW 2.03 and the first page the version is 2.01
Does the 2.03 version is publicly available ?

Thanks.
 

Objects for Android

Member
Licensed User
Longtime User
Just find in the topic the version 2.03, problem is that under 6.50 of B4A library seams not to work properly (When I try to call : ABLoadWifi - I get : Error description: Unknown member: abloadwifi).

Library is well load and my variables in declare in global correctly. The only member I saw are :
StartWifi
StopWifi
Initialize
GetNewReadingWifi
DisableWifi
EnableWifi

Any idea ?
 
Top