B4A Library Simple WIFI library

NOTICE :

I am looking for someone to take over development and maintenance on this and other libraries. I am just no longer interested in programming and don't know how long I will feel that way. Anyone interested in taking over this library or any of my others, let me know a bit about yourself by email to [email protected]

Thanks,

--- Jem Miller

The Eclipse project and all source code has been attached below.


This is a very simple library that uses only 16 methods:

This is version 2.17. Don't panic, this version does not break anything!

I re-wrote the MACAddress method to handle Android M (API 23) and above. I also added the INTERNET and ACCESS_FINE_LOCATION permissions which seems to be required in API 23, and added a new method getAPI() that returns the version of Android running on the device as an integer.

SEE THE NEW WIFITEST EXAMPLE included in the zip file for examples of how to use this library.


MLwifi
Author:
Jem Miller - Missing Link Software
Version: 2.17
  • MLScan
    Events:
    • ScanDone (Results() As String, Count As Int)
    Fields:
    • wifis As String[]
      A string array containing all scanned access points in the area of the device.
      These are comma separated entries consisting of the following:
      SSID, Security type, signal strength, and "Saved" if this network was previously saved for use on this device.
    Methods:
    • Disconnect As Boolean
      Disconnects the current wifi AP connection
      Returns True on success
    • WifiCap (EntryNumber As Int) As String
      Returns a String containing info about the entry. A startScan must have already been done. The string contains the SSID, the BSSID, and the capabilities of the network access point.
      Each item is separated by a comma (SSID,BSSID,CapabilityString). The capabilities part can be quite long.
      EntryNumber - If you display the string array wifis or the array returned in the getWifiList method UNSORTED, you can pass the Position variable from your ListView_Click event.
      Otherwise, you must pass the entry number from the string array since that is what is used as a reference.
      Returns a string of comma separated entries of SSID,BSSID, and capability string. Returns an empty string on error.
    • addAP (EntryNumber As Int, securityType As String, Password As String) As Boolean
      Adds an entry to the wifi manager and connects to it. This method saves the configuration of the new entry onto the device. Use the forgetAP method to delete them. Updated to handle quoted or unquoted SSID strings.
      EntryNumber - If you display the string array wifis or the array returned in the getWifiList method UNSORTED, you can pass the Position variable from your ListView_Click event.
      Otherwise, you must pass the entry number from the string array since that is what is used as a reference.
      securityType - The Access Point security type (returned in the scan result string array)
      Password - The password/phrase for the wifi access point. Pass "" for an open network. Handles WEP HEX or text phrases
      It is up to you to supply all information. The security type is found in the scan results array.

      Returns True if the connection is successful
    • connectToAP (EntryNumber As Int) As Boolean
      Connects to a network already configured on the device. The device will use saved values to connect. Updated to handle quoted or unquoted SSID strings.
      EntryNumber - If you display the string array wifis or the array returned in the getWifiList method UNSORTED, you can pass the Position variable from your ListView_Click event.
      Otherwise, you must pass the entry number from the string array since that is what is used as a reference.
      Returns true if successful
    • forgetAP (NetId As Int) As Boolean
      Forgets (erases) a stored access point on the device.
      NetId - The network ID number to forget (returned in the listSavedNetworks method)
      Returns True if successful
    • getBSSID (EntryNumber As Int) As String
      Returns the BSSID for the selected entry.
      EntryNumber - If you display the string array wifis or the array returned in the getWifiList method UNSORTED, you can pass the Position variable from your ListView_Click event.
      Otherwise, you must pass the entry number from the string array since that is what is used as a reference.
    • isStored (EntryNumber As Int) As Boolean
      Checks the stored networks on the device to see if this entry is a previously saved access point
      EntryNumber - The entry number in the wifis array to check. If you display the string array wifis or the array returned in the getWifiList method UNSORTED, you can pass the Position variable from your ListView_Click event.
      Otherwise, you must pass the entry number from the string array since that is what is used as a reference.
      Returns True if this is a stored network
    • listSavedNetworks As List
      Builds a list of all saved networks on the device.
      Returns a comma separated list of SSID's and their BSSID and network ID number.
    • startScan (EventName As String)
      Initializes all objects and scans for wifi networks. Builds a string array of results (wifis) that can be accessed directly or with a call to updateWifiList.
      THIS METHOD SHOULD ONLY BE CALLED ONCE!
    • updateWifiList
      Call only after startScan has already been called and a string array of Access Points has been built. This method returns a string array containing the access points from the most recent scan, including background scans done automatically by the Android WifiManager. *
      Calls the ScanDone event when a new list is ready.
      Returns the string array of comma separated wifi access points by SSID. Each entry may contain the following:
      SSID, Security type, Signal level (in DB's), and the word "Saved" if the network was previously saved for use on this device.
    Properties:
    • LinkSpeed As Int [read only]
      Returns the LinkSpeed of the connected wifi access point
  • MLwifi
    Methods:
    • EnableWifi (Enabled As Boolean)
      Turns wifi on or off
      Pass True to enable wifi or False to disable.
      Example
      dim wifi as MLwifi
      wifi.EnableWifi(true)
    • IpAddress As String
      Get IP address from the wifi interface)
      Returns address or empty string
    • MACAddress As String
      Returns MAC address of the current interface

      Returns MAC address or empty string
    • SSID As String
      Returns the current SSID
    • WifiSignal As Int
      Returns the raw signal strength in decibels as an integer
      Returns 0 on error
    • WifiStrength As Int
      Returns an integer between 0 and 100 as a percentage of signal strength
      Returns 0 on error.
    • isOnLine As Boolean
      Checks for internet connection
      This method does not check or care what type of network is used.
      It can be wifi, 2g, 3g, 4g, wimax, etc. It just tests for internet connectivity.
      Returns True if internet is available.
    • isWifiConnected As Boolean
      Checks if wifi is connected. This only checks for wifi connection to a router.
      It does NOT test for internet availability. Use isOnLine() for that.
      Returns True if wifi is connected or False if 2g, 3g, 4g, wimax or no connection.
    Permissions:
    • android.permission.ACCESS_FINE_LOCATION
    • android.permission.ACCESS_NETWORK_STATE
    • android.permission.ACCESS_WIFI_STATE
    • android.permission.CHANGE_WIFI_STATE
    • android.permission.INTERNET
    Properties:
    • API As Int [read only]
      Returns the current devices Android API (the version of Android running on the device.) as an integer such as 19, 21, 23, etc.


