[Atrix 4G Android Development] sendust7:
“Bluesleep” is a wakelock error in the Bluetooth system which prevents Android devices from entering deep sleep, thus shortening battery life in sleep mode.

This error also affects wifi.

According to XDA threads (see References), Bluesleep Fix (BSF) greatly improves battery life on CM9 and CM10 ROMs by allowing your device to truly enter deep sleep.

BSF dates back to mid-2012 perhaps earlier. So I think most of the newer Atrix ROMs already incorporate BSF or some variation of it.

However my CM10 SuperLite V8 ROM does not (yet :)). Also BSF may not be well known to all users. Or they may want to know how to quantify the improvement in battery life, if any that BSF yields.

So I tested BSF extensively using my new BSF ON/OFF script “bsf.sh” I developed on SuperLite and found that BSF greatly reduces battery discharge rate in sleep mode.

Link: http://www.mediafire.com/view/wqrhb9isx4rvjdh/bsf.sh

Features

My BSF script allows users to set BSF ON/OFF at run time and can be invoked via Script Manager, Terminal Emulator, ADB or other suitable app. There is no need to enter recovery, flash and reboot; however reboot is required for Android to recognize the modified permissions. My handy BSF ON/OFF script allows quick switching back to the legacy “root-only” permissions should BSF cause any adverse effects.

My BSF script also analyzes battery discharge rate by first time stamping the percentage charge level at launch. The difference in charge divided by dwell time T between launches yields estimated discharge rate in % per hour. Since the percentage “charge” is output as a whole number, and since two charge states are required to estimate rate, the uncertainty due to limited resolution is approximately 2/T % per hour. Thus for a dwell time of, say T=8 hours, this uncertainty is 2/8 = 0.25 % per hour.

Demo

BSF OFF

BSF ON

BSF NOOP (print usage instructions and current state)

Instructions

BSF ON/OFF can be launched many different ways. Like the demo, the method given here invokes ScriptManager (SManager) which is included in some CM10 ROMs including SuperLite. You must have root.

Push bsf.sh to internal or external memory, for example: /sdcard/Download

Launch SManager and select Menu > Advanced > Configuration > Home directory. Enter {path-to-bsf.sh} and “Select this directory”

At SManager command window, enter “sh bsf.sh”.

In the Argument field, enter 1 (for BSF ON), 0 (for BSF OFF) or blank to show current state

Press “Su” button to get superuser then press “Run”.

Once bsf.sh is launched, SManager stores all entered parameters as defaults for the next launch.

A reboot is required for the system to recognize the modified permissions.

When bsf.sh is launched with an argument of 1 or 0, battery statistics are printed and the estimated discharge rate is displayed in the right-most field. Negative sign indicates that the device was discharging on average during the past cycle.

BlueSleep Fix Technical Description

BSF is employed by setting the “setuid” bit (set user ID upon execution) via the UNIX command “chmod”. The setuid bit is an access rights flag that, when set, allows other entities (users, programs or apps) to run an executable with the permissions of that executable’s owner. For example:

The following legend shows how to interpret the bits and the corresponding permissions:

When you flash a ROM, the updater-script inside the ROM zip sets permissions for various files:

set_perm_recursive

sets permissions of a directory or set of directories and all files and folders within them. Syntax:

set_perm_recursive(uid, gid, dirmode, filemode, dir1, dir2 …)

where

In the following case:

set_perm_recursive(0, 2000, 0755, 0755, "/system/xbin")

the files are owned by root (numerically “0”) and the setuid bit is not set. Thus only root has elevated privileges. However when BSF is invoked, the setuid bit “s” is set and replaces the “x” (execute) bit for the owner (root), thus allowing apps to run Bluetooth executables with the same permissions as root ...

cd /system/xbin; ls -ld hci*
-rwsr-xr-x root shell 67144 2008-08-01 05:00 hciconfig
-rwsr-xr-x root shell 116564 2008-08-01 05:00 hcidump
-rwsr-xr-x root shell 35704 2008-08-01 05:00 hcitool

Performance Testing

I ran numerous BSF ON/OFF tests on CM10 SuperLite. Dwell times between consecutive launches of bsf.sh were generally 8 to 10 hours. The following results show estimated battery discharge rate in sleep mode: Display OFF, phone signal ON, WiFi switch ON, Bluetooth switch OFF and charger unplugged:

With BSF OFF the average discharge rate was 2.7% per hour. However with BSF ON the average discharge rate was only 0.8% per hour. For example, after 12 hours of sleep time, I have an average 23% more charge with BSF ON:

Operational test results: Starting from full charge, 56% charge still remains after 24 hours, including 8 hours of sleep and 16 hours of combined low-to-moderate use + sleep.

Final Thoughts

Newer ROMs may already have BSF. But for CM10 SuperLite, I have demonstrated that with BSF, battery discharge rate in sleep mode is reduced by 70% or more. Now I get almost two days of battery life with low to moderate use. Based on these results, I plan to roll BSF into my next version of SuperLite.

References

BSF Diagnosis: http://www.droidxforums.com/forum/d...sleep-fix-all-ics-roms-enable-deep-sleep.html

BSF Discussion: http://xdaforums.com/showthread.php?t=1817424

Credits

Th3Bill (http://xdaforums.com/member.php?u=4480697), author of bluesleep_fix based on settings from olegfusion. Thanks Bill :)