B4A Library WaitForInternet - Best way to check the internet connection

WFITop.png


This library was inspired and based on WaitForTheInternet by César Pérez
https://github.com/ouvigna/WaitForTheInternet

Your application will be able to get through an event, as it connects to the internet via Wifi or 3G, without the need to be checking all the time, not consuming battery unnecessarily.

Installation instructions:
- Copy the .xml and .jar(s) files to your custom libraries folder
- Select in your REFERENCED LIBRARIES



Version history:
V1.0.0 (First public release - Donationware):
- Initial version


WaitForInternet
Author:
Alberto Iglesias - alberto@visualnet.inf.br
Version: 1.0.0
  • WaitForInternet
    Events:
    • onInitialized ()
    • onReceived (Action As String, WifiAvailable As Boolean, WifiConnected As Boolean, WifiRoaming as Boolean As , MobileAvailable As Boolean, MobileConnected As Boolean, MobileRoaming as Boolean, ExtraNetworkInfo As String, HostFound as Boolean)
    Fields:
    • JavaDebug As Boolean
    • PingHost As String
    • PingHostOnReceive As Boolean
    • PingResult As String
    Methods:
    • About As String
    • ForcePing As Boolean
    • Initialize (eventname As String)
    • Version As String
    • isConnectedToInternet As Boolean
    Permissions:
    • android.permission.ACCESS_NETWORK_STATE
    • android.permission.ACCESS_WIFI_STATE
    • android.permission.INTERNET



* In the Full Version, you have a double check for then internet connection over the ping method to bypass Captive Portals and have a ForcePing Method to check the host connect in any time you want.
Please consider a simple €10 donation and I send to you the Library with this features.

Check all Libraries in: http://vnsoft.es/store/
 

Attachments

  • Example1.zip
    13.2 KB · Views: 734
  • WaitForInternet101.zip
    6.4 KB · Views: 676
  • WaitForTheInternet102Demo.zip
    6.5 KB · Views: 551
Last edited:

JakeBullet70

Well-Known Member
Licensed User
Longtime User
This will not compile if your MQTT lib is in the same project.

B4A version: 5.20
Parsing code. (0.69s)
Compiling code. (1.72s)
Compiling layouts code. (0.38s)
Generating R file. (0.06s)
Compiling generated Java code. (9.24s)
Convert byte code - optimized dex. Error
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Lcom/visualnet/mqtt;
at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:122)
at com.android.dx.dex.file.DexFile.add(DexFile.java:161)
at com.android.dx.command.dexer.Main.processClass(Main.java:732)
at com.android.dx.command.dexer.Main.processFileBytes(Main.java:673)
at com.android.dx.command.dexer.Main.access$300(Main.java:83)
at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:602)
at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284)
at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166)
at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)
at com.android.dx.command.dexer.Main.processOne(Main.java:632)
at com.android.dx.command.dexer.Main.processAllFiles(Main.java:510)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:280)
at com.android.dx.command.dexer.Main.run(Main.java:246)
at com.android.dx.command.dexer.Main.main(Main.java:215)
at com.android.dx.command.Main.main(Main.java:106)
warning: Ignoring InnerClasses attribute for an anonymous inner class
(org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence$1) that doesn't come with an
associated EnclosingMethod attribute. This class was probably produced by a
compiler that did not target the modern .class file format. The recommended
solution is to recompile the class from source, using an up-to-date compiler
and without specifying any "-target" type options. The consequence of ignoring
this warning is that reflective operations on this class will incorrectly
indicate that it is *not* an inner class.
warning: Ignoring InnerClasses attribute for an anonymous inner class
(org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence$2) that doesn't come with an
associated EnclosingMethod attribute. This class was probably produced by a
compiler that did not target the modern .class file format. The recommended
solution is to recompile the class from source, using an up-to-date compiler
and without specifying any "-target" type options. The consequence of ignoring
this warning is that reflective operations on this class will incorrectly
indicate that it is *not* an inner class.
1 error; aborting
 

