#Region Internet Connection
Sub Test_Internet_Connection()
Dim p As Phone
Dim overall_data_state As Int
Dim cellular_data_state As Int
'0 Disabled - 1 Enabled. May also return a 3 in enabled state but I can find no explanation of the return values
'Note: Turning on airplane mode is not the same as disabling wifi
Dim wifi As Int = p.GetSettings ("wifi_on")
'Msgbox("Wi-Fi value is: " & wifi, "")
Dim airplane_mode As Int = p.GetSettings ("airplane_mode_on")
'Msgbox("Airplane mode value is: " & airplane_mode, "")
'Test the Cellular State
Select Case p.GetDataState
Case "CONNECTED"
cellular_data_state = 1
Case Else
'Captures the other possible states of "SUSPENDED" "DISCONNECTED" "CONNECTING"
cellular_data_state = 0
End Select
'Check to see if airplane_mode is enabled. 1 means device is in airplane_mode, 0 means device is not in airplane_mode
If airplane_mode = 1 Then
'We know for sure the internet is disconnected
overall_data_state = 0
Else
'Otherwise we know that the device is not in airplane_mode so check wifi and cellular
If wifi = 0 AND cellular_data_state = 0 Then
'Both the cellular and WiFi are disabled so we cannot have a connection (except some kind of tethering over blue-tooth)
overall_data_state = 0
Else
'Either Wifi or cellular is active so it is possible to have a connection
overall_data_state = 1
End If
End If
'Test the connection if it is possible to have one
If overall_data_state = 1 Then
'Initially indicate that we do have a connection and the screen label will probably be correct.
'We do this because of the potential latency in determining when a connection is not available.
Internet_Connected(True)
'Now we will test the connection and change it back to False if it fails
Dim job1 As HttpJob
job1.Initialize("Job1", Me)
'Send a GET request
job1.Download("http://www.google.com")
'Once this completes the JobDone routine is executed and the labels are set to their true state.
Else
Internet_Connected(False)
End If
End Sub
Sub JobDone (job As HttpJob )
If job.Success = True Then
Internet_Connected(True)
Else
Internet_Connected(False)
End If
job.Release
End Sub
Sub Internet_Connected (Flag As Boolean)
If Flag = True Then
InternetConnected = True
lblInternetConnected.Text = Chr(ConnectedIcon)
lblInternetConnected.TextColor = Colors.White
Else
InternetConnected = False
lblInternetConnected.text = Chr(DisconnectedIcon)
lblInternetConnected.TextColor = Colors.Red
End If
End Sub
#End Region
Just for information: with my french Internet/phone provider (Free), I have a very fast ping to its own servers or to the Google servers, even when I'm unable to connect to everything else. I discovered this lately. I was totally unable to connect to many web sites where I was, while Google servers and Free servers were very responsive. So doing a test to reach Google.com learns me nothing about the ability to reach another server.I was not happy with the things I found in regards to this so I wrote my own which tests the device to see if it is possible given the current configuration. If it is possible than it actually tests by pulling down a page. I prefer to do both because of the latency in timing out when you don't have a connection which can be minimized.
B4X:#Region Internet Connection Sub Test_Internet_Connection() Dim p As Phone Dim overall_data_state As Int Dim cellular_data_state As Int '0 Disabled - 1 Enabled. May also return a 3 in enabled state but I can find no explanation of the return values 'Note: Turning on airplane mode is not the same as disabling wifi Dim wifi As Int = p.GetSettings ("wifi_on") 'Msgbox("Wi-Fi value is: " & wifi, "") Dim airplane_mode As Int = p.GetSettings ("airplane_mode_on") 'Msgbox("Airplane mode value is: " & airplane_mode, "") 'Test the Cellular State Select Case p.GetDataState Case "CONNECTED" cellular_data_state = 1 Case Else 'Captures the other possible states of "SUSPENDED" "DISCONNECTED" "CONNECTING" cellular_data_state = 0 End Select 'Check to see if airplane_mode is enabled. 1 means device is in airplane_mode, 0 means device is not in airplane_mode If airplane_mode = 1 Then 'We know for sure the internet is disconnected overall_data_state = 0 Else 'Otherwise we know that the device is not in airplane_mode so check wifi and cellular If wifi = 0 AND cellular_data_state = 0 Then 'Both the cellular and WiFi are disabled so we cannot have a connection (except some kind of tethering over blue-tooth) overall_data_state = 0 Else 'Either Wifi or cellular is active so it is possible to have a connection overall_data_state = 1 End If End If 'Test the connection if it is possible to have one If overall_data_state = 1 Then 'Initially indicate that we do have a connection and the screen label will probably be correct. 'We do this because of the potential latency in determining when a connection is not available. Internet_Connected(True) 'Now we will test the connection and change it back to False if it fails Dim job1 As HttpJob job1.Initialize("Job1", Me) 'Send a GET request job1.Download("http://www.google.com") 'Once this completes the JobDone routine is executed and the labels are set to their true state. Else Internet_Connected(False) End If End Sub Sub JobDone (job As HttpJob ) If job.Success = True Then Internet_Connected(True) Else Internet_Connected(False) End If job.Release End Sub Sub Internet_Connected (Flag As Boolean) If Flag = True Then InternetConnected = True lblInternetConnected.Text = Chr(ConnectedIcon) lblInternetConnected.TextColor = Colors.White Else InternetConnected = False lblInternetConnected.text = Chr(DisconnectedIcon) lblInternetConnected.TextColor = Colors.Red End If End Sub #End Region
Requires the httputils2 library.
That's a known problem for most intense mobile users...here in France.Just for information: with my french Internet/phone provider (Free), I have a very fast ping to its own servers or to the Google servers, even when I'm unable to connect to everything else. I discovered this lately. I was totally unable to connect to many web sites where I was, while Google servers and Free servers were very responsive. So doing a test to reach Google.com learns me nothing about the ability to reach another server.
Free cannot use the Bouygues network (it would have to pay for that). It pays already a lot of money for using the Orange network everywhere there's no Free antenna.That's a known problem for most intense mobile users...here in France.
I have two mobile providers, orange and SFR... On diferent devices, side by side, I can have an almost flawless connection with orange, and not even be able to register the mobile network on SFR... I haven't tested free nor bouygues, but assuming the pub is true, most 4G network was implemented by bouygues, so AFAIK, free uses the bouygues network while orange and SFR use their own...
Sub InternetStatus As Boolean
If HasFeature("android.hardware.wifi") = False AND HasFeature("android.hardware.telephony") = False Then Return False
GetPropInfo
If sGate = "" Then Return False
If sD1 = "" AND sD2 = "" Then Return False
Return True
End Sub
Sub HasFeature(Feature As String) As Boolean
Dim r As Reflector
r.Target = r.GetContext
r.Target = r.RunMethod("getPackageManager")
Return r.RunMethod2("hasSystemFeature", Feature, "java.lang.String")
End Sub
Sub GetPropInfo
sWifi = "wlan0"
sIP = ""
sD1 = ""
sD2 = ""
Dim P As Phone
Dim sb As StringBuilder
Dim i As Int
Dim PropData() As String
sb.Initialize
P.Shell("getprop", Null, sb, Null)
PropData = Regex.Split(CRLF,sb.ToString)
For i = 0 To PropData.Length - 1
' What is the Wifi Interface called?
If PropData(i).Contains("[wifi.interface]") Then
sWifi = PropData(i)
sWifi = sWifi.Replace("[","")
sWifi = sWifi.Replace("]","")
sWifi = sWifi.SubString(sWifi.IndexOf(" ")).Trim
Exit
End If
Next
If sWifi.Length > 0 Then
For i = 0 To PropData.Length - 1
If PropData(i).Contains("[dhcp." & sWifi & ".ipaddress]") Then
sIP = PropData(i)
sIP = sIP.Replace("[","")
sIP = sIP.Replace("]","")
sIP = sIP.SubString(sIP.IndexOf(" ")).Trim
End If
If PropData(i).Contains("[dhcp." & sWifi & ".mask]") Then
sMask = PropData(i)
sMask = sMask.Replace("[","")
sMask = sMask.Replace("]","")
sMask = sMask.SubString(sMask.IndexOf(" ")).Trim
End If
If PropData(i).Contains("[dhcp." & sWifi & ".gateway]") Then
sGate = PropData(i)
sGate = sGate.Replace("[","")
sGate = sGate.Replace("]","")
sGate = sGate.SubString(sGate.IndexOf(" ")).Trim
End If
If PropData(i).Contains("[dhcp." & sWifi & ".dns1]") Then
sD1 = PropData(i)
sD1 = sD1.Replace("[","")
sD1 = sD1.Replace("]","")
sD1 = sD1.SubString(sD1.IndexOf(" ")).Trim
End If
If PropData(i).Contains("[dhcp." & sWifi & ".dns2]") Then
sD2 = PropData(i)
sD2 = sD2.Replace("[","")
sD2 = sD2.Replace("]","")
sD2 = sD2.SubString(sD2.IndexOf(" ")).Trim
End If
DoEvents
Next
Else
For i = 0 To PropData.Length - 1
If PropData(i).Contains("[net.rmnet0.gw]") Then
sGate = PropData(i)
sGate = sGate.Replace("[","")
sGate = sGate.Replace("]","")
sGate = sGate.SubString(sGate.IndexOf(" ")).Trim
End If
If PropData(i).Contains("[net.rmnet0.dns1]") Then
sD1 = PropData(i)
sD1 = sD1.Replace("[","")
sD1 = sD1.Replace("]","")
sD1 = sD1.SubString(sD1.IndexOf(" ")).Trim
End If
' What is the Wifi Interface called?
If PropData(i).Contains("[net.rmnet0.dns2]") Then
sD2 = PropData(i)
sD2 = sD2.Replace("[","")
sD2 = sD2.Replace("]","")
sD2 = sD2.SubString(sD2.IndexOf(" ")).Trim
End If
DoEvents
Next
End If
End Sub
If you can reach a DNS, a Google server or your provider servers, but not the web site or the data server that you need, then I don't know if "connected" means something.You can check if you have a DNS-server or not.
B4X:Sub InternetStatus As Boolean If HasFeature("android.hardware.wifi") = False AND HasFeature("android.hardware.telephony") = False Then Return False GetPropInfo If sGate = "" Then Return False If sD1 = "" AND sD2 = "" Then Return False Return True End Sub Sub HasFeature(Feature As String) As Boolean Dim r As Reflector r.Target = r.GetContext r.Target = r.RunMethod("getPackageManager") Return r.RunMethod2("hasSystemFeature", Feature, "java.lang.String") End Sub Sub GetPropInfo sWifi = "wlan0" sIP = "" sD1 = "" sD2 = "" Dim P As Phone Dim sb As StringBuilder Dim i As Int Dim PropData() As String sb.Initialize P.Shell("getprop", Null, sb, Null) PropData = Regex.Split(CRLF,sb.ToString) For i = 0 To PropData.Length - 1 ' What is the Wifi Interface called? If PropData(i).Contains("[wifi.interface]") Then sWifi = PropData(i) sWifi = sWifi.Replace("[","") sWifi = sWifi.Replace("]","") sWifi = sWifi.SubString(sWifi.IndexOf(" ")).Trim Exit End If Next If sWifi.Length > 0 Then For i = 0 To PropData.Length - 1 If PropData(i).Contains("[dhcp." & sWifi & ".ipaddress]") Then sIP = PropData(i) sIP = sIP.Replace("[","") sIP = sIP.Replace("]","") sIP = sIP.SubString(sIP.IndexOf(" ")).Trim End If If PropData(i).Contains("[dhcp." & sWifi & ".mask]") Then sMask = PropData(i) sMask = sMask.Replace("[","") sMask = sMask.Replace("]","") sMask = sMask.SubString(sMask.IndexOf(" ")).Trim End If If PropData(i).Contains("[dhcp." & sWifi & ".gateway]") Then sGate = PropData(i) sGate = sGate.Replace("[","") sGate = sGate.Replace("]","") sGate = sGate.SubString(sGate.IndexOf(" ")).Trim End If If PropData(i).Contains("[dhcp." & sWifi & ".dns1]") Then sD1 = PropData(i) sD1 = sD1.Replace("[","") sD1 = sD1.Replace("]","") sD1 = sD1.SubString(sD1.IndexOf(" ")).Trim End If If PropData(i).Contains("[dhcp." & sWifi & ".dns2]") Then sD2 = PropData(i) sD2 = sD2.Replace("[","") sD2 = sD2.Replace("]","") sD2 = sD2.SubString(sD2.IndexOf(" ")).Trim End If DoEvents Next Else For i = 0 To PropData.Length - 1 If PropData(i).Contains("[net.rmnet0.gw]") Then sGate = PropData(i) sGate = sGate.Replace("[","") sGate = sGate.Replace("]","") sGate = sGate.SubString(sGate.IndexOf(" ")).Trim End If If PropData(i).Contains("[net.rmnet0.dns1]") Then sD1 = PropData(i) sD1 = sD1.Replace("[","") sD1 = sD1.Replace("]","") sD1 = sD1.SubString(sD1.IndexOf(" ")).Trim End If ' What is the Wifi Interface called? If PropData(i).Contains("[net.rmnet0.dns2]") Then sD2 = PropData(i) sD2 = sD2.Replace("[","") sD2 = sD2.Replace("]","") sD2 = sD2.SubString(sD2.IndexOf(" ")).Trim End If DoEvents Next End If End Sub
I use this, and if someone see any problem with this I would love to know. It seems to work really good for me.
If you can reach a DNS, a Google server or your provider servers, but not the web site or the data server that you need, then I don't know if "connected" means something.
I was under the impression that they only had their own fiber-optic network.. Didn't know they had gsm network too.Free cannot use the Bouygues network (it would have to pay for that). It pays already a lot of money for using the Orange network everywhere there's no Free antenna.
Just for information: with my french Internet/phone provider (Free), I have a very fast ping to its own servers or to the Google servers, even when I'm unable to connect to everything else. I discovered this lately. I was totally unable to connect to many web sites where I was, while Google servers and Free servers were very responsive. So doing a test to reach Google.com learns me nothing about the ability to reach another server.
If you want to reach Toto.com, what's the usefulness of testing a DNS before? Try to connect to Toto.com first (or just ping it). What can prove there's a connection to your server better than joining this server? If you have problems to reach the DNS you chose because a guy with a backhoe damaged an optical fiber out of the building hosting these domain name servers, can you conclude there's no connection to Internet?You can see if a connection exists before trying to reach your prefered destination, it's no point trying if the basic connection isn't there is it?
If you want to reach Toto.com, what's the usefulness of testing a DNS before? Try to connect to Toto.com first (or just ping it). What can prove there's a connection to your server better than joining this server? If you have problems to reach the DNS you chose because a guy with a backhoe damaged an optical fiber out of the building hosting these domain name servers, can you conclude there's no connection to Internet?
The usefullness is that you can see if the problem is in the unit, the router, the ISP or something else.
We don't want to know if we are connected to google or not, all we want to know is if a connection is at all possible.In brief, I really don't understand why everybody needs to know if they are connected to google.com or any server that is of no use to their app.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?