Android Question Communication Socket with esp8266 android 8

Humberto

Active Member
Licensed User
Longtime User
I have an app that send commands to an ESP8266 and the same program ( target targetSdkVersion 27 ) works in android 4.12 and 7.11 but fails to send in android 8

The ESP8266 is configurated as a server, I can connect to it and get an address 192.168.4.2 ( the server is at 192.168.4.1 )

When I tryed to connect I get this error

Erro Conexão socket Token 192.168.4.2 - (SocketTimeoutException) java.net.SocketTimeoutException: failed to connect to /192.168.4.1 (port 80) from /179.165.88.27 (port 47238) after 5000ms

What is strange is that the address "From/179.165.88.27" is an internet address and not I internal net address.

Just happen with android 8, the old version works in android 8



B4X:
        sock.Initialize("sock")
        sock.Connect("192.168.4.1", 80, 5000)
'        sock.Connect(EdtSend.Text, 80, 10000)
        Wait For sock_Connected (Successful As Boolean)
        If Successful Then
            astream.InitializePrefix(sock.InputStream, False, sock.OutputStream, "astream")
'            astream.Write(ser.ConvertArrayToBytes(RecordToObjects(rec)))
'            astream.Write(ser.ConvertArrayToBytes( Array ("teste de envio") ))
            Buffer_Rec = ""
            astream.Write(  bc.StringToBytes ( pMensagem, "UTF8" )  )' ( "xxGET /set/clear/998557019/9012", "UTF8") )
            xFlag = True
            Do While xFlag
                Wait For astream_NewData (Buffer() As Byte)
                xResp = bc.StringFromBytes ( Buffer, "UTF8")
                If xResp = "EOL" Then
                    xFlag = False
                    Log ( "Recebidos: " &  Buffer_Rec )
                Else
                    Buffer_Rec = Buffer_Rec & xResp
                End If
            Loop
            Set_Sv_Mensagens ( Buffer_Rec , Altura_Mensagem)
        Else
            Log  ( "Erro Conexão socket Token " & wifi.WifiIpAddress & " - " & LastException )
            Set_Sv_Mensagens ( trans.translate( "Problemas conexão SOS Token " ) , 30dip)
        End If


The manifest is
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="27"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.

AddPermission(android.permission.CAPTURE_AUDIO_OUTPUT)
AddPermission(android.permission.MODIFY_AUDIO_SETTINGS) 
AddPermission(android.permission.RECORD_AUDIO)
AddPermission(android.permission.READ_EXTERNAL_STORAGE)
AddPermission(android.permission.WRITE_EXTERNAL_STORAGE)

CreateResourceFromFile(Macro, Themes.LightTheme)

'#### NEW MANIFEST FIREBASE FOR B4A V8
CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)
CreateResourceFromFile(Macro, FirebaseAnalytics.Firebase)
CreateResourceFromFile(Macro, FirebaseNotifications.FirebaseNotifications)
'CreateResourceFromFile(Macro, FirebaseAnalytics.FirebaseAnalytics)
'CreateResourceFromFile(Macro, FirebaseAuth.FirebaseAuth)

CreateResourceFromFile("google-services", "google-services.json")

SetActivityAttribute(main, android:windowSoftInputMode, adjustPan|stateHidden)   
AddPermission(android.permission.ACCESS_FINE_LOCATION)
AddPermission (android.permission.ACCESS_COARSE_LOCATION)

AddPermission (android.permission.ACCESS_NETWORK_STATE)
AddPermission (android.permission.ACCESS_WIFI_STATE)
AddPermission (android.permission.CHANGE_WIFI_STATE)
AddPermission (android.permission.INTERNET)

AddPermission(android.permission.WAKE_LOCK)

AddManifestText( <permission
  android:name="$PACKAGE$.permission.MAPS_RECEIVE"
  android:protectionLevel="signature"/>
  <uses-feature android:glEsVersion="0x00020000" android:required="true"/>)