JakeBullet70

Well-Known Member
Licensed User
Longtime User
Also @Alberto Iglesias I have in the past done something like this. As my apps run 24/7... I did after 3 months start to get warnings from Google about 'abnormal traffic' for our web browsers. We had to type in a code to continue a basic search. I fixed it by rotating the pings to use google, yahoo and MS. You might want to add this option.
 

JakeBullet70

Well-Known Member
Licensed User
Longtime User
Hi @Alberto Iglesias

A couple things I noticed,
1. Mobile/3G is showing as turned on. This is on a tablet that does not have 3G
2. If I turn off the Wifi on the tablet Every thing looks go on your sample project except 'Connected' still says True.
3. If I unplug my router the sample app still shows everything as connected.

Thanks.
 

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
Hello Jake,

1. This behavior is the broadcast itself which receives android is ConnectivityManager according to the object, and in some devices such as Samsung for example, occur differently
2. the same....
3. Yes, because the app is connected to wifi (not because the internet), thinking is this I create the Ping method to check in some external website.
 

avrtech

Member
Licensed User
Hello

The library works perfectly with one exception ... when a device such as my tablet does not have a mobile / 3G ... Library causes an error in the operation of the program for android 5.0.1 and then the application is closed ... when I stop using the library WaitForInternet program starts .

I conclude that the library checks whether Mobile / 3G or Wi-Fi is enabled or not but if your Android device does not have a mobile / 3G application stops working.

Is it possible to add to the library to check whether it has the SIM if it does not have to think it has not confirmed that such a procedure would eliminate the error.

Regards
 

gregbug

Member
Licensed User
Longtime User
first thanks to Alberto Iglesias for this great library i use it in my latest app (Tora Bora Radio Player)
but as said by avrtech i had the same error if used in a device that not have the 3g module...

here is error details...

B4X:
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x4000010 (has extras) } in com.visualnet.waitforinternet$1@3fb09c3c
    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:877)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5306)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.net.NetworkInfo.isAvailable()' on a null object reference
    at com.visualnet.waitforinternet$1.onReceive(waitforinternet.java:176)
    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:867)
    ... 8 more

as reported from google dev console...

@Alberto Iglesias can you look at this issue ?
thanks again for your library.
Gianluca.
 

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
Gianluca, I try now in a device without 3G and all is working, the device I tried was: CMP4708T (http://www.tablet-user.fr/test/carrefour-cmp4708t--autonom.html)

I need to fix this issue , I need to know if is your device of android version or library ok?

Put the property in a library JavaDebug = TRUE

and try again, and send to me the B4A log.

If you can send to me a simple sample to replicate this issue......

My log in a sample project with this device
** Service (pushservice) Create **
** Service (pushservice) Start **
[PushService] = (Intent) Intent { act=android.intent.action.BOOT_COMPLETED flg=0x10 cmp=com.visualnet.wemo/.pushservice$pushservice_BR }
** Activity (main) Create, isFirst = true **JAVA [Initialize] eventname = objWFI
[objWFI_onInitialized]
[isConnectedToInternet]true
** Activity (main) Resume **
Network WIFI connected
WifiAvailable:true
WifiConnected:true
BroadcastReceiver::eek:nReceive::android.net.conn.CONNECTIVITY_CHANGE
[objWFI_OnReceived]Action:android.net.conn.CONNECTIVITY_CHANGE
[objWFI_OnReceived]WifiAvailable:true
[objWFI_OnReceived]WifiAvailable:true
[objWFI_OnReceived]WifiRoaming:false
[objWFI_OnReceived]MobileAvailable:false
[objWFI_OnReceived]MobileConnected:false
[objWFI_OnReceived]MobileRoaming:false
[objWFI_OnReceived]ExtraNetworkInfo:Network WIFI connected
[objWFI_OnReceived]Host Found:true (247.439)
[isConnectedToInternet]true
[objWFI_OnReceived]Force Ping:true
Ping Host:www.google.com
Ping:0
** Activity (main) Pause, UserClosed = false **


Thanks

Alberto iglesias
 

gregbug

Member
Licensed User
Longtime User
Thanks Alberto for your time,

i'm not at home now i'll try tonight as soon as i'll go home..,
and send you the logs.. ok?

can you try to download (from play store searh for "Tora Bora Radio player") my app and see if crash?
my device is Samsung P5110 Tablet no 3g. (android 5.1.1 CandyROM)
your lib is 1.0.1

thanks again,
Gianluca.
 
Last edited:

gregbug

Member
Licensed User
Longtime User
ok.

here the detailed b4a log error (JavaDebug = True)

B4X:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (playerservice) Create **
JAVA [Initialize] eventname = onInternet
** Service (playerservice) Start **
WifiAvailable:true
Ping not available in Demo Version.** DONATIONWARE **
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x4000010 (has extras) } in com.visualnet.waitforinternet$1@3a969967
    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:877)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5306)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.net.NetworkInfo.isAvailable()' on a null object reference
    at com.visualnet.waitforinternet$1.onReceive(waitforinternet.java:176)
    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:867)
    ... 8 more

