B4A Library Simple WIFI library Updated V. 3.07 09-05-2018

This library is based on the library Jem:https://www.b4x.com/android/forum/threads/simple-wifi-library.38601/

Which Jem can no longer maintain.

I needed some methods that the library did not have, for an application I'm doing, and instead of creating a library from scratch, I preferred to take advantage of Jem's library and create those methods that I needed.

I've modified the source code, I have corrected some errors and added new methods that I needed.

I’ve tested on Android 4x, 5x and 6x on different devices and it gives me no error.

Author: V-2.17 by Jem Miller - Missing Link Software

Permissions:
android.permission.ACCESS_NETWORK_STATE
android.permission.ACCESS_WIFI_STATE
android.permission.CHANGE_WIFI_STATE
android.permission.INTERNET
android.permission.WAKE_LOCK


______________________________________________________________________

V-3.07 by bgsoft

Corrected compatibility issues with API level <21
______________________________________________________________________

V-3.06 by bgsoft

Corrected compatibility issues with API level <23

______________________________________________________________________
V-3.05 by bgsoft

Events:

Modified

ScanDone (Results() As String, Count As Int) Results()

returns a string array containing all scanned Access Points in the area of the device, sorted 'by Signal strength. These are comma separated entries consisting of the following:
SSID, Security Type, Signal strength (dBm) , "Saved" (if this network was previously saved for use on this device) or “”, BSSID, Signal Percentage (0-100%), Frequency (Mhz), Center Frequency 0 (Mhz), Center Frequency 1 (Mhz), Channel Width (Mhz), Channel number, Band (2.4 Ghz / 5Ghz), Distance (m).



Field:

Modified

Wifis As String()

A string array containing all scanned Access Points in the area of the device, sorted 'by Signal strength. These are comma separated entries consisting of the following:
SSID, Security Type, Signal strength (dBm) , "Saved" (if this network was previously saved for use on this device) or “”, BSSID, Signal Percentage (0-100%), Frequency (Mhz), Center Frequency 0 (Mhz), Center Frequency 1 (Mhz), Channel Width (Mhz), Channel number, Band (2.4 Ghz / 5Ghz), Distance (m).


______________________________________________________________________

V-3.04 by bgsoft

New Constants:


CHANNEL_WIDTH_20MHZ = 0

Channel Width 20Mhz

CHANNEL_WIDTH_40MHZ = 1

Channel Width 40Mhz

CHANNEL_WIDTH_80MHZ = 2

Channel Width 80Mhz

CHANNEL_WIDTH_160MHZ = 3

Channel Width 160Mhz

CHANNEL_WIDTH_80MHZ_PLUS_MHZ = 4

Channel Width 80Mhz + 80Mhz


Events:

Modified

ScanDone (Results() As String, Count As Int) Results()

returns a string array containing all scanned Access Points in the area of the device, sorted 'by Signal strength. These are comma separated entries consisting of the following:
SSID, Security Type, Signal strength (dBm) , "Saved" (if this network was previously saved for use on this device) or “”, BSSID, Signal Percentage (0-100%), Frequency (Mhz), Channel Width (Mhz), Channel number, Band (2.4 Ghz / 5Ghz), Distance (m).


Field:

Modified


Wifis As String()

A string array containing all scanned Access Points in the area of the device, sorted 'by Signal strength. These are comma separated entries consisting of the following:
SSID, Security Type, Signal strength (dBm) , "Saved" (if this network was previously saved for use on this device) or “”, BSSID, Signal Percentage (0-100%), Frequency (Mhz), Channel Width (Mhz), Channel number, Band (2.4 Ghz / 5Ghz), Distance (m).


____________________________________________________________________________
V-3.03 by bgsoft

Events:


Modified

ScanDone (Results() As String, Count As Int)
Results() Sorted by Signal strength.


Method:

NEW
StopScan *** Erase by Error in version 3.02

NEW
WifiAPDistance As Double
Returns the estimated distance to WIFI Access Point, depending on the signal level and frequency
Returns 0 on error.

