'Partial wake lock releases when screen turns off
Im building a Cordova/Ionic app which lets a user view the camera of an Andoroid cellphone live using webrtc (peerJs).
I need to allow the device to turn off the screen after (let's say) 15 seconds, and keep transmitting the video to other Android device.
As far as I know to deal with this problem I need to aquire a PARTIAL_WAKE_LOCK (This allows the screen to be turned off and keeps the app running), and never release it, so the app keeps sending me the video endlessly.
I decided to go for an Android native implementation, so i acquired the lock in the "onStart()" method, present in platforms\android\CordovaLib\src\org\apache\cordova\CordovaActivity.java with this code present in almost every tutorial
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"My wakelook");
wakeLock.acquire();
I first tried the code above, aquiring a SCREEN_DIM_WAKE_LOCK. And it worked, the screen never turned off and It kept me sending the video endlessly. This makes me think that the wakelock adquisition is successful. When I changed the code to PARTIAL_WAKE_LOCK, the screen just turns off after 15 seconds and the video just freezes.
I've read that the class that acquires the wakelock has to extend "Application", but this class CordovaActivity extends Activity and I cant make it extend another class neither replace "Activity" with "Application". I dont know if this is neccesary since the code above works fine using SCREEN_DIM_WAKE_LOCK.
I tried using this plugin https://github.com/boltex/cordova-plugin-powermanagement to aquire the partial wake lock, but it does just the same as above.
Thanks
Solution 1:[1]
Turns out that the wake lock wasnt releasing. What happens is that the programming of the camera is tightly coupled to the state of the screen. Even though the CPU was still on, turning off the screen disabled the camera, so thats why the video freezed.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | emilianop11 |