B4A Library [B4X] SP IFStatus - Check if interface status (WiFi, cell network data or LAN) has enabled or disabled

  • IFStatus
    • Events
      • SocketStarted (Port As Int)
        Triggers when the socket starts
      • SocketClosed
        Triggers when socket closes
      • ConnectionStatusChanged (IPStatus As tpeIPs)
        Triggers when there is a change in the connection. Returns a tpeIPs type variable. See details below
      • Error (Msg As String)
        Trigger when an error occurs, they can be the following:
        • Socket not initialized.
        • The IP format is not valid. Includes IPv4 or IPv6
        • Can't initialize socket.
        • Can't initialize socket on port NUM_OF_PORT
        • Socket is already initialized on port NUM_OF_PORT
    • Types
      • tpeIPs (IsConnected As Boolean, ConnType As String, IP As String)
        • IsConnected: True or False
        • ConnType: NONE (B4X), WIFI (B4A/B4I), CELL (B4A/B4I) or LAN (B4J)
        • IP: Valid IPv4 or IPv6
    • Fields
      • Tag As Object
        Default: Null (It is always valuable to have a Tag field ;))
      • IPs As tpeIPs
        Default: IsConnected = False, ConnType = "NONE", IP: "127.0.0.1"
      • blnIPv4IsValid As Boolean
        Default: false
      • blnIPv6IsValid As Boolean
        Default: false
    • Methods
      • Initialize (CallBack As Object, EventName As String)
        Initializes the object. Add parameters to this method.
      • Start (Port As Int)
        Start the socket on the desired port
        IMPORTANT: The number can be 0 or one greater than or equal to 1025 up to 65535.
        Search on
        this list of ports for one that is available and does not conflict with another service
      • GetIfChanged (ForceRaiseStatus As Boolean)
        Update IPs variable
        Default: False. If ForceRaiseStatus is True, raise event ConnectionStatusChanged whether the IP has changed or not
      • Close
        Close socket
Change log:
  • 1.00
    • Release
B4XMainPage:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private ifSts As IFStatus
    Private btnCheck As Button
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    ifSts.Initialize(Me, "SPIFStatus")
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)
    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

The result in the log
1719518009722.png
 

Attachments

  • IFStatus.b4xlib
    2.1 KB · Views: 370
Last edited:

Oscarin

Member
  • IFStatus
    • Events
      • SocketStarted (Port As Int)
        Triggers when the socket starts
      • SocketClosed
        Triggers when socket closes
      • ConnectionStatusChanged (IPStatus As tpeIPs)
        Triggers when there is a change in the connection. Returns a tpeIPs type variable. See details below
      • Error (Msg As String)
        Trigger when an error occurs, they can be the following:
        • Socket not initialized.
        • The IP format is not valid. Includes IPv4 or IPv6
        • Can't initialize socket.
        • Can't initialize socket on port NUM_OF_PORT
        • Socket is already initialized on port NUM_OF_PORT
    • Types
      • tpeIPs (IsConnected As Boolean, ConnType As String, IP As String)
        • IsConnected: True or False
        • ConnType: NONE (B4X), WIFI (B4A/B4I), CELL (B4A/B4I) or LAN (B4J)
        • IP: Valid IPv4 or IPv6
    • Fields
      • Tag As Object
        Default: Null (It is always valuable to have a Tag field ;))
      • IPs As tpeIPs
        Default: IsConnected = False, ConnType = "NONE", IP: "127.0.0.1"
      • blnIPv4IsValid As Boolean
        Default: false
      • blnIPv6IsValid As Boolean
        Default: false
    • Methods
      • Initialize (CallBack As Object, EventName As String)
        Initializes the object. Add parameters to this method.
      • Start (Port As Int)
        Start the socket on the desired port
        IMPORTANT: The number can be 0 or one greater than or equal to 1025 up to 65535.
        Search on
        this list of ports for one that is available and does not conflict with another service
      • GetIfChanged (ForceRaiseStatus As Boolean)
        Update IPs variable
        Default: False. If ForceRaiseStatus is True, raise event ConnectionStatusChanged whether the IP has changed or not
      • Close
        Close socket
Change log:
  • 1.00
    • Release
B4XMainPage:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private ifSts As IFStatus
    Private btnCheck As Button
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    ifSts.Initialize(Me, "SPIFStatus")
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)
    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

The result in the log
View attachment 154973
Hi, I am getting this error

B4XMainPage - 23: Unknown type: ifstatus<br />Are you missing a library reference?
I already copied the library to my library folder also copied to the project folder\B4A\Objects\b4xlibs\Files
and still getting the error

Thanks for any help
 
  • IFStatus
    • Events
      • SocketStarted (Port As Int)
        Triggers when the socket starts
      • SocketClosed
        Triggers when socket closes
      • ConnectionStatusChanged (IPStatus As tpeIPs)
        Triggers when there is a change in the connection. Returns a tpeIPs type variable. See details below
      • Error (Msg As String)
        Trigger when an error occurs, they can be the following:
        • Socket not initialized.
        • The IP format is not valid. Includes IPv4 or IPv6
        • Can't initialize socket.
        • Can't initialize socket on port NUM_OF_PORT
        • Socket is already initialized on port NUM_OF_PORT
    • Types
      • tpeIPs (IsConnected As Boolean, ConnType As String, IP As String)
        • IsConnected: True or False
        • ConnType: NONE (B4X), WIFI (B4A/B4I), CELL (B4A/B4I) or LAN (B4J)
        • IP: Valid IPv4 or IPv6
    • Fields
      • Tag As Object
        Default: Null (It is always valuable to have a Tag field ;))
      • IPs As tpeIPs
        Default: IsConnected = False, ConnType = "NONE", IP: "127.0.0.1"
      • blnIPv4IsValid As Boolean
        Default: false
      • blnIPv6IsValid As Boolean
        Default: false
    • Methods
      • Initialize (CallBack As Object, EventName As String)
        Initializes the object. Add parameters to this method.
      • Start (Port As Int)
        Start the socket on the desired port
        IMPORTANT: The number can be 0 or one greater than or equal to 1025 up to 65535.
        Search on
        this list of ports for one that is available and does not conflict with another service
      • GetIfChanged (ForceRaiseStatus As Boolean)
        Update IPs variable
        Default: False. If ForceRaiseStatus is True, raise event ConnectionStatusChanged whether the IP has changed or not
      • Close
        Close socket
Change log:
  • 1.00
    • Release
B4XMainPage:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private ifSts As IFStatus
    Private btnCheck As Button
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    ifSts.Initialize(Me, "SPIFStatus")
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)
    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

The result in the log
View attachment 154973
In release mode, it works as long as wifi or cellular connection is there . However, once there is no connection, it shows :
Connection:NONE
Local IP:127.0.0.1
After that, even if I connect to wifi or cellular, the status does not change. It still shows "NONE"
I use the following routine to see the connection. What should I modify to see the connections again?:
B4X:
Private Sub btnCheck_Click
    If Not (ifSts.IPs.IsIfConnected) Then
        ifSts.Start(0)
    Else
        ifSts.GetIfChanged(True)
        lbl_type.Text = "Connection: " & ifSts.IPs.ConnType & CRLF & "Local IP: " & ifSts.IPs.IP
    End If
End Sub
 
Top