Android Question Sub isConnected() As Boolean not work Android 6.0

rscheel

Well-Known Member
Licensed User
Longtime User
I am using this to find out if the phone has an Internet connection or not, to android 5.1.1 worked perfectly, but on android 6.0 even without internet connection just returns true, someone has a solution that can be shared.

Code: Copy
Sub isConnected() As Boolean
Dim sSocket As ServerSocket
If  sSocket.IsInitialized = False Then
    sSocket.Initialize(8080, "sSocket")
    sSocket.Close
End If
If sSocket.GetMyIP = "127.0.0.1" Then
    Return False
Else
       Return True
End If
End Sub

Thanks.
 
What is the output of
Code: Copy
log(sSocket.GetMyIP)
 
Upvote 0
Returns this, so it will never be equal to the ip 127.0.0.1 and always will be true.

Code: Copy
fe80::b0cb:6dff:fe91:f8d0%dummy0
 
Upvote 0
I have tried other methods, but do not work when used in a service, and this worked perfectly in service and in the app, but does not work on android 6.0.
 
Last edited by a moderator:
Upvote 0
Nexus 5 with CyanogenMod Android 5.x and higher return ipv6 if there is no internet connection. Already get trouble with this in past.
 
Upvote 0
What is the output of this code:
Code: Copy
Sub Activity_Create(FirstTime As Boolean)
   Dim s As ServerSocket
   Log(s.GetMyIP)
   Log(s.GetMywifiIP)
End Sub

These are the outputs Android 6.0
Code: Copy
Log(s.GetMyIP) 'fe80::b0cb:6dff:fe91:f8d0%dummy0
Log(s.GetMywifiIP) '127.0.0.1


These are the outputs Android 5.1
Code: Copy
Log(s.GetMyIP) '127.0.0.1
Log(s.GetMywifiIP) '127.0.0.1
 
Last edited:
Upvote 0
You could do something like this, but I'm not sure at all Android 6.0 phones send this same
fe80::b0cb:6dff:fe91:f8d0%dummy0

Code: Copy
Sub isConnected() As Boolean
    Dim sSocket As ServerSocket
    If  sSocket.IsInitialized = False Then
        sSocket.Initialize(8080, "sSocket")
        sSocket.Close
    End If
    If sSocket.GetMyIP = "127.0.0.1" Or sSocket.GetMyIP = "fe80::b0cb:6dff:fe91:f8d0%dummy0" Then
        Return False
    Else
           Return True
    End If
End Sub
 
Upvote 0
One solution might be this, but what if a phone has not ipv4 and ipv6 connectivity.

Code: Copy
Sub isConnected() As Boolean
    Dim sSocket As ServerSocket
    If  sSocket.IsInitialized = False Then
        sSocket.Initialize(8080, "sSocket")
        sSocket.Close
    End If
    Dim cadenaIP As String = sSocket.GetMyIP
    If sSocket.GetMyIP = "127.0.0.1" Or cadenaIP.StartsWith("fe80:") Then 'IP V6 local fe80::b0cb:6dff:fe91:f8d0%dummy0
        Return False
    Else
           Return True
    End If
End Sub
 
Upvote 0
Why not use GetMyWifiIp?

Though the best way (and the only reliable way) to test for an internet connection is:
Code: Copy
Sub Test
   Dim j As HttpJob
   j.Initialize("test internet", Me)
   j.Download("http://www.google.com")
   j.GetRequest.Timeout = 10000
End Sub

Sub JobDone(Job As HttpJob)
   If Job.JobName = "test internet" Then
     InternetStatus(Job.Success)
   End If
   Job.Release
End Sub

Sub InternetStatus(Good As Boolean)

End Sub

You can change google with the relevant host.

It is better than checking the ip address as the fact that there is an ip address doesn't mean that there is an internet connection.
 
Upvote 0
Why not use GetMyWifiIp?

Though the best way (and the only reliable way) to test for an internet connection is:
Code: Copy
Sub Test
   Dim j As HttpJob
   j.Initialize("test internet", Me)
   j.Download("http://www.google.com")
   j.GetRequest.Timeout = 10000
End Sub

Sub JobDone(Job As HttpJob)
   If Job.JobName = "test internet" Then
     InternetStatus(Job.Success)
   End If
   Job.Release
End Sub

Sub InternetStatus(Good As Boolean)

End Sub

You can change google with the relevant host.

It is better than checking the ip address as the fact that there is an ip address doesn't mean that there is an internet connection.

Thanks for your time works wonders.
 
Upvote 0
Back
Top