Modified
StartScan (EventName As String, NoEventOnSystemScan As Boolean)
Recovered parameter "NoEventOnSystemScan", Erase in version 3.02
Does not start Wifi if it is stopped.

Field:

Modified
Wifis As String()
A string array containing all scanned Access Points in the area of the device, sorted 'by Signal strength. 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.


____________________________________________________________________________
V-3.02 by bgsoft

Constants:

WIFI_2_4GHZ_BAND As Int = 1

2.4GHz WIFI Band

WIFI_5GHZ_BAND As Int = 2

5GHz WIFI Band

WIFI_MODE_FULL As Int = 1

In this WIFI lock mode, WIFI will be kept active, and will behave normally. It will attempt to automatically establish a connection to a remembered access point that is within range, and will do periodic scans if there are remembered access points but none are in range.

WIFI_MODE_SCAN_ONLY As Int = 2

In this WIFI lock mode, WIFI will be kept active, but the only operation that will be supported is initiation of scans, and the subsequent reporting of scan results. No attempts will be made to automatically connect to remembered access points, nor will periodic scans be automatically performed looking for remembered access points.
Scans must be explicitly requested by an application in this mode.

WIFI_MODE_FULL_HIGH_PERF As Int = 3

In this WIFI lock mode, WIFI will be kept active as in mode WIFI_MODE_FULL but it operates at high performance with minimum packet loss and low packet latency even when the device screen is off.
This mode will consume more power and hence should be used only when there is a need for such an active connection.


Events:

isOnline_PingDone(isOnline As Boolean)

Launches when "isOnlinePing5" finishes or when "isOnlinePing6" finishes or timeout
isOnline: Returns True if Internet is available


Methods:

WifiFrequency As Int

Returns the Link Frequency (MHz) of the connected WIFI Access Point
Returns 0 on error.

WifiChannel As Int

Returns the Link Channel of the connected WIFI Access Point
2.4GHz Band: 1 - 13, 14 (Japan only)
5GHz Band: 0 - 200
Returns -1 on error.

WifiBand As Int

Returns the Link Band of the connected WIFI Access Point
WIFI_2_4GHZ_BAND: 2.4GHz Band
WIFI_5GHZ_BAND: 5GHz Band
Returns 0 on error.

is5GHzBandSupported As Boolean

Returns true if this WIFI adapter supports 5 GHz band

holdWifiOn As Boolean

Allows an application to keep the WIFI radio awake. Normally the WIFI radio may turn off when the user has not used the device in a while.
WIFI radio will keep awake until "releaseWifiOn" is called.
Returns True on success

holdWifiOn2(lockType As Int) As Boolean

Allows an application to keep the WIFI radio awake. Normally the WIFI radio may turn off when the user has not used the device in a while.
WIFI radio will keep awake until "releaseWifiOn" is called.
lockType: WIFI_MODE_FULL, WIFI_MODE_SCAN_ONLY, WIFI_MODE_FULL_HIGH_PERF
Returns True on success

releaseWifiOn As Boolean

Unlocks the WIFI radio, previously locked by calling "holdWifiOn" or "holdWifiOn2", allowing it to
turn off when the device is idle.
Returns True on success

isWifiHeldOn As Boolean

Checks if WIFI is held On by a call to "holdWifiOn" or "holdWifiOn2"


isOnlinePing5(timeout As Long) As Boolean

Checks for Internet connection by sending a Ping to Google DNS server (Ip: 8.8.8.8)
Launches the event "isOnline_PingDone(isOnline As Boolean)" with the ping result
timeout: Maximum waiting time in milliseconds
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 success

isOnlinePing6(IpHost As String, timeout As Long) As Boolean

Checks for Internet connection by sending a Ping to IpHost
Launches the event "isOnline_PingDone(isOnline As Boolean)" with the ping result
IpHost: Ip address or Host name
timeout: Maximum waiting time in milliseconds
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 success

__________

V-3.01 by bgsoft

I have added the "stopScan" method to stop "_ScanDone" events from scans made automatically by the system, in the background.

I have also modified the "startScan" method, adding the parameter "NoEventOnSystemScan" that allows to disable the events coming from Scans made automatically by the system, in the background.

