Android Question Connect ESP8266/ESP32 to Android Emulator

max123

Well-Known Member
Licensed User
Longtime User
Hi programmers,

I had necessity on some situations to use my app on android emulator to connect over wifi to ESP8266/ESP32.
This works, I can estabilish a socket TCP or UDP connection, send and receive data to/from.

Now I have the opposite necessity, I have developed an android app where ESP send UDP (connectionless) packets
to Android but the emulator cannot receive from ESP on my app.

On real device all is working.

What I do not know is the IP address, Android emulator settings and my app says the current IP Address is 10.0.2.16,
this range of IP addresses should be inside a Local Area Network as my short tutorial here:
https://www.b4x.com/android/forum/t...-address-is-private-or-public.144370/#content

I've put 10.0.2.16 in the ESP sketch, ESP send packets but the app on emulator still wait for incoming UDP packets and nothing happen.

I even tried to put the PC IP Address where emulator is, but nothing happen.

Can someone tell me if that is possible ?

Many thanks
 

Attachments

  • Screenshot_1682792918.png
    Screenshot_1682792918.png
    43.7 KB · Views: 221
Last edited:

max123

Well-Known Member
Licensed User
Longtime User
Hi Erel, thanks for reply ;),

do you mean to use B4J to forward the connection ?
I already tried it but with no success....

Here my develops....

I've read that this is a known Emulator bug, after that I've read and applied a small guide and now I can receive UDP packets sent from ESP8266 on emulator in my app, but probably because I launch emulator in the terminal by disabling default WiFi, it can only receive and cannot respond to ESP8266.

Here a guide I follow to get it receive, please see last 2 posts:
https://stackoverflow.com/questions/65918070/how-to-get-udp-data-from-pc-to-android-emulator

So I used telnet to connect to emulator (127.0.0.1:5554) and redirect ports...

After that I managed with B4J (before your advice :p ) to get the final UDP data packet routing, but I'm confuse here and created two different UDP sockets, one listen ESP and send to emulator (127.0.0.1:8888) and one listen emulator and send to ESP.

ESP send data to port 8888 and listen on port 8889 for replies.
My app on emulator listen on port 8888 and send replies to ESP requests on port 8889, ESP8266 IP Address 192.168.178.38.

As expected my code does not work :confused:

It only work receiving packets but cannot send. To get it working I used a small port forwarding found on the web, but it does not provide a way to get responses. You can find it on bottom of link I posted.

Would be useful have a simple B4J app that can do it without manage telnet and other things any time we need to connect to an emulator and get replies, so engaging a bidirectional comunication. This can be useful for TCP & UDP sockets, as well for webserver, websocket and other useful things.

This just for debugging purpose instead of using real device ....

I've attached my simple B4J project if you want better know what I do.

Thanks.
 

Attachments

  • UDPForward_Bidirectional.zip
    3.1 KB · Views: 192
Last edited:
Upvote 0

teddybear

Well-Known Member
Licensed User
My app on emulator listen on port 8888 and send replies to requests to port 8889.
What ip addr do you use for replying in your app? you need to use adapter ipv4 instead of loopback addr.
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Thanks for reply @teddybear , the IP address is specified in a text field, the user can change it.

My app just send it as is working on real device, so in my case ESP8266 have 192.168.178.38, so yes it is ipv4, not a loopback 127.0.0.1.

I've attached the code, please try it if you have some time. It will not work now as it is. To test it you can just make a simple app that receive UDP data (on emulator) and on Android cell use a simple app that send UDP, to test I use some on PlayStore, or may manage it with B4A and just send some UDP packets.

I used ESP8266, but probably you can successfully send data from Phone to Emulator over WiFi, this worked for me, only the replies does not work. Not tested with phone but should work too, here Android phone acts as my ESP8266.

Remember to follow these 2 steps or it will not work, I even used a simple udp port forwarding in follow page at bottom a github link to it, or you can do it with B4J (by changing my code).
https://stackoverflow.com/questions/65918070/how-to-get-udp-data-from-pc-to-android-emulator

Thanks for your time and interest.
 
Last edited:
Upvote 0

teddybear

Well-Known Member
Licensed User
Thanks for reply @teddybear , the IP address is specified in a text field, the user can change it.

My app just send it as is working on real device, so in my case ESP8266 have 192.168.178.38, so yes it is ipv4, not a loopback 127.0.0.1.

I've attached the code, please try it if you have some time. It will not work now as it is. To test it you can just make a simple app that receive UDP data (on emulator) and on Android cell use a simple app that send UDP, to test I use some on PlayStore, or may manage it with B4A and just send some UDP packets.

I used ESP8266, but probably you can successfully send data from Phone to Emulator over WiFi, this worked for me, only the replies does not work. Not tested with phone but should work too, here Android phone acts as my ESP8266.

