From what I understand you are trying to do, this is what I would try next..
1) Create a service that it's sole purpose is to send data via UDP.
2) When the service starts, it will create the UDP object before it sends the data.
3) The service then sends the desired UDP data
4) After the data is sent, it will close and destroy the UDP object
5) The service will then stop itself
Then call this service (passing the data and port info in global vars) every time you want to send UDP data.
See if this will then reliably send UDP packets.
Thanks
@JohnC for tips,
my app works as WiFi TFT display for ESP8266/ESP32, for these micros I developed a library in Arduino enviroment, ESP send data as fast possible, Android receive fast commands, like 200-1000 every second.
For these reasons the UDP socket should start once and always remain active, I never close (destroyed) it, and is a problem reinitialize the socket before send back any packet, require time.... and I still search to optimize and decrease time.
Note that it send just some acknowledge packets to remote requests, eg, returns screen width, height, ping etc.
The main work here, is to receive (and parse) packets and socket should always be active to no miss packets sent from ESP.
I already used Services and it shoud work in background until the app is manually closed and Service destroyed, yes.
I will try to completely move UDP on a Service as Your and
@Erel advices and initialize once on Service_Create.
May I can use Starter Service ? This is already in the project and never used.
I will try it, but I do not know at all because after VR event sub the socket (in B4XPages without Service) will end up the socket to be broken. I used a lots sockets and never encountered a similar problem. Please can you help me to know because this happen ?
Thanks