Two new isOnlinePing3 and isOnlinePing4 methods, equivalent to isOnlinePing and isOnlinePing2 but adding a timeout parameter, in milliseconds.

NEW

isOnlinePing3(timeout As Long) As Boolean

Checks for Internet connection by sending a Ping to Google DNS server (Ip: 8.8.8.8)
timeout: Maximum waiting time in milliseconds
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

isOnlinePing4(IpHost As String, timeout As Long) As Boolean

Checks for Internet connection by sending a Ping to IpHost
IpHost: Ip address or Host name
timeout: Maximum waiting time in milliseconds
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

stopScan

Stops events launched on system background scans, if enabled in "startScan".
Call "updateWifiList" to re-activate this events.

UPDATED
startScan(EventName As String, NoEventOnSystemScan As Boolean)

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.
EventName: Name of the Event to launch when Scan finish (EventName_ScanDone)
NoEventOnSystemScan: Launches events only on requested scans (true) or on all scans (false), including system background scans.
THIS METHOD SHOULD ONLY BE CALLED ONCE!

______________________________________________________________________________________

V-3.00 by bgsoft

Class MLwifi

Methods:


APIversion As Int
Returns the Version of Android running on the device as an integer such as 19, 21, 23, etc

EnableWifi(Enabled As Boolean)
Turns WIFI On or Off. Pass True to Enable WIFI or False to Disable

isWifiEnabled As Boolean
Returns whether WIFI is Enabled (true) or Disabled (false)

WifiMACAddress As String
Returns the MAC Address of the WIFI interface or empty string

WifiIpAddress As String
Returns the IP Address of the WIFI interface or empty string

WifiSSID As String
Returns the current WIFI SSID or empty string

WifiSignal As Int
Returns the WIFI signal strength in dBm as an integer or 0 on error

WifiStrength As Int
Returns the WIFI signal strength in percentage as an integer (0 to 100)
Returns 0 on error
This method fails in some devices. Use WifiSignalPct() instead

CalcWifiPct(int WifiSignal)
Calculates WIFI signal percentage from the WIFI signal level as an integer (0 to 100)
Signal Range: -100dBm -> 0%, -50dBm -> 100%

WifiSignalPct As Int
Returns the WIFI signal strength in percentage as an integer (0 to 100)
Returns 0 on error

WifiLinkSpeed As Int
Returns the LinkSpeed of the connected WIFI Access Point
Returns 0 on error.

isWifiConnectedOld As Boolean
Checks if WIFI is connected. This only checks for WIFI connection to a router
It does NOT test for Internet availability
Returns True if WIFI is connected or False if 2G, 3G, 4G, WIMAX or no connection
This method uses "getNetworkInfo()" that was deprecated in API level 23

isWifiConnected As Boolean
Checks if WIFI is connected. This only checks for WIFI connection to a router
It does NOT test for Internet availability
Returns True if WIFI is connected or False if 2G, 3G, 4G, WIMAX or no connection

isMobileConnected As Boolean
Checks if Phone Data is connected. This only checks for connection to a phone network.
It does NOT test for Internet availability.
Returns True if 2G, 3G or 4G Data is connected or False if WIFI, WIMAX or no connection

ActiveNetworkTypeName As String
Returns the Type (0:MOBILE, 1:WIFI, 6:WIMAX, 7:BLUETOOTH, 9:ETHERNET, etc) of the current Active Network or -1

ActiveNetworkType As Int
Returns the Type Name (WIFI, MOBILE, etc) of the current Active Network or empty string

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
ERROR!!!. This method only checks if the device is connected to any network (WIFI, Phone Data, etc),
but the network can be not connected to Internet (ADSL router not connected to phone line, etc)

isOnlinePing As Boolean
Checks for Internet connection by sending a Ping to Google DNS server (Ip: 8.8.8.8)
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

isOnlinePing2(String IpHost) As Boolean
Checks for Internet connection by sending a Ping to any Internet server
IpHost: Ip address or Host name
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

saveWifiAP(String ssid, int security, String password, boolean connect) As Boolean
Saves the configuration of a WIFI AP onto the device
ssid: Access Point SSID
security: Access Point security type (0:NONE, 1:WEP, 2:WAP)
password: Access Point password/phrase ("" for an open network)
connect: true -> Connect to the WIFI AP after saving
Returns True on success

isSavedWifiAP(String ssid) As Boolean
Checks if a WIFI AP is saved on the device
ssid: Access Point SSID
Returns True if this SSID is saved

removeWifiAP(String ssid) As Boolean
Remove a saved WIFI AP on the device
ssid: Access Point SSID
Returns True on success

disconnectWifiAP() As Boolean
Disconnects the current WIFI AP connection
Returns True on success

connectWifiAP(String ssid) As Boolean
Connects to a WIFI AP already configured on the device
ssid: Access Point SSID
Returns true on success

Class MLscan

Events:

ScanDone(Results() As String, Count As Int)
Launches when Scan finishes and a new list of Access Points is ready
Results(): 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.
Count: The number of elements in the Results() array

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:

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.
EventName: Name of the Event to launch when Scan finish (EventName_ScanDone)
THIS METHOD SHOULD ONLY BE CALLED ONCE!
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 dBm), and the word "Saved" if the network was
previously saved for use on this device.
updateWifiList

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.