AddApplicationText(<meta-data
  android:name="com.google.android.maps.v2.API_KEY"
  android:value="AIzaSyDectesLvKHN3wCGpKl3r-XUlXVTSgfwFE"/>
   <meta-data android:name="com.google.android.gms.version"
   android:value="@integer/google_play_services_version" />
)

and I ask for the permissions

B4X:
    Log ( "Check permission - PERMISSION_WRITE_EXTERNAL_STORAGE")
    rp.CheckAndRequest (rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result = False Then
        ToastMessageShow ("Fechando aplicação",True)
        Activity.Finish
    End If

    Log ( "Check permission - PERMISSION_READ_EXTERNAL_STORAGE")
    rp.CheckAndRequest (rp.PERMISSION_READ_EXTERNAL_STORAGE)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result = False Then
        ToastMessageShow ("Fechando aplicação",True)
        Activity.Finish
    End If

    Log ( "Check permission - PERMISSION_RECORD_AUDIO")
    rp.CheckAndRequest (rp.PERMISSION_RECORD_AUDIO)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result = False Then
        ToastMessageShow ("Fechando aplicação",True)
        Activity.Finish
    End If



    Log ( "Check permission - PERMISSION_ACCESS_COARSE_LOCATION")
    rp.CheckAndRequest (rp.PERMISSION_ACCESS_COARSE_LOCATION)
    wait for Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result = False Then
        ToastMessageShow ("Fechando aplicação",True)
        Activity.Finish
    End If
    
    Log ( "Check permission - PERMISSION_ACCESS_FINE_LOCATION")
    rp.CheckAndRequest (rp.PERMISSION_ACCESS_FINE_LOCATION)
    wait for Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result = False Then
        ToastMessageShow ("Fechando aplicação",True)
        Activity.Finish
    Else
        Starter.f_Permissions = False
    End If
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
Fyi - when I ping 179.165.88.27, it comes back as:

Pinging 179-165-88-27.user.vivozap.com.br [179.165.88.27] with 32 bytes of data:
Reply from 179.165.88.27: bytes=32 time=1724ms TTL=238
Reply from 179.165.88.27: bytes=32 time=404ms TTL=238
Reply from 179.165.88.27: bytes=32 time=435ms TTL=238
Reply from 179.165.88.27: bytes=32 time=411ms TTL=238

Which indicates that it's a cellular data user on the Vivo network in Brazil.

- Colin.
 
Upvote 0

Humberto

Active Member
Licensed User
Longtime User
There is a small project that communicate with ESP8266 .

With manifest android:targetSdkVersion ="19" works

With manifest android:targetSdkVersion="22" timeout error
(SocketTimeoutException) java.net.SocketTimeoutException: failed to connect to /192.168.4.1 (port 80) from /179.165.88.27 (port 37703) after 5000ms
 

Attachments

  • Test_Socket.zip
    11 KB · Views: 286
Upvote 0

Humberto

Active Member
Licensed User
Longtime User
Which indicates that it's a cellular data user on the Vivo network in Brazil.

As my wifi connection ( ESP8266) isn´t connected to internet when I try to connect to my sever ( ESP8266 192.168.4.1 ) in the same local network in TargetSdk = 19 connect OK, on targetSDk > 22 goes to GPRS trought Vivo-Brazil .

How can I connect to a server in the same network ?

It´s a real device Moto G6plus
 
Upvote 0

Humberto

Active Member
Licensed User
Longtime User
I disable the mobile data from the cellphone and it works.

I don´t know why even if this is a internal address ( 192.....) the android goes to outside.

I´ll try to disable by code the mobile data and then when finished enable again

Thanks Colin
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
I disable the mobile data from the cellphone and it works.

I don´t know why even if this is a internal address ( 192.....) the android goes to outside.

I´ll try to disable by code the mobile data and then when finished enable again

Thanks Colin

Sounds like the Moto G6 Plus might have an auto-switching function like the Samsung devices.

- Colin.
 
Upvote 0

Humberto

Active Member
Licensed User
Longtime User
I didn´t find this configuration.

It seems that is not allowed to disable mobie data by code, I´ll have to ask for the user to do it.

Android should verify first if the address is a LAN or internet address
 
Upvote 0
Top