I would like my app to check for an internet connection. I have only seen threads which are over a year old on this topic. I have been told that a new thread should be started when that is so.
I have just tried:
B4X:
myDevice.Initialize("")
If myPhone.GetDataState.EqualsIgnoreCase("CONNECTED") _
Or myDevice.Connected Then
ToastMessageShow("Internet Connected", True)
Else
ToastMessageShow("Internet Not Connected", True)
ToastMessageShow("myPhone.GetDataState="&myPhone.GetDataState, True)
ToastMessageShow("myDevice.Connected="&myDevice.Connected, True)
End If
But it always gives myPhone.GetDataState as "DISCONNECTED" and myDevice.Connected as False. How should this be coded? It would be nice to see the corrected coding including a test to see if the device is a phone or a tablet.
Sub IsConn As Boolean
Dim P As Phone
Dim WF As ServerSocket
Dim B As Boolean=False
If P.GetDataState="CONNECTED" Then B=True
If WF.GetMyIP<>"127.0.0.1" Then B=True
Return B
End Sub
Use for this code Phone Library and Netwrok Library.
Sub Button1_Click
TestConnection
End Sub
Sub TestConnection
Dim Connect As HttpJob
Connect.Initialize("Connect", Me)
Connect.Download("http://yourdomain.com/connect.php")
Msgbox("wifi/Data status","Wifi/data, internet OK")
End Sub
I don't understand how this code tests for an internet connection: there is no IF statement to do the test. BTW httputils2 has been deprecated in favour of OkHttpUtils2.
Sub IsConn As Boolean
Dim P As Phone
Dim WF As ServerSocket
Dim B As Boolean=False
If P.GetDataState="CONNECTED" Then B=True
If WF.GetMyIP<>"127.0.0.1" Then B=True
Return B
End Sub
I don't understand how this code tests for an internet connection: there is no IF statement to do the test. BTW httputils2 has been deprecated in favour of OkHttpUtils2.
@johnaaronrose Please use [code]code here...[/code] tags when posting code.
In most cases the best way is to just send the http request that you need to send. If it succeeds then all is good. If not then the relevant remote resource is not accessible.
@johnaaronrose Please use [code]code here...[/code] tags when posting code.
In most cases the best way is to just send the http request that you need to send. If it succeeds then all is good. If not then the relevant remote resource is not accessible.
There doesn't seem to be an icon when posting to put a Code tag enclosing some coding. Or am I missing it?
Thanks for the tip. Unfortunately if I do that it would request a download of a file and I don't know how to process that. Star-Dust's solution worked for me once I'd coded in the missing Initialize command.
Sub IsConn As Boolean
Dim P As Phone
Dim WF As ServerSocket
Dim B As Boolean=False
If P.GetDataState="CONNECTED" Then B=True
If WF.GetMyIP<>"127.0.0.1" Then B=True
Return B
End Sub
Use for this code Phone Library and Netwrok Library.
Initialize is not necessary in this case.
To not let you report a mistake, add the 'ignore note to the line
B4X:
Sub IsConn As Boolean 'ignore
Dim P As Phone
Dim WF As ServerSocket 'ignore
Dim B As Boolean=False
If P.GetDataState="CONNECTED" Then B=True
If WF.GetMyWiFiIP<>"127.0.0.1" Then B=True 'ignore
If WF.GetMyIP<>"127.0.0.1" Then B=True
Return B
End Sub
in all my Apps this method works, I'm sorry that it does not work for you
Initialize is not necessary in this case.
To not let you report a mistake, add the 'ignore note to the line
B4X:
Sub IsConn As Boolean 'ignore
Dim P As Phone
Dim WF As ServerSocket 'ignore
Dim B As Boolean=False
If P.GetDataState="CONNECTED" Then B=True
If WF.GetMyWiFiIP<>"127.0.0.1" Then B=True 'ignore
If WF.GetMyIP<>"127.0.0.1" Then B=True
Return B
End Sub
in all my Apps this method works, I'm sorry that it does not work for you
The method did work with the Initialize statement inserted. I didn't check whether it worked without the Initialize statement as I had a compiler warning stating that the WF variable was not Initialized. Due to originally programming in Fortran, Cobol & IBM 360 Assembler, I found that it was good practice to remove all compiler warnings by making code changes. It's a habit that has stuck.