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.

B4X:
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.
 

rscheel

Well-Known Member
Licensed User
Longtime User
Returns this, so it will never be equal to the ip 127.0.0.1 and always will be true.

B4X:
fe80::b0cb:6dff:fe91:f8d0%dummy0
 
Upvote 0

rscheel

Well-Known Member
Licensed User
Longtime User
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

Pendrush

Well-Known Member
Licensed User
Longtime User
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

rscheel

Well-Known Member
Licensed User
Longtime User

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


These are the outputs Android 5.1
B4X:
Log(s.GetMyIP) '127.0.0.1
Log(s.GetMywifiIP) '127.0.0.1
 
Last edited:
Upvote 0

rscheel

Well-Known Member
Licensed User
Longtime User
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

B4X:
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

rscheel

Well-Known Member
Licensed User
Longtime User
One solution might be this, but what if a phone has not ipv4 and ipv6 connectivity.

B4X:
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

Erel

B4X founder
Staff member
Licensed User
Longtime User
Why not use GetMyWifiIp?

Though the best way (and the only reliable way) to test for an internet connection is:
B4X:
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

rscheel

Well-Known Member
Licensed User
Longtime User

Thanks for your time works wonders.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…