Deconstructing Doze Mode
The Myth of the "Foreground Service"
Many developers (and pilots) believe that simply showing a persistent notification (Foreground Service) makes an app immune to Android's power saving features. This is false.
Two Types of "Sleep"
Android has two distinct power-saving mechanisms:
1. App Standby (The Bucket System)
- What it does: Sorts apps into "Active", "Working Set", "Frequent", and "Rare" buckets. Limits job execution and network access for "Rare" apps.
- Foreground Service Effect: Exempt. As long as the notification is visible, the app is considered "Active".
- Result: The app won't be throttled individually.
2. Doze Mode (The Coma)
- What it does: When the device is stationary with the screen off for a period of time, the entire system enters a deep sleep.
- Restrictions:
- Network: Blocked. UDP packets from your drone will be dropped.
- Wake Locks: Ignored. The CPU suspends even if you hold a partial wake lock.
- Alarms: Deferred.
- Foreground Service Effect: NOT Exempt. A foreground service will be suspended by Doze.
The Solution: "Ignore Battery Optimizations"
To survive Doze Mode, MAVLink GPS must be added to the User Power Save Whitelist.
- Action: The user must go to Settings > Apps > MAVLink GPS > Battery > Unrestricted.
- AOSP Mechanism: This adds the package to the
mPowerSaveWhitelistUserAppslist inDeviceIdleController.java. - Result: The system grants the app network access and honors its wake locks even when the rest of the device is dozing.
USB Host Mode Exception
There is one loophole. When the phone is acting as a USB Host (powering the Flight Controller or communicating via OTG):
- The kernel driver often holds a low-level wake lock to manage the bus traffic.
- This can prevent the device from entering deep sleep, effectively bypassing Doze without user intervention.
- Warning: This depends heavily on the specific kernel implementation of the phone manufacturer (Samsung vs. Pixel vs. Xiaomi). Do not rely on it. Always whitelist the app.
Source Code Reference
- Idle Controller:
frameworks/base/apex/jobscheduler/service/java/com/android/server/DeviceIdleController.java- Manages the whitelist and Deep Doze transitions.