Android Code Snippet Check Internet connection + Know connection type (WiFi/Cell)

SOLUTION HERE


Please do not use the following code in your projects.

Below I share a function that returns if the Internet connection exists and what type of connection is available.

Warning: @drgottjr wrote to me: The classes and methods used for b4a have all been deprecated for some time (since sdk 28/29)

[CODE lang="b4x" title="Code Module"]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 = "NONE"
ci.blnIsOnline = False
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 Then
If activeNetwork.RunMethod("isConnected", Null).As (Boolean) = True Then
ci.blnIsOnline = True
If activeNetwork.RunMethod("getType", Null).As (Int) = 1 Then
ci.strType = "WIFI"
Else
ci.strType = "CELL"
End If
End If
End If
Return ci
End Sub[/CODE]

[CODE lang="b4x" title="Usage Example Code"]Sub Class_Globals
Dim tpeInet As tpeCheckInternet
End Sub

Public Sub Initialize
tpeInet.Initialize
End Sub

Private Sub B4XPage_Appear
'Check if the connection is active
If tpeInet.blnIsOnline Then
If tpeInet.strType = "WIFI" Then
'Download the large file automatically
Else
'Wait for user approval to use cellular network data
End If
Else
'No internet connection available
End If
End Sub[/CODE]
 
Last edited:
I could somehow "force" to use a type of connection in case both are connected, currently I have a problem with an application that if the cellular connection is active takes the ip of this network and not the wifi network that is the ip I need to take.
 
I could somehow "force" to use a type of connection in case both are connected, currently I have a problem with an application that if the cellular connection is active takes the ip of this network and not the wifi network that is the ip I need to take.
Hello @enrfortin, if you want to activate WiFi or those types of functions, I believe I have read in the forum that Android is becoming more and more restrictive and does not allow it.
See this post:


My function just tries to find out if there is a connection and what type. I use it BEFORE to send GET or POST queries to an API, I leave you the example.

[CODE lang="b4x" title="GetJSONData" highlight="3"]Public Sub GetJSONData(strLink As String) As ResumableSub
Dim Result As String
If mdlFunctions.CheckInternet.blnIsOnline Then
Dim j As HttpJob
Try
j.Initialize("", Me)
j.Download(strLink)
j.GetRequest.SetHeader("Content-Type","application/json")
If mdlFunctions.mapToken.IsInitialized And mdlFunctions.mapToken.Size > 0 Then
j.GetRequest.SetHeader("UserId", mdlFunctions.mapToken.Get("id"))
j.GetRequest.SetHeader("UniqueKey", mdlFunctions.mapToken.Get("key"))
j.GetRequest.SetHeader("Authorization", mdlFunctions.mapToken.Get("token"))
End If
If mdlFunctions.mapHeaders.IsInitialized And mdlFunctions.mapHeaders.Size > 0 Then
For Each strKey As String In mdlFunctions.mapHeaders.Keys
j.GetRequest.SetHeader(strKey, mdlFunctions.mapHeaders.Get(strKey))
Next
End If
j.GetRequest.Timeout = 60000
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Result = j.GetString
Else
Result = j.ErrorMessage
End If
Catch
Result = LastException.Message
Log(LastException)
End Try
j.Release
End If
Return Result
End Sub[/CODE]

[CODE lang="b4x" title="PostJSONData" highlight="3"]Public Sub PostJSONData(strRun As String, mapData As Map) As ResumableSub
Dim Result As String
If mdlFunctions.CheckInternet.blnIsOnline Then
Dim JSONGenerator As JSONGenerator
Dim JSONString As String
JSONGenerator.Initialize(mapData)
Try
Dim j As HttpJob
j.Initialize("", Me)
JSONString = JSONGenerator.ToString
j.PostString(mdlFunctions.strRemoteHost & strRun, JSONString)
j.GetRequest.SetContentType("application/json")
If mdlFunctions.mapToken.IsInitialized And mdlFunctions.mapToken.Size > 0 Then
j.GetRequest.SetHeader("UserId", mdlFunctions.mapToken.Get("id"))
j.GetRequest.SetHeader("UniqueKey", mdlFunctions.mapToken.Get("key"))
j.GetRequest.SetHeader("Authorization", mdlFunctions.mapToken.Get("token"))
End If
If mdlFunctions.mapHeaders.IsInitialized And mdlFunctions.mapHeaders.Size > 0 Then
For Each strKey As String In mdlFunctions.mapHeaders.Keys
j.GetRequest.SetHeader(strKey, mdlFunctions.mapHeaders.Get(strKey))
Next
End If
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Result = j.GetString
Else
Result = j.ErrorMessage
End If
Catch
Result = LastException.Message
Log(LastException)
End Try
j.Release
End If
Return Result
End Sub[/CODE]

I have some variables of type Map (mapToken and mapHeaders), which I fill with the information that is required, both in the GET and in POST.

As you can see on line 3 in both cases, check beforehand, is there internet?

In the following example code, I query for the current version of the database

[CODE lang="b4x" title="Example to load DB"]Public Sub LoadDB(strTable As String) As ResumableSub
Dim strURL As String = ""
Select Case strTable
Case "get_db_info"
strURL = mdlFunctions.strRemoteHost & "db_info"
End Select
Wait For(clsFunct.GetJSONData(strURL)) Complete (strResult As String)
Return strResult
End Sub[/CODE]

I made the code prior to paying the $99 from Apple and @Erel's service to be able to remotely compile the application. Now I find myself with these issues that should be simple but in reality, they are not.
 
I could somehow "force" to use a type of connection in case both are connected, currently I have a problem with an application that if the cellular connection is active takes the ip of this network and not the wifi network that is the ip I need to take.
Try this library @enrfortin

 
Back
Top