WifiCap(EntryNumber As Int) As String
Returns a String containing info about the selected entry. A startScan must have already been done.
The string contains the SSID, the BSSID, and the Capabilities of the Network Access Point,
comma separated (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.

isSavedWifiAP(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

connectWifiAP(EntryNumber As Int) As Boolean
Connects to a network already configured on the device. The device will use saved values to connect.
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 on success

listSavedNetworks As List
Builds a list of all saved networks on the device.
Returns a comma separated list of: SSID,BSSID,Network ID number.

saveWifiAP(EntryNumber As Intr, securityType As String, Password As String) As Boolean
Saves the configuration of a WIFI AP onto the device and connects to it.
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
Returns True if the connection is successful

removeWifiAP(NetId As Int) As Boolean
Remove a saved WIFI AP on the device
NetId - The network ID number to delete (returned in the listSavedNetworks method)
Returns True on success



Regards
 

Attachments

  • MLWifi_3_0.zip
    13.7 KB · Views: 712
  • MLWifi_3_01.zip
    27.6 KB · Views: 639
  • ML_Wifi_3_02.zip
    17.4 KB · Views: 627
  • MLWifi_3_03.zip
    18.8 KB · Views: 1,142
  • MLwifi 3_04.zip
    19.5 KB · Views: 502
  • MLwifi_3.05.zip
    19.6 KB · Views: 542
  • MLwifi_3.06.zip
    19.7 KB · Views: 550
  • MLwifi_3.07.zip
    19.8 KB · Views: 1,593
Last edited:

bgsoft

Well-Known Member
Licensed User
Longtime User
Hi:

I have implemented new methods and events:

Changes v-3.03:
 

bgsoft

Well-Known Member
Licensed User
Longtime User
I noticed that the ScanDone is being fired multiple times because the library registers a broadcast receiver and so every time the OS performs a scan the ScanDone sub get fired.
For avoiding to execute the code in the sub Scandone when not needed you can use for example a boolean variable like this example

B4X:
Sub FireIt
ScanStarted=True
wifi.startscan("wifi")
End sub

Sub wifi_ScanDone
If ScanStarted==True then
    Scanstarted=False

    '......<Your remaining code here>.....

End if
End sub

With the new library 3.03, you can use the NoEventOnSystemScan parameter of the method StartScan.
Or the Stopscan method.

Regards
 

JTmartins

Active Member
Licensed User
Longtime User
Hi, thanks for saying it, I deleted it by mistake :D.


I've tried it on several devices and it works fine


regards

I have some problems with this...

In one device I always get a false status. Even when connection is achieved.

In other device it reports properly for WPA, but it always reports true for WEP (even trying to connect with wrong password) even when connection is not achieved.
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
is it possible to add an ap with "advanced settings" ? i.e. static IP configuration?
Look at the documentation....

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.

So i would guess yes.
 

sfgwl

Member
Licensed User
Longtime User
This library is based on the library Jem:https://www.b4x.com/android/forum/threads/simple-wifi-library.38601/

Which Jem can no longer maintain.

I needed some methods that the library did not have, for an application I'm doing, and instead of creating a library from scratch, I preferred to take advantage of Jem's library and create those methods that I needed.

I've modified the source code, I have corrected some errors and added new methods that I needed.

I’ve tested on Android 4x, 5x and 6x on different devices and it gives me no error.

Author: V-2.17 by Jem Miller - Missing Link Software

Permissions:
android.permission.ACCESS_NETWORK_STATE
android.permission.ACCESS_WIFI_STATE
android.permission.CHANGE_WIFI_STATE
android.permission.INTERNET
android.permission.WAKE_LOCK


____________________________________________________________________________
V-3.03 by bgsoft

Events:


Modified

ScanDone (Results() As String, Count As Int)
Results() Sorted by Signal strength.


Method:

NEW
StopScan *** Erase by Error in version 3.02

NEW
WifiAPDistance As Double
Returns the estimated distance to WIFI Access Point, depending on the signal level and frequency
Returns 0 on error.

Modified
StartScan (EventName As String, NoEventOnSystemScan As Boolean)
Recovered parameter "NoEventOnSystemScan", Erase in version 3.02
Does not start Wifi if it is stopped.

Field:

Modified
Wifis As String()
A string array containing all scanned Access Points in the area of the device, sorted 'by Signal strength. 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.


____________________________________________________________________________
V-3.02 by bgsoft

Constants:

WIFI_2_4GHZ_BAND As Int = 1

2.4GHz WIFI Band

WIFI_5GHZ_BAND As Int = 2

5GHz WIFI Band

WIFI_MODE_FULL As Int = 1

In this WIFI lock mode, WIFI will be kept active, and will behave normally. It will attempt to automatically establish a connection to a remembered access point that is within range, and will do periodic scans if there are remembered access points but none are in range.

WIFI_MODE_SCAN_ONLY As Int = 2

In this WIFI lock mode, WIFI will be kept active, but the only operation that will be supported is initiation of scans, and the subsequent reporting of scan results. No attempts will be made to automatically connect to remembered access points, nor will periodic scans be automatically performed looking for remembered access points.
Scans must be explicitly requested by an application in this mode.

WIFI_MODE_FULL_HIGH_PERF As Int = 3

In this WIFI lock mode, WIFI will be kept active as in mode WIFI_MODE_FULL but it operates at high performance with minimum packet loss and low packet latency even when the device screen is off.
This mode will consume more power and hence should be used only when there is a need for such an active connection.


Events:

isOnline_PingDone(isOnline As Boolean)

Launches when "isOnlinePing5" finishes or when "isOnlinePing6" finishes or timeout
isOnline: Returns True if Internet is available


Methods:

WifiFrequency As Int

Returns the Link Frequency (MHz) of the connected WIFI Access Point
Returns 0 on error.

WifiChannel As Int

Returns the Link Channel of the connected WIFI Access Point
2.4GHz Band: 1 - 13, 14 (Japan only)
5GHz Band: 0 - 200
Returns -1 on error.

WifiBand As Int

Returns the Link Band of the connected WIFI Access Point
WIFI_2_4GHZ_BAND: 2.4GHz Band
WIFI_5GHZ_BAND: 5GHz Band
Returns 0 on error.

is5GHzBandSupported As Boolean

Returns true if this WIFI adapter supports 5 GHz band

holdWifiOn As Boolean

Allows an application to keep the WIFI radio awake. Normally the WIFI radio may turn off when the user has not used the device in a while.
WIFI radio will keep awake until "releaseWifiOn" is called.
Returns True on success

holdWifiOn2(lockType As Int) As Boolean

Allows an application to keep the WIFI radio awake. Normally the WIFI radio may turn off when the user has not used the device in a while.
WIFI radio will keep awake until "releaseWifiOn" is called.
lockType: WIFI_MODE_FULL, WIFI_MODE_SCAN_ONLY, WIFI_MODE_FULL_HIGH_PERF
Returns True on success

releaseWifiOn As Boolean

Unlocks the WIFI radio, previously locked by calling "holdWifiOn" or "holdWifiOn2", allowing it to
turn off when the device is idle.
Returns True on success

isWifiHeldOn As Boolean

Checks if WIFI is held On by a call to "holdWifiOn" or "holdWifiOn2"


isOnlinePing5(timeout As Long) As Boolean

Checks for Internet connection by sending a Ping to Google DNS server (Ip: 8.8.8.8)
Launches the event "isOnline_PingDone(isOnline As Boolean)" with the ping result
timeout: Maximum waiting time in milliseconds
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 success

isOnlinePing6(IpHost As String, timeout As Long) As Boolean

Checks for Internet connection by sending a Ping to IpHost
Launches the event "isOnline_PingDone(isOnline As Boolean)" with the ping result
IpHost: Ip address or Host name
timeout: Maximum waiting time in milliseconds
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 success

__________

V-3.01 by bgsoft

I have added the "stopScan" method to stop "_ScanDone" events from scans made automatically by the system, in the background.

I have also modified the "startScan" method, adding the parameter "NoEventOnSystemScan" that allows to disable the events coming from Scans made automatically by the system, in the background.

Two new isOnlinePing3 and isOnlinePing4 methods, equivalent to isOnlinePing and isOnlinePing2 but adding a timeout parameter, in milliseconds.

NEW

isOnlinePing3(timeout As Long) As Boolean

Checks for Internet connection by sending a Ping to Google DNS server (Ip: 8.8.8.8)
timeout: Maximum waiting time in milliseconds
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

isOnlinePing4(IpHost As String, timeout As Long) As Boolean

Checks for Internet connection by sending a Ping to IpHost
IpHost: Ip address or Host name
timeout: Maximum waiting time in milliseconds
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

stopScan

Stops events launched on system background scans, if enabled in "startScan".
Call "updateWifiList" to re-activate this events.

UPDATED
startScan(EventName As String, NoEventOnSystemScan As Boolean)

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.
EventName: Name of the Event to launch when Scan finish (EventName_ScanDone)
NoEventOnSystemScan: Launches events only on requested scans (true) or on all scans (false), including system background scans.
THIS METHOD SHOULD ONLY BE CALLED ONCE!

______________________________________________________________________________________

V-3.00 by bgsoft

Class MLwifi

Methods:


APIversion As Int
Returns the Version of Android running on the device as an integer such as 19, 21, 23, etc

EnableWifi(Enabled As Boolean)
Turns WIFI On or Off. Pass True to Enable WIFI or False to Disable

isWifiEnabled As Boolean
Returns whether WIFI is Enabled (true) or Disabled (false)

WifiMACAddress As String
Returns the MAC Address of the WIFI interface or empty string

WifiIpAddress As String
Returns the IP Address of the WIFI interface or empty string

WifiSSID As String
Returns the current WIFI SSID or empty string

WifiSignal As Int
Returns the WIFI signal strength in dBm as an integer or 0 on error

WifiStrength As Int
Returns the WIFI signal strength in percentage as an integer (0 to 100)
Returns 0 on error
This method fails in some devices. Use WifiSignalPct() instead

CalcWifiPct(int WifiSignal)
Calculates WIFI signal percentage from the WIFI signal level as an integer (0 to 100)
Signal Range: -100dBm -> 0%, -50dBm -> 100%

WifiSignalPct As Int
Returns the WIFI signal strength in percentage as an integer (0 to 100)
Returns 0 on error

WifiLinkSpeed As Int
Returns the LinkSpeed of the connected WIFI Access Point
Returns 0 on error.

isWifiConnectedOld As Boolean
Checks if WIFI is connected. This only checks for WIFI connection to a router
It does NOT test for Internet availability
Returns True if WIFI is connected or False if 2G, 3G, 4G, WIMAX or no connection
This method uses "getNetworkInfo()" that was deprecated in API level 23

isWifiConnected As Boolean
Checks if WIFI is connected. This only checks for WIFI connection to a router
It does NOT test for Internet availability
Returns True if WIFI is connected or False if 2G, 3G, 4G, WIMAX or no connection

isMobileConnected As Boolean
Checks if Phone Data is connected. This only checks for connection to a phone network.
It does NOT test for Internet availability.
Returns True if 2G, 3G or 4G Data is connected or False if WIFI, WIMAX or no connection

ActiveNetworkTypeName As String
Returns the Type (0:MOBILE, 1:WIFI, 6:WIMAX, 7:BLUETOOTH, 9:ETHERNET, etc) of the current Active Network or -1

ActiveNetworkType As Int
Returns the Type Name (WIFI, MOBILE, etc) of the current Active Network or empty string

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
ERROR!!!. This method only checks if the device is connected to any network (WIFI, Phone Data, etc),
but the network can be not connected to Internet (ADSL router not connected to phone line, etc)

isOnlinePing As Boolean
Checks for Internet connection by sending a Ping to Google DNS server (Ip: 8.8.8.8)
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

isOnlinePing2(String IpHost) As Boolean
Checks for Internet connection by sending a Ping to any Internet server
IpHost: Ip address or Host name
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

saveWifiAP(String ssid, int security, String password, boolean connect) As Boolean
Saves the configuration of a WIFI AP onto the device
ssid: Access Point SSID
security: Access Point security type (0:NONE, 1:WEP, 2:WAP)
password: Access Point password/phrase ("" for an open network)
connect: true -> Connect to the WIFI AP after saving
Returns True on success

isSavedWifiAP(String ssid) As Boolean
Checks if a WIFI AP is saved on the device
ssid: Access Point SSID
Returns True if this SSID is saved

removeWifiAP(String ssid) As Boolean
Remove a saved WIFI AP on the device
ssid: Access Point SSID
Returns True on success

disconnectWifiAP() As Boolean
Disconnects the current WIFI AP connection
Returns True on success

connectWifiAP(String ssid) As Boolean
Connects to a WIFI AP already configured on the device
ssid: Access Point SSID
Returns true on success

Class MLscan

Events:

ScanDone(Results() As String, Count As Int)
Launches when Scan finishes and a new list of Access Points is ready
Results(): 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.
Count: The number of elements in the Results() array

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:

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.
EventName: Name of the Event to launch when Scan finish (EventName_ScanDone)
THIS METHOD SHOULD ONLY BE CALLED ONCE!
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 dBm), and the word "Saved" if the network was
previously saved for use on this device.
updateWifiList

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.

WifiCap(EntryNumber As Int) As String
Returns a String containing info about the selected entry. A startScan must have already been done.
The string contains the SSID, the BSSID, and the Capabilities of the Network Access Point,
comma separated (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.

isSavedWifiAP(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

connectWifiAP(EntryNumber As Int) As Boolean
Connects to a network already configured on the device. The device will use saved values to connect.
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 on success

listSavedNetworks As List
Builds a list of all saved networks on the device.
Returns a comma separated list of: SSID,BSSID,Network ID number.

saveWifiAP(EntryNumber As Intr, securityType As String, Password As String) As Boolean
Saves the configuration of a WIFI AP onto the device and connects to it.
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
Returns True if the connection is successful

removeWifiAP(NetId As Int) As Boolean
Remove a saved WIFI AP on the device
NetId - The network ID number to delete (returned in the listSavedNetworks method)
Returns True on success



Regards
 

bgsoft

Well-Known Member
Licensed User
Longtime User
Hi, I have answered you in the Spanish forum.

Regards
 

mterveen

Member
Licensed User
Longtime User
Potential Bug - If you have a service called "Wifi" in your program it will cause compilation errors with this library.
 

Matt and John

New Member
Licensed User
On some older devices of mine ScanDone returns Results() properly in a service when no activities are showing, but on android 7.0 (or maybe just this device), ScanDone fires, however there are always empty Results() in the service when the activity is paused?

How can I still get Results() in a service when my activity isn't showing? Is it a bug in this library?

*edit - I found it happens only when the home button is pressed, but not when the power button turns off the screen.

Both raise the same activity pause - user closed = false event for activity, im confused
 
Last edited:

FrankBerra

Active Member
Licensed User
Longtime User
Maybe it is something related to this:
Android 6+ requires location for scanning wifi.
Info: https://stackoverflow.com/questions...-m-requires-location-permissions-to-scan-wifi

More detailed infos here:
https://developer.android.com/reference/android/net/wifi/WifiManager.html#getScanResults()
" An app must hold ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission in order to get valid results."

Adding the following lines in your manifest should solve the problem:
AddPermission(android.permission.ACCESS_FINE_LOCATION)
AddPermission(android.permission.ACCESS_COARSE_LOCATION)
 

Michael Wenning

Member
Licensed User
Longtime User
I have tried the scan function on Android 7 Samsung A3.
See attached project file.
I add permissions
AddPermission(android.permission.ACCESS_FINE_LOCATION)
AddPermission(android.permission.ACCESS_COARSE_LOCATION)
in manifest.
I have got always empty results on ScanDone firing.
On some older devices (< Android 6.0) of mine ScanDone returns Results() properly. ( See post #31).
I am thankful for every tip!
 

Attachments

  • MlwifiTest_V5.zip
    441.2 KB · Views: 463

Matt and John

New Member
Licensed User
Still has same issue as in post #31

An example that reproduces the error is attached.

On android 7, when you press 'home', the scan results in log output become 0, however, when you press the power button (to turn screen off), normal scan results are returned.

Both actions trigger the same ** Activity (main) Pause, UserClosed = false ** event

So why does pressing home return 0 wifi results (from a running service)? Is it a library bug? (It works fine on android 6 and below, just not on 7)

*edit - setting target APK to 26 get no scan results at all
 

Attachments

  • demo.zip
    7.5 KB · Views: 322
Last edited:

Michael Wenning

Member
Licensed User
Longtime User
Hello again,
i have updated the Android 7 Samsung A3 mobile phone to Android 8.0
The same code/app as in android 7.0.
No scan results at all. I reinstalled the app and added runtimepermission for the dangerous permissions.
Still no scanresult.
I am happy about any help.
 

Matt and John

New Member
Licensed User
Be sure you have downloaded the SDK you are targeting.

(Click Tools / SDK Manager, search SDK Platform 26 or your manifest's targetSdkVersion="26", select in the list and click Install Selected)
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
An example that reproduces the error is attached
Please create a new thread in the Questions forum for your question.
You are not handling the runtimepermissions correctly.
You are not targeting sdk 26

Running on Samsung Note 8, Android 8, TargetSDK 26

** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
** Activity (main) Resume **
*** Service (icon) Create ***
** Service (icon) Start **
Wifi scan started!
scan done 14
scan done 13
scan done 13
scan done 13
scan done 14
scan done 11
scan done 13
scan done 13
scan done 12
scan done 13
scan done 12
scan done 14
scan done 12
scan done 10
scan done 12
scan done 11
scan done 13
scan done 11
scan done 13
scan done 14
scan done 15
scan done 13
scan done 13
scan done 12
scan done 12
scan done 13
scan done 12
scan done 12
scan done 13
 

Attachments

  • demo_new.zip
    7 KB · Views: 418

Michael Wenning

Member
Licensed User
Longtime User
Thank you Manfred!
I have checked your "Demo new" but no succsess:

** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
** Activity (main) Resume **
*** Service (icon) Create ***
** Service (icon) Start **
Wifi scan started!
scan done 0
scan done 0
scan done 0

Verzeichniskonfiguration:

C:\Program Files\Java\jdk-10.0.1\bin\javac.exe

C:\Program Files (x86)\Android\android-sdk\platforms\android-26\android.jar

B4A Vers 8.3

I have tried 3 times with App deinstallation and "Projekt säubern"

I am always asked once for the release and agree.
The runtime permission seem to be okay.
Still no success....

Any idea?

Thanks and regards from Wuppertal
Michael
 
Top