I'm building an app to control led bulbs (Mi-Light, limitlessLed and the likes). The purpose of the app is to have effects that the user can select and let run for an indefinite amount of time. For example, a "candle light" effect might change the bulbs color between shades of yellow and red, more or less randomly, until the user decides to stop.
Quick background info: the way these bulbs are controlled is through UDP packages which are sent over the WiFi network. Consequently, I need the app to keep on sending such UDP packages even when the device is sleeping.
After researching a little, I ended up making use of a wakelock in order to let the device broadcast UDP packages through the WiFi network even when sleeping (please do tell me in case there is a better approach I didn't find out about).
Everything works fine for some minutes (maybe 10?), until the device seemingly goes into some sort of deep sleep mode and stops sending packages over the network.
How can I prevent this from happening? And more generally speaking, what is a good approach I should take in order to accomplish what described above?
Just for reference, here's a sample snippet of code which just rotates through an array of 7 colours:
[...]
private static boolean animationRunning = false;
private Timer timer = new Timer();
private PowerManager mgr = (PowerManager)getReactApplicationContext().getSystemService(Context.POWER_SERVICE);
private PowerManager.WakeLock wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
[...]
public void startAnimation(final int step) {
animationRunning = true;
wakeLock.acquire();
final int[] steps = { 0, 66, 99, 122, 166, 199, 255 };
resetTimer();
timer.scheduleAtFixedRate(new TimerTask(){
int curstep = 0;
@Override
public void run(){
byte[] Bytes = {64, (byte)steps[curstep], 85};
try {sendUdp(Bytes);} catch(IOException e) {};
curstep = curstep + 1;
if(curstep == steps.length) {
curstep = 1;
}
}
},0,1000);
}