Android Code Snippet [B4X] Check if Internet interfaces are enabled and internet status + type (WiFi/Cell/LAN)

This project combines two libraries, [B4X] SP Ping and [B4X] IFStatus. The following code shows how to use both libraries.




B4XMainPage:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private ifSts As IFStatus
    Private SPPing As Ping
    Private btnCheck As Button
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    ifSts.Initialize(Me, "SPIFStatus")
    SPPing.Initialize(Me, "SPPing")
End Sub

Private Sub SPPing_PingStarted

End Sub
Private Sub SPPing_PingFinished
    If SPPing.pingResult.Success Then
        Log("--- Ping info ---")
        Log("Ping Success: " & SPPing.pingResult.Success)
        Log("Ping To host: " & SPPing.pingResult.toHost)
        Log("Ping To IP: " & SPPing.pingResult.toIP)
        Log("Ping From host: " & SPPing.pingResult.fromDest)
        For Each bteKey As Byte In SPPing.pingResult.mapResponse.Keys
            Dim seqMap As Map = SPPing.pingResult.mapResponse.Get(bteKey)
            Log("Ping Sequence " & bteKey & " - TTL : " & seqMap.Get("ttl") & " - Time : " & seqMap.Get("time"))
        Next
        Dim stats As Map = SPPing.pingResult.mapStatistics
        Log("Internet connected?: " & SPPing.pingResult.Success)
        Log("Ping Packets transmitted: " & stats.Get("packets transmitted"))
        Log("Ping Packets received: " & stats.Get("received"))
        Log("Ping Packets loss: " & stats.Get("packet loss"))
        Log("Ping Round Trip Time (Minimum): " & stats.Get("rtt min"))
        Log("Ping Round Trip Time (Average): " & stats.Get("rtt agv"))
        Log("Ping Round Trip Time (Maximum): " & stats.Get("rtt max"))
        Log("Ping Round Trip Time (Mean Deviation): " & stats.Get("rtt mdev"))
    End If
End Sub

Private Sub SPPing_PingError(Msg As String)
    Log(Msg)
End Sub

Private Sub SPIFStatus_SocketStarted (Port As Int)

End Sub

Private Sub SPIFStatus_SocketClosed

End Sub

Private Sub SPIFStatus_ConnectionStatusChanged (IPStatus As tpeIPs)
    If ifSts.IPs.IsIfConnected Then
        Log("--- Interface General info ---")
        Log("Interface connected?: " & ifSts.IPs.IsIFConnected)
        Log("Connection type: " & ifSts.IPs.ConnType)
        Log("Local IP: " & ifSts.IPs.IP)
        SPPing.Destination = "google.com"
        SPPing.Attempts = 1
        SPPing.Timeout = 200
        SPPing.Start
    End If
End Sub

Private Sub SPIFStatus_Error (Msg As String)

End Sub

Private Sub btnCheck_Click
    If Not (ifSts.IPs.IsIfConnected) Then
        ifSts.Start(0)
    Else
        ifSts.GetIfChanged(True)
    End If
End Sub

Log result
1719630781390.png
 

Attachments

  • CheckInternet.zip
    12.9 KB · Views: 157
Last edited:

serg992313

Member
I am connected to the internet via an Apple router. Just launched the provided example for B4J and disconnected the LAN internet cable from the router. Unfortunately, I got the result that the internet is still available. Do I need to set any extra parameters to correctly verify that the internet is still available?
 

Sergio Haurat

Active Member
Licensed User
Longtime User
I am connected to the internet via an Apple router. Just launched the provided example for B4J and disconnected the LAN internet cable from the router. Unfortunately, I got the result that the internet is still available. Do I need to set any extra parameters to correctly verify that the internet is still available?
Hello @serg992313, the library does not detect if your device has internet access; it only checks if the "virtual" interface for WiFi and mobile data, or PC network is enabled. In your case, you would need to disable the network adapter, not just disconnect the cable.
For the function you need, I am adding the option to ping an external site to the library. If it fails, it will notify you that the Internet is not available.. The property will be called something like "Is internet available?" and will return true or false.

I hope my response is understandable, I do not speak English.
 
Top