Check internet connection. Worked on Android 2.3.X but not on 4.0.3

susu

Well-Known Member
Licensed User
Longtime User
Hi guys, I used this code to check internet connection. It worked correctly on Android 2.3.X but on 4.0.3 it's always said there's no internet connection. How can I fix it? Thank you :sign0188:

B4X:
Sub Activity_Create(FirstTime As Boolean)
   
    If CheckConnection Then
           Msgbox("You connected internet", "OK")
    Else
           Msgbox("You didn't connect internet", "Error")
    End If

End Sub


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
 
Last edited:

susu

Well-Known Member
Licensed User
Longtime User
Yes, I can see the ad banners but this code said there's no internet connection.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
@susu: I tested your code on a 7 inch tablet running OS 4.03 and a phone running OS4.04. Your code worked very well on both, if this is any consolation.
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
Hi Mahares, could you please let me know the name of your device? I tested this code on Asus Transfomer TF300 Android 4.0.3 and it didn't work correctly.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
@susu: I tested it on a 7 inch Aluratek tablet AT107F with OS4.03, not a household name but a good tablet, both with the internet on and also without internet. Both times it works. I also tested it on a Droid Razr smarthphone with OS4.04 and it worked.
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
Thank you Mahares and JoanRPM. It seems this issue only happen on Asus Transformer TF300.
I just wonder, do we have another way to check internet connection?
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
Oh, that is nice trick. I'll do it. Thanks Erel.
 
Upvote 0

JakeBullet70

Well-Known Member
Licensed User
Longtime User
I use this.

B4X:
Public Sub Check4InetConnection() As Boolean
    Dim oSSocket As ServerSocket
    Dim ip As String = oSSocket.GetMyWifiIP  'ignore
    If ip = "127.0.0.1" Then
        Return False
    Else
        Return True
    End If
   
End Sub

Is there a drawback to this method?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You mean he should try to load it into a WebView?
No. The best way to check for a working internet connection is by sending a http request. If Job.Success is true then there is an internet connection.

Is there a drawback to this method?
This is a good check. However it doesn't really check for a working internet connection. The device can be connected to an access point that is not connected to the internet for example.
 
Upvote 0

hypergreatthing

Member
Licensed User
Longtime User
No. The best way to check for a working internet connection is by sending a http request. If Job.Success is true then there is an internet connection.


This is a good check. However it doesn't really check for a working internet connection. The device can be connected to an access point that is not connected to the internet for example.
I actually ran into a problem with this method. In captive portals or firewalls that require authentication, you'll get a job successful reply, but the data downloaded will be an error message instead.
 
Upvote 0
Top