B4J Question Check Internet Connection

Sandman

Expert
Licensed User
Longtime User
In case you're looking to find an existing solution, then you might be out of luck. However, that would be overly narrow-minded, considering concepts and code carry very well over between the different B4X platforms. So find something that makes sense to you in B4i or B4A and use that. Should work just fine, with perhaps some minor adjustments.

By the way: This is also true for many other topics here in the forum, so this is a good thing to get the hang of.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
The most common way is to use HttpUtils to download a page such as google.com.
B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private Label1 As Label
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show 
    Wait For (TestInternet) Complete (Result As String)
    Log("result: " & Result)
    Label1.Text = Result
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub TestInternet()As ResumableSub
    Dim result As String
    Dim Job As HttpJob
    Job.Initialize("Job", Me)
    Job.Download("https://abc.xyz")
    Wait For(Job) JobDone(Job As HttpJob)
    If Job.Success Then
        'result = Job.GetString
        result = "connection success"
    Else
        'result = Job.ErrorMessage
        result = "connection failed"
    End If
    Job.Release
    Return result
End Sub
 

Attachments

  • test.zip
    2.1 KB · Views: 184
Upvote 0

flent

Member
Licensed User
Longtime User
The best way is to NOT check it. Just make the request you need and if it fails then there is no internet connection.

Incorrect strategy, as the request may fail due to the remote server. We need a test that says if our network termination works and is universal (on B4J).

If server.GetMyIP.Length >15 Then
Label1.text = "Internet connection required."
Else
' continue
End If
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
In most cases it is the best strategy. Try to connect and tell the user that the app wasn't able to connect if there are errors.

The check you are making is completely wrong. You are making the assumption that if the device has an IPv6 address it is not connected to the internet.
A device can be connected to a local (without internet) network and have an IPv4 address and a device can be connected to the internet and have an IPv6 address.
A device can also have the localhost (127.0.0.1) address when there is no connection.

If your server is unreliable and you want to show a different error when there is an internet connection but the server is down then make another request to google.com or any other reliable server when the client cannot connect to your server.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…