There is a small test app included and a readme file as well. The whole mess is tiny and requires no other libs.

--- Jem

Version 1.00 - Initial release.

Version 1.01 - Added EnableWifi that I forgot in the initial release.

Version 1.02 - Using a different method to find IP address.

Version 1.03 - Adds getSSID

Version 1.04 - Removed "'s from getSSID. Renamed IsWifiConnected to isWifiConnected.

Version 1.05 - Adding AP scanning.

Version 1.06 - Adds getWifiSignal and getWifiStrength

Version 1.07 - Fixed an error in the test project and forced getSSID to return an empty string instead of Null if no SSID is found.

Version 2.00 - New connection manager methods in the MLScan class. Also changed the string array returned on a scan to include more information in a comma separated list of: SSID, security type, signal strength, and the word "Saved" if this network is saved on the device.

Version 2.01 - Added a new method WifiCap(SSID). This returns a string with comma separated fields of SSID, BSSID, and network capabilities. The capabilities refers to its security protocols. I also included a new version of the wifitest app. I forgot to add it to the version 2.00 library zip file. Tapping on a listed SSID will allow you to connect to that network.

Version 2.10 - Added Disconnect, isStored, and LinkSpeed and changed the way you must call several methods.

Version 2.11 - Removed WifiList and added updateWifiList. Fixed the connectToAP and isStored methods. Added a ScanDone event to automate scan results (no more timers needed). Added error correction and more speed.

Version 2.1.2 - Updated ScanDone event to allow real time scan results.

Version 2.13 - Updated the connect methods to handle either quoted or unquoted SSID values so in theory any version of the API should now work correctly for connecting to an AP.

Version 2.14 - Fixed the ConnectToAP method (Hopefully). Also updated the wifitest app to fix a few minor bugs.

Version 2.15 - Corrected a null pointer error if wifi is turned off during operation.

Version 2.16 - Added some error checking an changed the connecttoAP method slightly. It now checks both the SSID and BSSID to ensure that correct AP is connect to.

Version 2.17 - Added permissions for API 23, Re-wrote the MACAddress method so it returns a valid MAC address on Android M OR any lower version of Android. Added the getAPI method to get the current version of Android on the device.

.
 

Attachments

  • mlwifi-2.17.zip
    23.2 KB · Views: 2,435
  • mlwifi-src.zip
    16.4 KB · Views: 1,469
Last edited:

HotShoe

Well-Known Member
Licensed User
Longtime User
Hmm, really, I get it on my tablet, bluestacks, and S4... I'll need to do some testing I guess.

Stupid question, but you do have wifi turned on before you try to get the IP address?

I did forget to add 1 method, so the new version has 5 now. I added the EnableWifi to turn wifi on or off.

--- Jem
 
Last edited:

Mahares