Remember to follow these 2 steps or it will not work, I even used a simple udp port forwarding in follow page at bottom a github link to it, or you can do it with B4J (by changing my code).
https://stackoverflow.com/questions/65918070/how-to-get-udp-data-from-pc-to-android-emulator

Thanks for your time and interest.
I see what you mean, in post #4, I asked what ip do you use when your app in the emulator is communicating with udpforward of b4j?
if your replying ip is the pc,UDP bidirectional communication will work between PC and simulator
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
I will try it and post here results, thanks.
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
So, here is my last ...

I've tried to get it working. Almost is working using a manual procedure to redirect ports where telnet connect to emulator.

Almost is working, even B4J reply packets to ESP, but for some strange reasons (I need to investigate) the ESP do not receive it.

Note that in my case I use ESP, but it can be just a Phone or a PC that send and receice udp packets.
If you don't know ESP. you can replace the word ESP with PHONE on this post.
Because UDP api cannot send and receive to/from the same port B4J code required to use two different UDP sockets, one listen data from ESP and send to Emulator, another listen data for Emulator and send back to ESP.

I explain here my steps:
  1. Go to Android/emulator folder, start a command Prompt here. Manually start emulator with this command. This start emulator without WiFi connection:
    emulator.exe -avd 10in_Tablet_Platform_28_default -feature -Wifi (change android avd name with your)
  2. Even if the emulator has no WiFi connection, B4A can communicate with it over ADB, so if Android app is compiled, this executes on the emulator.
  3. Start another command Prompt and type the follow command (you need telnet enabled, on Win11 I enabled it in the custom settings), note that you can probably do it in other ways, just need to connect to emulator:
    telnet localhost 5554
  4. After that You will be asked to write the auth login, as the prompt says, you can find it in a text file called .emulator_console_auth_token in your User folder. Open it and copy the token.
  5. Return on command prompt, type:
    auth (space)
    then paste the token and press ENTER
  6. Now we are connected to Emulator console. Now we need to redirect PC UDP port to emulator, considering we use a port 8888 the command is:
    redir add udp:8888:8888 (You can map different ports, eg. redir add udp:8888:4000 will map pc UDP port 8888 to emulator port 4000, even viceversa should work)
  7. Press RETURN and emulator can now receive incoming packets on port 8888. No, it cannot, to do this we need another step, redirect with B4J UDP packets to/from ESP and Emulator. To do this here my last code, note that here now not just need input and output UDP ports, but more.... If you receive on one port and send to another one where another socket listen, B4J send and receive packets that sent itself in an infinite loop.
In my code B4J receive an UDP ping packet from ESP and send to emulator on 127.0.0.1:8888 then on the emulator my app receive the packet, parse it and reply to a ping request. My app originally reply on Packet.HostAddress, but now I placed in the settings a way to force output IP Address, this because as @teddybear says (thanks) the emulator shoud not send to ESP address, but to PC IP Address where B4J listen. At this point B4J finally receive the packet and send it to ESP over WiFi.

I would request your help to make it fully working and just make a simple B4J app that automatize all the manual work by just insert emulator port and press a button.

Here my last code, note that ESP IP Address and PORT to reply back are automatically populated when a packet arrive from ESP, these will be next used to reply back to ESP. Note even that I managed udp data to remove spurious characters from data length to a listening buffer size, without this probably need to use line feed terminator. This is something to fix because UDP packets need to be as fast possible and have no much sense convert bytes to string and then convert back to bytes to send over the socket:
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
 
    Private Button1 As Button
 
    Private UDP1 As UDPSocket
    Private UDP2 As UDPSocket
 
    Private EMU_PORT As Int
    Private OUT_IP As String
    Private OUT_PORT As Int

    Private TextField1 As TextField
    Private TextField2 As TextField
    Private TextField3 As TextField
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
End Sub

Sub Button1_Click
 
    If Not(IsNumber(TextField1.Text.Trim)) Then
        fx.Msgbox(MainForm, "Please insert a valid Android Emulator port.", "Message")
        Return
    Else
        EMU_PORT = TextField1.Text.Trim
        If Not(EMU_PORT >= 1024) Or Not(EMU_PORT <= 65535) Then
            EMU_PORT = 0
            fx.Msgbox(MainForm, "Please insert a valid Emulator port in range (1024-65535)", "Message")
            Return
        End If
    End If
 
    UDP1.Initialize("UDP1", 7888, 500)  ' From ESP
    UDP2.Initialize("UDP2", 8889, 500)  ' From Emulator
'    UDP2.Initialize("UDP2", 7889, 500)  ' From Emulator
 
    Button1.Text = "Listen incoming ESP data on Port " & UDP1.Port
End Sub

