B4R Question android send udp packet to esp8266, error occured.

tzfpg

Active Member
Licensed User
Longtime User
Hi all,
After connect to esp8266, and send udp packet, android often occured error and esp8266 can't receive the packet.

error code:
B4X: Copy
Phone WifiIpAddress 192.168.4.3
java.io.IOException: sendto failed: ENETUNREACH (Network is unreachable)
    at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:576)
    at libcore.io.IoBridge.sendto(IoBridge.java:544)
    at java.net.PlainDatagramSocketImpl.send(PlainDatagramSocketImpl.java:125)
    at java.net.DatagramSocket.send(DatagramSocket.java:721)
    at anywheresoftware.b4a.objects.SocketWrapper$UDPSocket$1.run(SocketWrapper.java:479)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:764)
Caused by: android.system.ErrnoException: sendto failed: ENETUNREACH (Network is unreachable)
    at libcore.io.Linux.sendtoBytes(Native Method)
    at libcore.io.Linux.sendto(Linux.java:227)
    at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:307)
    at libcore.io.IoBridge.sendto(IoBridge.java:542)
    ... 8 more
B4X: Copy

android code:
B4X: Copy
btn_Click
    Dim b1(2) As Object
    b1(0) = "test"
    b1(1) = "1"
    UDP_Send(Starter.ser.ConvertArrayToBytes(b1))
End Sub

Sub UDP_Send(data() As Byte)
    udp_start
    Dim Packet As UDPPacket
    Packet.Initialize(data, udp_ip, 8888)
    usocket.Send(Packet)
End Sub

Sub udp_start
    Dim ip_str() As String = Regex.Split("\.", Main.wifi.WifiIpAddress)
    If ip_str(0) = "192" And ip_str(1) = "168" And ip_str(2) = "4" Then
        If usocket.IsInitialized = False Then
            usocket.Initialize("UDP", 8888, 1024)
        End If
    End If
End Sub
B4X: Copy

android send packet very not stable, how can solve this error?

Thank you.
 
i found the problem is android device connect to esp8266 no more than 4 second, then disconnect.
how to connect esp8266 wifi more then 5 second?
 
Upvote 0
 
Upvote 0
Back
Top