I’m getting “NetworkOnMainThreadException”
I’m sending UDP packets from a AsyncTask but from a Sensor event, my guess is that the Sensor event triggers on the main UI thread.
I want to send UdpPackets when new values are available from the sensor, what is the best way since I cant do it from the sensor event?
Spawning a thread for each sensor event isn’t very good, and have a separate thread that polls new data also has problems (WarrenFaith answer).
I ended up using a CyclicBarrier, like this, a infinite loop inside a seperate thread
The thread will halt on the
sync.await();lineUntil new data is available from the sensor
And
sync.reset();is called, which will let the poller thread proceed