SOLUTION HERE
I have seen in the forum some ways to know if you are connected or not to Internet.
Example: https://www.b4x.com/android/forum/threads/b4x-b4xlib-b4xcheckinternetlm.133628/post-848997
The problem is... I don't know if the connection is WiFi or cellular network. If I need to download an update of many records from a database, a video file or whatever but the size is considerably large, I must first ask the user if they want to download it.
This is my code.
The B4I code is a suggestion from ChatGPT, but I'm not doing something right or ChatGPT is lying to me
Another suggestion from ChatGPT but it's strange
At the end of the code leave the following comment:
[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. https://www.b4x.com/android/forum/threads/b4x-sp-ping-run-the-ping-command-from-b4a-b4j-and-b4i.161861/...
www.b4x.com
I have seen in the forum some ways to know if you are connected or not to Internet.
Example: https://www.b4x.com/android/forum/threads/b4x-b4xlib-b4xcheckinternetlm.133628/post-848997
The problem is... I don't know if the connection is WiFi or cellular network. If I need to download an update of many records from a database, a video file or whatever but the size is considerably large, I must first ask the user if they want to download it.
This is my code.
CheckInternet:
Sub Process_Globals
Type tpeCheckInternet(blnIsOnline As Boolean, strType As String)
End Sub
Public Sub CheckInternet() As tpeCheckInternet
Dim ci As tpeCheckInternet
ci.Initialize
ci.strType = "Unknown"
#if b4a
Dim jo As JavaObject
jo.InitializeContext
Dim cm As JavaObject = jo.RunMethod("getSystemService", Array("connectivity"))
Dim activeNetwork As JavaObject = cm.RunMethod("getActiveNetworkInfo", Null)
If activeNetwork.IsInitialized = False Then
ci.blnIsOnline = False
Else
If activeNetwork.RunMethod("isConnected", Null).As (Boolean) = True Then
If activeNetwork.RunMethod("getType", Null).As (Int) = 1 Then
ci.blnIsOnline = True
ci.strType = "WiFi"
Else
ci.blnIsOnline = True
ci.strType = "Cell"
End If
Else
ci.blnIsOnline = False
End If
End If
#Else If b4i
Dim n As Network
If n.GetCurrentType = n.TYPE_WIFI Then
ci.blnIsOnline = True
ci.strType = "WiFi"
Else If n.GetCurrentType = n.TYPE_MOBILE Then
ci.blnIsOnline = True
ci.strType = "Cell"
End If
#End If
Return ci
End Sub
The B4I code is a suggestion from ChatGPT, but I'm not doing something right or ChatGPT is lying to me
Another suggestion from ChatGPT but it's strange
B4X:
Sub Process_Globals
' These global variables will be declared once when the application starts.
Dim no As NativeObject
End Sub
Sub Globals
' These global variables will be redeclared each time the activity is created.
Dim cm As ConnectivityManager
End Sub
Sub Activity_Create(FirstTime As Boolean)
' Do not forget to load the layout file created with the visual designer. For example:
' Activity.LoadLayout("Layout1")
no.InitializeNewInstance("android.content.ContextWrapper", Array As Object(Activity))
cm = no.RunMethod("getSystemService:", Array("connectivity"))
CheckInternetConnection
End Sub
Sub CheckInternetConnection
Dim isConnected As Boolean
isConnected = IsConnectedToInternet
If isConnected Then
Log("Internet connection is available.")
Dim connectionType As String = GetConnectionType
If connectionType = "WIFI" Then
Log("Connection type: WiFi.")
Else If connectionType = "MOBILE" Then
Log("Connection type: Cellular network.")
Else
Log("Unknown connection type: " & connectionType)
End If
Else
Log("No internet connection.")
End If
End Sub
Sub IsConnectedToInternet As Boolean
Dim ni As NativeObject
ni.InitializeNewInstance("java.net.NetworkInterface", Null)
Dim interfaces As List = ni.RunMethod("getNetworkInterfaces", Null)
For i = 0 To interfaces.Size - 1
Dim networkInterface As NativeObject = interfaces.Get(i)
Dim interfaceName As String = networkInterface.GetField("name").AsString
If interfaceName.StartsWith("pdp_ip") Or interfaceName.StartsWith("rmnet") Then
Return True ' Cellular network is connected
End If
Next
Return cm.RunMethod("getActiveNetworkInfo", Null).IsInitialized
End Sub
Sub GetConnectionType As String
Dim activeNetwork As NativeObject = cm.RunMethod("getActiveNetworkInfo", Null)
If activeNetwork.IsInitialized Then
If activeNetwork.GetField("getType") = 1 Then
Return "WIFI"
Else
Return "MOBILE"
End If
Else
Return ""
End If
End Sub
At the end of the code leave the following comment:
This code utilizes NativeObject to interact with Android's ConnectivityManager to check for internet connectivity and determine the type of connection.
Make sure to handle permissions in your Info.plist file to allow network access.
- IsConnectedToInternet method checks if there is any active network interface, which indicates internet connectivity.
- GetConnectionType method determines whether the active network is WiFi or mobile data.
Last edited: