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:

francoisg

Active Member
Licensed User
Longtime User
IsConnected property

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
 

bluedude

Well-Known Member
Licensed User
Longtime User
I think the location stuff isn't working properly. I have WIFI on on a Nexus one but GSM off. It should give me a location but it tells me "no provider found".

I guess it needs both WIFI and GSM?

Cheers,
 

rbsoft

Active Member
Licensed User
Longtime User
Thank you, Alain, for this excellent library.

Tried the location stuff on my Galaxy Tab und HTC Desire. With Wifi I get a very good position with an accuracy of 80 meters, without GSM accuracy is 800 m.

Which brings me to my question about the basics!
Where does a Wifi connection get accurate coordinates from? Could somebody enlighten me?
 

corwin42

Expert
Licensed User
Longtime User
Which brings me to my question about the basics!
Where does a Wifi connection get accurate coordinates from? Could somebody enlighten me?

Google collects informations about SSIDs and its GPS positions in many ways. One is Google StreetView cars. They also collected SSIDs and their positions. I think another way is Google Maps which can report SSIDs and exact GPS positions to Google.
 

rbsoft

Active Member
Licensed User
Longtime User
I did some testing yesterday while driving from Bochum to Essen on the A40 (Germany). Got very interesting results.

Scenario: GPS switched off, Wifi and 3G on.

Whenever I was to far from any houses (and likewise Wifi routers) on the highway I got no accurate positions. But as soon as I got close to houses, especially denser populated areas, it became very accurate. In most cases I was shown the exact adress, sometimes just the next house or so.

I wonder what else can be done with all of that accumulated data!
 

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

I have the same issue as well. Thankfully the State property works.
 

Gary Miyakawa

Active Member
Licensed User
Longtime User
I've run into an inconsistance problem with the number of Networks this library returns. Has anyone else seen this ? (I created a new thread for it too)..

Thanks,

Gary M
 

andrewtheart

Member
Licensed User
Longtime User
What conditions would cause ABGetLocation("myWifi") to return false and ABWifiLastError() to be blank when -

a) device is connected to a wireless network, and
b) "Use wireless networks" is enable in the Android location settings?

I am stumped.
 

andrewtheart

Member
Licensed User
Longtime User
What conditions would cause ABGetLocation("myWifi") to return false and ABWifiLastError() to be blank when -

a) device is connected to a wireless network, and
b) "Use wireless networks" is enable in the Android location settings?

I am stumped.
Right now the only possibility I can think of is that the wireless network is secured with WEP or WPA2 PSK (don't know which as a user connected to their home network is reporting these issues to me)

I don't have this issue on 3g networks and on a wireless network with "open authentication" (access control through MAC address filtering)
 

pinoy_ako

Member
Licensed User
Longtime User
is there a way to programmatically connect a wifi device to android phone? i have a mini wifi camera, android phone can't find the camera. but windows phone and iphone can find.

i also have this problem before with LMX9838 uart bluetooth. it can't be detected by android phone. but you can connect to it by setting the bt id on your program.

i dont know how to do it in wifi. need advice. appreciate your help.

thanks
 

Inman

Well-Known Member
Licensed User
Longtime User
Thanks for the update. Anyway to fix the issue mentioned by francoisg?

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
 

alwaysbusy

Expert
Licensed User
Longtime User
I cannot reproduce the problem here.

There is not much happening. When you run ABLoadWifi it checks your current Wifi connection. If your are connected, I'll temporary save it's SSID.

Then I'll run over all the wifi connections and if the SSID = temporary saved SSID I'll set isconnected to true.

Maybe you can check it in b4a by doing the same.

Do an ABLoadWifi, do a for loop through all the found wifi's (with ABNumberOfAvailableWifiNetworks and ABGetWifiNetwork) and check if the SSID of one of them = ABGetCurrentWifiInfo.SSID

It's the same check I'll do in Java.
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
Having some trouble, myWifi.ABNumberOfAvailableWifiNetworks returns 0 even though OS Settings returns a bunch of wifi hotspots near me
 

Gary Miyakawa

Active Member
Licensed User
Longtime User
I found that I had to wait at least 2.5 seconds before the wifi names/count would be available (and correct)...

Cheers,

Gary M
 

NJDude

Expert
Licensed User
Longtime User
I found that I had to wait at least 2.5 seconds before the wifi names/count would be available (and correct)...

Cheers,

Gary M

Just an observation, the "wait" time varies from device to device, a pause is needed indeed, but you better put a longer pause or slower devices will fail to acquire the available WiFi networks in range.
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
That sounds sub-optimal...

Could you either:
-trigger an event when its done
-have a variable set to false when we poll for hotspots then switch to true when done?

EDIT: Right now I'm polling till ABNumberOfAvailableWifiNetworks>0 but then I can't tell the difference between it's still loading or no hotspots were found

EDIT2: So what exactly is the signal level? It's always a negative number, and seems to be inversely proportional to the actual signal strength
Ah, it's in dBm. And I have to convert it to % myself...

http://www.tamos.com/htmlhelp/commwifi/understandingsignalstrength.htm

The level of 100% is equivalent to the signal level of -35 dBm and higher, e.g. both -25 dBm and -15 dBm will be shown as 100%, because this level of signal is very high. The level of 1% is equivalent to the signal level of -95 dBm. Between -95 dBm and -35 dBm, the percentage scale is linear, i.e. 50% is equivalent to -65 dBm.

http://www.tested.com/news/how-to-measure-cell-signal-strength-on-android-phones/557/

A signal of -60dBm is nearly perfect, and -112dBm is call-dropping bad. If you're above about -87 dBm, Android will report a full 4 bars of signal.
 
Last edited:

droman

Member
Licensed User
Longtime User
There are some functions in this library that tells us the name of the city in which the user is?
 
Top