' If received data from ESP ...
Private Sub UDP1_PacketArrived (Packet As UDPPacket)
'    Log("Received from ESP " & Packet.Data.Length & " Bytes from " & Packet.HostAddress & ":" & Packet.Port)
'    Log("Redirect packet to emulator: 127.0.0.1:" & EMU_PORT)
'    Dim pkt As UDPPacket
'    pkt.Initialize(Packet.Data, "127.0.0.1", EMU_PORT)  ' Redirect data to emulator
'    UDP1.Send(pkt)

    OUT_IP = Packet.HostAddress ' Assign it so we reuse next to send back repLies to ESP
    OUT_PORT = Packet.Port      ' Assign it so we reuse next to send back repLies to ESP
 
    TextField2.Text = OUT_IP    ' Auto populate the text field
    TextField3.Text = OUT_PORT  ' Auto populate the text field
 
    Dim msg As String = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
    Log("Received from ESP a packet of " & Packet.Length & " Bytes  ->  " & Packet.HostAddress & ":" & Packet.Port & "  Data: [" & msg & "]")
    Log("Redirect packet to Emulator: 127.0.0.1:" & EMU_PORT)
 
    Dim data() As Byte = msg.GetBytes("UTF8")
 
    Dim pkt As UDPPacket
    pkt.Initialize(data, "127.0.0.1", EMU_PORT)  ' Redirect data to emulator
    UDP1.Send(pkt)
End Sub

' If received data from emulator ...
Private Sub UDP2_PacketArrived (Packet As UDPPacket)
'    Log("Received from Emulator " & Packet.Data.Length & " Bytes from " & Packet.HostAddress)
'    Log("Redirect packet to ESP: " & OUT_IP & ":" & OUT_PORT)
'    Dim pkt As UDPPacket
'    pkt.Initialize(Packet.Data, OUT_IP, OUT_PORT)  ' Redirect data to outside
'    UDP1.Send(pkt)
 
    Dim msg As String = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
    Log("Received from Emulator " & Packet.Length & " Bytes from " & Packet.HostAddress & ":" & Packet.Port & "   [" & msg & "]")
 
    Log("Received from Emulator a packet of " & Packet.Length & " Bytes  ->  " & Packet.HostAddress & ":" & Packet.Port & "  Data: [" & msg & "]")
    Log("Redirect packet to ESP: " & OUT_IP & ":" & OUT_PORT)
 
    Dim data() As Byte = msg.GetBytes("UTF8")
 
    Dim pkt As UDPPacket
    pkt.Initialize(data, OUT_IP, OUT_PORT)  ' Redirect data to emulator
    UDP1.Send(pkt)
End Sub
 
Last edited:
Upvote 0

teddybear

Well-Known Member
Licensed User
I guess you have done the VirtualDiaplay app in android, I am curious why not create a B4J app for it as Erel said post #2.
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
I guess you have done the VirtualDiaplay app in android, I am curious why not create a B4J app for it as Erel said post #2.
Yes, this is my goal,

as I wrote here I still search to do it, here I explained what I do, but to get it working I need the community help:
I would request your help to make it fully working and just make a simple B4J app that automatize all the manual work by just insert emulator port and press a button.
This is not something related to my VirtualDisplay app, no need at all this part my app.

I just need it to test this app (and in future other apps) in a big 24 inch display instead of my phone, so this not only for me, but everyone have the necessity to do similar things, communicate with emulator by starting a communication outside.

The inverse method starting communication from emulator works and I've used it a lot to test my apps.

As I explained it is not even related to ESP8266/ESP32, but extended to any device capable to send/receive packets over socket, so a smarthphone, a tablet, a PC, Raspberry etc...
 
Last edited:
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
In past and some days ago I have posted a lots of Code Snippets and Tutorials with a lots of Examples and Open Source Code (see my overview). Even on github ...

This to search help peoples solve common problems .....
And and no one helps me when I have a problem ?
That's a good thing.o_O

Thanks guys, I am very happy that I not able to continue my work because I can not solve the problem and no one helps me. ? Today I sleep.
 
Last edited:
Upvote 0

teddybear

Well-Known Member
Licensed User
You have resolved the issue with this thread,haven't you?
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
You have resolved the issue with this thread,haven't you?
Absolutely noo....

I've written it in bold already 2 times that I look for help to make this to work.

Now it doesn't work and if no one helps me the time I put to search to know it and test with B4J code was all wasted time, as well as I can't do what I need.

On all my solved threads you can see the [SOLVED] in the thread title, this even to help peoples.
Unless you don't see the [SOLVED] on this thread the problem was not solved.
 
Last edited:
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Please any help on this..... ?
I want make it to work, I even managed with B4J.
Now emulator can receive UDP packets but cannot reply back.

Thanks
 
Upvote 0
Top