Check Internet connection

Status
Not open for further replies.

robife

Member
Licensed User
Longtime User
Hi guys
I need help to check internet connection is ON or OFF

I try eks.
B4X:
    If CheckConnection Then
           Msgbox("You connected internet", "OK")
    Else
           Msgbox("You didn't connect internet", "Error")
    End If


Sub CheckConnection As Boolean
    Dim p As Phone
    
    If (p.GetDataState == "CONNECTED") Then
        Return True
    End If
        
    If (p.GetSettings ("wifi_on") == 1) Then
        Return True
    End If
    
    If (p.GetDataState == "DISCONNECTED") Then
        Return False
    End If
    
    If (p.GetDataState == "SUSPENDED") Then
        Return False
    End If
End Sub


But how to do it on application with label text ?

or Only to show msg when Internt is OFF, not each time we start app
 

sam_madinah

Member
Licensed User
I've just tested this simple alternative and it seems to work OK for me:

B4X:
Sub Connected As Boolean
    'Requires Phone Library
    Dim p As Phone
    Dim Response, Error As StringBuilder
    Response.Initialize
    Error.Initialize
    'Ping Google DNS - if you can't reach this you are in serious trouble!
    p.Shell("ping -c 1 8.8.8.8",Null,Response,Error)
    Log("======= Response ========")
    Log(Response)
    Log("======= Error ===========")
    Log(Error)
    Log("======================")

    If Error.ToString="" Then
        Return True
    Else
        Return False
    End If

End Sub

I'm not sure if all devices allow the use of the shell command. It worked on my non-rooted Moto G.
I tested in with Aeroplane mode ON and OFF.

Derek


getting this

1 packets transmitted, 0 received, 100% packet loss, time 0ms

in Response

so I guess will look for another solution
 
Upvote 0

sam_madinah

Member
Licensed User
Could you let me know what device is giving this problem and what version of Android is it running on please?

the setup :
I enabled wifi hotspot in my laptop with win 10 ,, my mobile is connected to that wifi,, my laptop is connected to internet via network cable
in this setup,it works well,
then I removed the network cable from laptop,, my mobile is still connected to the wifi (but ofcource the laptop has no internet)
then I got that message

using Samsung note 8 , with Android V9 ,, using b4a 9.5

imo , my mobile did send the packets with no error, but they got dropped somewhere (my laptop in this case, could be any node like router , modem , access point , etc)
 
Upvote 0

Derek Johnson

Active Member
Licensed User
Longtime User
the setup :
I enabled wifi hotspot in my laptop with win 10 ,, my mobile is connected to that wifi,, my laptop is connected to internet via network cable
in this setup,it works well,
then I removed the network cable from laptop,, my mobile is still connected to the wifi (but ofcource the laptop has no internet)
then I got that message

using Samsung note 8 , with Android V9 ,, using b4a 9.5

imo , my mobile did send the packets with no error, but they got dropped somewhere (my laptop in this case, could be any node like router , modem , access point , etc)

So what is the problem? You don't have Internet Access from your device, and that is what the function is telling you. You can't ping Google because there is no Internet connection.
 
Upvote 0

Derek Johnson

Active Member
Licensed User
Longtime User
the code checking
if Error.ToString=""
and in that case
Error.ToString="" = True
and the sub Connected returned True while I am not connected

thats why I wrote
(so I guess will look for another solution)
I can't understand how the code can decide that the error string is empty when there is an error message.
 
Upvote 0
Status
Not open for further replies.
Top