What it does:
- Script I: Switch to 2G when screen is locked and switch back to 4G when screen is unlocked. TELEports or Dekko notifications work well.
- Alternative script II: Switch to 2G when screen is off and switch back to 4G when screen is on (regardless of screen lock). TELEports or Dekko notifications work well.
- Alternative script III: Turn off cellular data when screen is off and turn on cellular data again when screen is on (regardless of screen lock). No TELEports or Dekko notifications when screen is off.
Precondition: Working 4G connection which drains battery when device is inactive (screen is locked or off).
Originally made for Xiaomi Mi A2 and tested on it but can be useful for any device.
Create a script "/home/phablet/lte-battery-saver"
Script I:
#!/bin/bash
primary_preference="lte"
saving_preference="gsm"
sim_slot="/ril_0"
interface=org.freedesktop.DBus.Properties
member=PropertiesChanged
dbus-monitor --session "type=signal,interface='${interface}',member='${member}'" |
while read -r line; do
if [[ ${line} == *"com.canonical.UnityGreeter"* ]]; then
read; read; read -r line
if [[ ${line} == *"IsActive"* ]]; then
read -r line
[[ ${line} == *"true"* ]] && /usr/share/ofono/scripts/set-tech-preference "${sim_slot}" "${saving_preference}" 1>/dev/null
[[ ${line} == *"false"* ]] && /usr/share/ofono/scripts/set-tech-preference "${sim_slot}" "${primary_preference}" 1>/dev/null
fi
fi
done
Make script executable
chmod u+x /home/phablet/lte-battery-saver
Create an upstart session job "/home/phablet/.config/upstart/lte-battery-saver.conf"
description "LTE battery saver"
start on started dbus
stop on stopped dbus
exec /home/phablet/lte-battery-saver
Start the job
start lte-battery-saver
or reboot the phone.
NOTES (Script I and Alternative script II):
- Change "/ril_0" to "/ril_1" if your data SIM is in slot 2 (see sim_slot variable)
- If switching back doesn't work, try to set primary_preference variable to "any"
- To switch between 3G and 2G replace "lte" with "umts" (see primary_preference variable)
- To switch between 4G and 3G replace "gsm" with "umts" (see saving_preference variable)
To apply the changes, restart the job
restart lte-battery-saver
or reboot the phone.
To remove all changes
stop lte-battery-saver
rm /home/phablet/.config/upstart/lte-battery-saver.conf
rm /home/phablet/lte-battery-saver
Alternative script II:
#!/bin/bash
primary_preference="lte"
saving_preference="gsm"
sim_slot="/ril_0"
interface=com.canonical.Unity.Screen
member=DisplayPowerStateChange
dbus-monitor --system "type=signal,interface='${interface}',member='${member}'" |
while read -r line; do
if [[ ${line} == *"int32 0" ]]; then
read
/usr/share/ofono/scripts/set-tech-preference "${sim_slot}" "${saving_preference}" 1>/dev/null
elif [[ ${line} == *"int32 1" ]]; then
read
/usr/share/ofono/scripts/set-tech-preference "${sim_slot}" "${primary_preference}" 1>/dev/null
fi
done
Alternative script III:
#!/bin/bash
interface=com.canonical.Unity.Screen
member=DisplayPowerStateChange
dbus-monitor --system "type=signal,interface='${interface}',member='${member}'" |
while read -r line; do
if [[ ${line} == *"int32 0" ]]; then
read
dbus-send --type=method_call --dest=com.ubuntu.connectivity1 /com/ubuntu/connectivity1/Private org.freedesktop.DBus.Properties.Set string:com.ubuntu.connectivity1.Private string:MobileDataEnabled variant:boolean:false
elif [[ ${line} == *"int32 1" ]]; then
read
dbus-send --type=method_call --dest=com.ubuntu.connectivity1 /com/ubuntu/connectivity1/Private org.freedesktop.DBus.Properties.Set string:com.ubuntu.connectivity1.Private string:MobileDataEnabled variant:boolean:true
fi
done