Expert
Licensed User
Longtime User
You did not include a sample, so I created this simple code. Please help me see if I am interpreting your methods correctly. Here is my code:
B4X:
Dim MyWiFi As MLwifi
Log("My Device IP Address IPv4: " & MyWiFi.getIPAddress(True))  'displays my device 192.xxx.0.xxx
Log("Mac address: " & MyWiFi.getMACAddress)  'displays:  00:xx:xx:xx:xx:xx
Log("Is my device connected to WiFi: " &  MyWiFi.isOnLine)   'displays true
Log("Is internet available where I am now: " & MyWiFi.IsWifiConnected)  'displays true
 

NJDude

Expert
Licensed User
Longtime User
Ok, I narrowed down, it doesn't seem to work on KitKat (SDK 19) I tried on my HTC One and Nexus 7, no IP shown, on my Galaxy tab (ICS SDK 15) doesn't work either but on a Froyo device, I see the device's IP.

@Mahares: There's a sample included with the library is on a ZIP file named WiFiTest.zip
 

Mahares

Expert
Licensed User
Longtime User
Thanks NJDUde. I see his sample now. I was able to run the little code I made on OS 4.03 and was able to see the IP address. I just like HotShoe to clarify these 2 methods for me. Am I understanding them correctly?
B4X:
Log("Is my device connected to WiFi: " & MyWiFi.isOnLine) 'displays true
Log("Is internet available where I am now: " & MyWiFi.IsWifiConnected) 'displays true
 

HotShoe

Well-Known Member
Licensed User
Longtime User
Thanks NJDUde. I see his sample now. I was able to run the little code I made on OS 4.03 and was able to see the IP address. I just like HotShoe to clarify these 2 methods for me. Am I understanding them correctly?
B4X:
Log("Is my device connected to WiFi: " & MyWiFi.isOnLine) 'displays true
Log("Is internet available where I am now: " & MyWiFi.IsWifiConnected) 'displays true

isOnLine tests to see if you are connected to the internet. Is WifiConnected checks to see that wifi is turned on and connected to an AP, but it doesn't care about intenet (local LAN).

--- Jem
 

HotShoe

Well-Known Member
Licensed User
Longtime User
Ok, I narrowed down, it doesn't seem to work on KitKat (SDK 19) I tried on my HTC One and Nexus 7, no IP shown, on my Galaxy tab (ICS SDK 15) doesn't work either but on a Froyo device, I see the device's IP.

@Mahares: There's a sample included with the library is on a ZIP file named WiFiTest.zip

I dunno man. My S4 is 4.4.2 and it gets the IP on it, My tablet is 4.1 and blurstacks is 4.1 or 4.2 based. I also tried it on my old chinese tablet that is froyo (2.2) and it works as well. I'll look around for a better method.

--- Jem
 

HotShoe

Well-Known Member
Licensed User
Longtime User
A new version is available. Hopesfully the method used for the IP address will work on all devices.
 

jhamill

New Member
Licensed User
Longtime User
Hi Jem
works fine for me thanks.. any chance of adding SSID in there at some stage?
john
 

HotShoe

Well-Known Member
Licensed User
Longtime User
Fixed... OK, so my bitwise operations are a bit rusty... :)

New version is in the first post.

--- Jem
 

HotShoe

Well-Known Member
Licensed User
Longtime User
Hi Jem
works fine for me thanks.. any chance of adding SSID in there at some stage?
john


SSID should be easy enough. I'll look at that tonight.

--- Jem
 

HotShoe

Well-Known Member
Licensed User
Longtime User
Added getSSID. New version attached to first post.

--- Jem
 

NJDude

Expert
Licensed User
Longtime User
One minor thing, can you remove the quotes surrounding the SSID? if my network SSID is for example MyHome the lib reports "MyHome" (in quotes)

Another little thing, change IsWifiConnected to isWifiConnected (to lower case i), I know, is silly, but my OCD is killing me :D
 

HotShoe

Well-Known Member
Licensed User
Longtime User
One minor thing, can you remove the quotes surrounding the SSID? if my network SSID is for example MyHome the lib reports "MyHome" (in quotes)

Another little thing, change IsWifiConnected to isWifiConnected (to lower case i), I know, is silly, but my OCD is killing me :D

LOL, it shall be done. I noticed the quotes after posting the update. I figured 4 updates in one day was enough hehe.

--- Jem
 

HotShoe

Well-Known Member
Licensed User
Longtime User
NJ, your OCD can rest easy. All is right with the world again... :)

--- Jem
 

Peter Simpson

Expert
Licensed User
Longtime User
Hello @HotShoe , this is an excellent library.
Do you think that you will ever add the facility for your library to list all the WiFi networks that are currently available on a device. Basically all the viewable WiFi networks?

Keep up the great work.
 
Last edited:

HotShoe

Well-Known Member
Licensed User
Longtime User
Good question. I didn't intend to but I will look at what it takes to scan an list them. The intention was to do the things that I needed and to be a small, fast library. I'll play around with it though.

--- Jem
 
Top