Android Question Constantly disconnecting from ESP8266 WiFi

mare1980k1

Member
Licensed User
I can connect to my ESP8266 just fine. But quite often after just a few seconds (2,3,4,5 seconds), it randomly disconnects. Sometimes it takes a bit longer to disconnect randomly, after a minute or so.

And it does so on LG Nexus 5X. On the other phone the app is working just fine and can maintain connection for few hours at a time. It is the exact same app on both phones - the app that I made in B4A. The problem phone is LG Nexus 5X and the phone that it works correctly on is LG G5.

And also there is an app that is called USB/WiFi/Bluetooth terminal that I didn't make, which works just fine on the problematic phone with ESP8266.
Basically the core of the app is sending some commands from an app to ESP8266.

Please help,
Thank you,
Best Regards,
Marx

Here are the snippets of code:

In the Starter:
B4X:
Sub Process_Globals
   Public client1 As Socket
   Public connected As Boolean
   Public server As ServerSocket
   Public astream As AsyncStreams
   Private working As Boolean = True
 
   Public connecting As Boolean
End Sub

Public Sub ConnectToServer(Host As String)
   Try
       Log("Trying to connect to: " & Host)
       If client1.IsInitialized Then
           If astream.IsInitialized Then astream.Close
           client1.Close
           Sleep(20)
       End If
  
       client1.Initialize("client1")
       Dim portMAP As Map
       portMAP.Initialize
       portMAP=File.readMap(File.DirInternal, "PORT.properties")
       If portMAP.ContainsKey("0") Then
           Dim pomint As Int
           pomint=portMAP.Get("0")
           client1.Connect(Host, pomint, 0)
       Else
           portMAP.Put("0","2460")
           client1.Connect(Host, 2460, 0)
           File.WriteMap(File.DirInternal, "PORT.properties",portMAP)
       End If
  
       Wait For client1_Connected (Successful As Boolean)
       If Successful Then
           astream.Initialize(client1.InputStream,client1.OutputStream,"astream")
           CallSub(Main, "Its_connected")
           UpdateState(True)
       Else
           Log("Failed to connect: " & LastException)
       End If
   Catch
       Log(LastException)
       ToastMessageShow("G10",False)
   End Try
End Sub
Main:
B4X:
Sub Process_Globals
   Dim laterTMR as timer
End Sub

Sub Activity_Create(FirstTime As Boolean)
   laterTMR.Initialize("laterTMR",3000)
End Sub

Sub laterTMR_tick
   CallSub2(Starter, "ConnectToServer", "192.168.1.7")
   laterTMR.Enabled=False
End Sub

Sub connect_to_wifi
Dim l As List
   l.Initialize
   Dim m As Int
   m=0
   Dim map2 As Map
   map2.Initialize
  
   For i=0 To sken.wifis.Length-1
       If sken.wifis(i).StartsWith("LED_DISPLAY_")=True Then
           l.Add(sken.wifis(i))
           map2.Put(sken.wifis(i),i)
           m=m+1
       End If
   Next
 
   If m<>0 Then
       If m>1 Then
           Dim res As Int
           res = InputList(l, "Choose display: ", -1)
           If res <> DialogResponse.CANCEL Then
               Dim pomstring As String
               pomstring=l.Get(res)
               pomstring=pomstring.SubString2(12,16)
               If checkingExistence(l.Get(res)) Then
                   If sken.saveWifiAP(map2.Get(l.Get(res)),"WPA2",password)) Then
                       laterTMR.Enabled=True
                   Else
                       ToastMessageShow("Didn't manage to connect.", False)
                   End If
               End If
           End If
       Else if m=1 Then
           Dim pomstring As String
           pomstring=l.Get(0)
           pomstring=pomstring.SubString2(12,16)
           If sken.saveWifiAP(map2.Get(l.Get(0)),"WPA2",password)) Then
               laterTMR.Enabled=True
           Else
               ToastMessageShow("Didn't manage to connect.", False)
           End If
       End If
   Else
       ToastMessageShow("Display not in range.", False)
   End If
 
Last edited:
Top