Android Question Checkconnection example for b4a

valentino s

Active Member
Licensed User
Longtime User
I've found in many examples a snippet to check the internet connection.

I'm using it, but yesterday I've found that if I've wifi on but disconnected, the script returns true.

So using serversocket connection I've added another condition.

Actually p.GetSettings ("wifi_on") could detect only if wifi is on, right ?
To check if it is connected I should check serversocket.GetMyWifiIP , as Erel suggested elsewhere in this forum.

Is it correct ? Thanks

B4X:
Sub CheckConnection As Boolean
  Dim p As Phone
  If (p.GetDataState == "CONNECTED") Then
  Return True
  End If

  If (p.GetSettings ("wifi_on") == 1) Then
    Dim s As ServerSocket
     Log(s.GetMyWifiIP)
     If (s.GetMyWifiIP="127.0.0.1") Then
       Return False
     Else
     Return True
     End If
  End If
  
  If (p.GetDataState == "DISCONNECTED") Then
  Return False
  End If
  
  If (p.GetDataState == "SUSPENDED") Then
  Return False
  End If
End Sub
 

valentino s

Active Member
Licensed User
Longtime User
Something like ? :

B4X:
Sub Globals
   Dim job1 As HttpJob
   public ver as string
end sub

Sub checkver
  job1.Download("http://www.iusondemand.it/app/ver.txt")
End Sub

Sub Activity_Create(FirstTime As Boolean)
end sub

Sub JobDone (Job As HttpJob)
  dim ver as string
  If Job.Success = True Then
     Select Job.JobName
     Case "Job1"
       ver=Job.GetString 
     End Select
  Else
      ver=False
  End If
  Job.Release
End Sub

I'm worried just for the timeout: how much I have to wait ? Can I change it ?
 
Upvote 0

ArminKH

Well-Known Member
@Erel as @corwin42 said this code works correct
B4X:
Sub IsConnectedToInternet As Boolean
    Dim r As Reflector
 
    r.Target = r.GetContext
    r.Target = r.RunMethod2("getSystemService", "connectivity", "java.lang.String")
    r.Target = r.RunMethod("getActiveNetworkInfo")
 
    If r.Target <> Null Then
        Return r.RunMethod("isConnectedOrConnecting")
    End If
 
    Return False
End Sub

note.you need to add AddPermission(android.permission.ACCESS_NETWORK_STATE) to your manifest
 
Upvote 0

valentino s

Active Member
Licensed User
Longtime User
Reading the guide makes me think. Downloading a video (big file) the connection can change or fall down.

If the connection falls down I suppose that I'll receive an error from HttpJob.

If the connection changes (from wifi to 3g for example) I probably don't / can't receive an event to decide to stop the download already started, for example to allow downloading a big file only from wifi.

Is there a workaround or a canonical solution ? Thanks in advance.

The workaround could be using a timer and check the type of connection repeatly during a download, isn't it ?
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Reading the guide makes me think
he workaround could be using a timer and check the type of connection repeatly during a download, isn't it ?

In that page:
Monitor for Changes in Connectivity
The ConnectivityManager broadcasts the CONNECTIVITY_ACTION ("android.net.conn.CONNECTIVITY_CHANGE") action whenever the connectivity details have changed. You can register a broadcast receiver in your manifest to listen for these changes and resume (or suspend) your background updates accordingly.


<actionandroid:name="android.net.conn.CONNECTIVITY_CHANGE"/>
Changes to a device's connectivity can be very frequent—this broadcast is triggered every time you move between mobile data and Wi-Fi. As a result, it's good practice to monitor this broadcast only when you've previously suspended updates or downloads in order to resume them. It's generally sufficient to simply check for Internet connectivity before beginning an update and, should there be none, suspend further updates until connectivity is restored.



Some Guru could implement b4a versions of all those "functions"
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Just FYI, my Google Play Services wrapper contains a class to handle connections and return the network properties (GPlayNetworkInfo). It's more complete than PhoneEvents. The Java source code is included. A library developer could extract this class to create a specific library (very easy as my class is independent from the other classes).
 
Upvote 0
Top