thanks for your time,
Gianluca.
 

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
OK. I tried and no errors!

Can be something about android version when check NetworkInfo.isAvaiable()

You can download in this page: Get the Version 1.0.2

Check all libraries in http://vnsoft.es/store/

In this 1.0.2 version I put a Try/Catch Error in a null point exception when is 3g, you can try and tell me please if works?

Thanks
 
Last edited:

gregbug

Member
Licensed User
Longtime User
ok.!

just tested with new version (1.0.2) and works perfect!!!

buying full version now:)
thanks again for your time!

Gianluca.
 

Rusty

Well-Known Member
Licensed User
Longtime User
Alberto,
Looks like you've been busy :) I appreciated your help with the FLAC files.
I'm trying to use your (free version) library and when I turn off WiFi, both Mobile/3G and WIFI show Connected false. When I turn the WIFI back on, both show Connected true.
However, my Mobile/3G (really 4G), is always on.
Is there a reason that both toggle with the WIFI turning ON and OFF? I'd think if the Mobile is on and there is a cell signal, that it would indicate connected regardless of the WIFI status.
Regards,
Rusty

I'm using a Samsung S7; Android Version 7.0; B4a Version 6.80; version 1.02 of your library
 

Rusty

Well-Known Member
Licensed User
Longtime User
Thanks Alberto,
I'm not clear.
I have cellular ON (my phone is 4G, does this matter?); I turn WIFI OFF and both toggle to connected FALSE.
When I turn the WIFI back ON, both toggle to connected TRUE.
Shouldn't only WIFI toggle to False and back to True while the Cellular should remain connected TRUE?
Please explain, I'm don't understand the relationship between the cellular and WIFI connection...
Regards,
Rusty
 

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
ohhh I think i know where is the problem....

check the sample I create:

where I put this

B4X:
chkWifi1.Checked = WifiAvailable
chkWifi2.Checked = WifiConnected
chkWifi3.Checked = WifiRoaming

chkMobile1.Checked = WifiAvailable
chkMobile2.Checked = WifiConnected
chkMobile3.Checked = WifiRoaming

change for this

B4X:
chkWifi1.Checked = WifiAvailable
chkWifi2.Checked = WifiConnected
chkWifi3.Checked = WifiRoaming

chkMobile1.Checked = MobileAvailable
chkMobile2.Checked = MobileConnected
chkMobile3.Checked = MobileRoaming

Sorry, I will fix the sample code and update this post.
 

Rusty

Well-Known Member
Licensed User
Longtime User
Perfect!
(also, in your designer you have misspelled Connected with Conneted...no problem, just thought you should know :) )
Thanks for the help!
Can you briefly describe what the PING function of the paid version improves for functionality? i.e. What would someone do with the PING function?
Rusty
 
Top