yashsingh1/xdaforums huggingface dataset:
[Magisk] RadarNyan:
This is the first time I tried to use Battery Life Extender, I installed it via Magisk, sadly it didn't work on my phone.

After install (and reboot) I ran 'blex --configure' with termux (after 'su'), following on-screen message hit enter (my phone is already charging) and it took a while, during waiting my phone stopped charging for a brief moment and returned to charging state. After it's done I don't see anything indicates that blex is working, so I ran 'blex --info', it says my battery is in good health, charging, and the auto switch thresholds are 80, 70... blahblahblah, and in the last line it says 'Daemon State: Not running'

I thought I might need to reboot my phone so I did just that, after rebooting I ran 'blex --info' again, it still says the daemon is not running. So I ran 'blex --mkdaemon', it says '-Starting blex daemon - Done' yet when I check with 'blex --info', it still says 'Daemon State: Not running'

Needless to say, my phone keeps charging above 80%, I guess that means blex is not working at all?

Logs attached.

(Xiaomi MI MIX 2, OmniROM 8.1 official, Blex version 0.1.3.1)
[Magisk] Jaymin.0.1910:
Thanks to the community, you have already gotten your answer :D

Like many other users reported, the daemon is not running.

The module is supposed to stop charging the very first time the threshold is reached.

To every user reporting daemon not running issues, I have analyzed the code many times and nothing seemed wrong to me, so I suspect something much suspicious going on behind the scenes (some kind of root task manager or optimization), so I will soon push a new build with the suspicious part of the daemon removed until I can figure out what's that behind the scenes thing.

I ask a humble apology to users who were disappointed and look towards further involvement of you, the community to keep using blex and also help me make it better (by suggestions, request and bug reports (with logs)).

Best regards to all.

Jaymin Suthar.
[Magisk] cesar_9046:
Works with Blex 1.3.3 version
[Magisk] Dead-neM:
Yeah blex is working like a charm since 1.3.2!

I just have a little question about the power state.

I know that it will shutdown the phone when battery reach 20%. Ok

But how? I don't like when for exemple xposed or other apps use a force reboot method that goes directly in reboot.

Android needs to close some stuff and write some things too before phone gets completely off. Like any os in fact.

So which method blex is using to power off the phone?
[Magisk] Jaymin.0.1910:
Well, I'm glad blex is working good.

Yes, it will shutdown the device at 20% and for the question you ask, blex will first try to use the BusyBox' poweroff applet which sets the init propery sys.powerctl to shutdown the android, and if that fails (due to busybox execution issues) it will directly setprop the same. This isn't a force reboot method since every required action will be taken by the init binary itself.

Best regards.
[Magisk] J_M_V_S:
[MENTION=8570610]JayminSuthar[/MENTION] I get "ERROR: Failed to configure blex"

LOG: https://drive.google.com/file/d/129iDLnZaJJvCOsFq8jEnHntgv2Isexdn/view?usp=sharing

OnePlus 3T Stock OOS 5.0.5 Magisk 17.1
[Magisk] louas:
Blex 1.3.3 work with mi6 miui10
[Magisk] RadarNyan:
I just tried blex 0.1.3.3 (OmniROM 8.1, Magisk v17.1)

After install I rebooted my phone (to let magisk load module) after reboot I ran 'blex --configure', after that I tried 'blex --configure' yet it still says: "Daemon State: Not running" I tried to reboot my phone, still not running.

However, this time using 'blex --mkdaemon' the output is different. It prints out a line above "- Done":

6362 root 0:00 {blex} /system/bin/sh /system/xbin/blex --skip-logs --run-daemon

Now when I check with 'blex --info', it first prints out the same line above, and it says "Daemon State: Running"

After that I wanted to turn off auto shutdown feature, so I ran 'blex --toggle power OFF', and I checked with 'blex --info'

It says the Auto Power state is OFF, but now I have two lines above:

6362 root 0:13 {blex} /system/bin/sh /system/xbin/blex --skip-logs --run-daemon
7879 root 0:12 {blex} /system/bin/sh /system/xbin/blex --skip-logs --run-daemon

I thought that might be a bad thing, so I ran 'blex --mkdaemon stop' and both daemons are stopped. Then I tried to restart the daemon with 'blex --mkdaemon', it starts 2 new daemons again.

I'm kinda confused right now. Logs attached.

----------

After I submit this post, I checked my phone again with 'blex --info', one of the daemons is gone (I didn't do anything) and my phone stopped charging when it reaches 80%. So blex now works for me... I guess? With the only problem being that I have to manually fire up termux to start the daemon every time I reboot my phone.

----------

Just now I connected my phone to the charger, it went back and forth between charging and not charging, I heard the charging sound like... 5~6 times. It's a brand new cable so I can say for sure that's not a connection problem, so I checked 'blex --info' again and I got two daemons running.

So yeah... blex works for me but very unstable. I'll just uninstall it for now to be safe. Logs attached (new_logs.zip)
[Magisk] neekless:
Daemon is running in 0.1.3.3 but I found that the time based method for disabling charging will disable it but not enable it back. E.g I disabled charging for 5 seconds but after 5 seconds charging remains disabled and I have to manually enable it back. I took a look at the code.

charge() {
case "$1" in
--enable) key_init=enable ;;
--disable) key_init=disable ;;
*) abort 'Invalid argument(s)' ;;
esac

if test -z "$2"; then
abort 'Missing argument'
fi

setup_exit --test-methods

print 'Parsing argument'
if echo $2 | grep -Eq '^[0-9]+%$'; then
print --info 'Level based method call detected'

end_level=$(echo $2 | tr -d %)
limits_wrap $end_level 1 99

elif echo $2 | grep -Eq '^[0-9]+[smh]?$'; then
print --info 'Time based method call detected'
else
abort 'Invalid format'
fi

print 'Catching blex daemon'
set_prop switch_do $key_init

print "Keeping charging ${key_init}d until $2"
if ! sleep $2; then
while test $(batt_level) -ne $end_level; do
sleep 1
done
fi

print 'Releasing daemon'
set_prop switch_do default
}

I think you're missing a 'sleep $2' under the time based method call. Since '! sleep $2' will always be true, and end_level is undefined, the while loop may be running forever and switch_do never gets set back to default. I think this is what's happening, please correct me if I'm wrong.
[Magisk] catacomb71:
How to configure?

I flashed my new rom and rooted it. I found this magisk module useful.But it says it wont work untill i configure it.

I dont want to mess things up.

I want the module to charge and discharge alternatively to extent battery life and prevent heating.

My phone is moto g4 plus on lineage 15.1.

I want the threshhold to be 95% and to start charching again at 80%.

can anyone please write the terminal emulator code which include the above requirement?
[Magisk] TatsuyaKaneki2015:
Hmmm this sounds very noob. . But i cant seem to find how to change values of the thresholds. The first one with 80,70

---------- Post added at 07:20 AM ---------- Previous post was at 07:18 AM ----------

This sounds very noob but i cant seem to find how to change values of the threshold the first one with 80,70 i want to set it 99,50 please. . Thank you
[Magisk] Jaymin.0.1910:
This logs are really big and I have college exams to attend currently, so I will get back to you once I have analyzed your logs. I hope you understand.

Please check the new release version 0.1.4 which addresses both the issues, and for the daemon not launching on boot issues, your logs didn't provide info relating that. Can you remove the --skip-logs flag from /sbin/.core/img/blex/post-fs-data.sh and then provide me logs after 2 minutes from rebooting) ??

Hey, the code is correct, the while loop is executed only when the $2 is unrecognized by sleep command, which is the case for level-based methods, so the code is correct, and if you saw '- Releasing daemon' on the screen, then that's a proof of it. The reason behind charging not enabling back might be that the battery level would be outside Auto Switch thresholds, at where Auto Switch will override the charging state as long as the method has finished. Please quote me back if that's not the case :)

Kindly run

blex --configure
blex --enforce --update switch 95 80

in the terminal and blex will be all set to serve you.

Kindly run

blex --enforce --update switch 99 50

to set Auto Switch thresholds.
[Magisk] cesar_9046:
Version 0.1.4 works perfectly ;)
[Magisk] neekless:
Hey I think you're right. I think the problem is I tried the manual disable right after I freshly installed it and configured it. The default thresholds are 80 70 and my battery level was between them. So after I ran manual disable for 5s (where switch_do changed to disable and switch changed to --OFF), the switch_do changed back to default. Since my battery level did not trigger 'test $(batt_level) -le $(thr_enable)', switch never changed back to --ON. This is because since it was a fresh install and I have never triggered the auto switch conditions, my stat_saved is default and the elif $revert part never triggers to change switch back. Maybe for the elif $revert part if stat_saved -eq default you can set switch to --ON? What do you think?
[Magisk] bisherbas:
Example needed

Can you provide an example command for auto switch feature? I can't seem to get the blex command to work with various parameters I have tried so far.
[Magisk] neekless:
Examples:

* 'blex --update switch 85 65' > Update auto switch thresholds to 85 65

* 'blex --update switch 55' > Update auto switch disable threshold to 55

* 'blex --enforce --update switch 95' > Force update auto switch disable threshold to 95

* 'blex --enforce --update switch 95 90' > Force update auto switch thresholds to 95 90

* 'blex --update switch' > Revert auto switch thresholds to default
[Magisk] Jaymin.0.1910:
You got it all correct, methods will not be reverted if Auto Switch was never triggered, and thanks for the suggestion, but I think defaulting the revert state to ON will be less convenient, rather a better idea will be to detect charging state before running daemon and revert to that when $revert is true, and another idea is to change state to the inverse of state used by the method, but I'm a bit confused which one to use. Can you suggest me ??

Check out the GitHub :cool:

Best regards.
[Magisk] RadarNyan:
I installed 0.1.4, this time after 'blex --configure' the daemon is running (according to 'blex --info')

About daemon on boot issue, I opened the file you mentioned and realize the daemon command is after 'sleep 120', I'm just checking for it too early. I waited for 2 minutes and checked again, the daemon starts just fine. What's the reason behind this 2 minutes wait?

Although I understand the set-and-forget concept, I still think it would be nice to have an app to provide GUI for configure blex and provide additional functions, for example: I don't like blex just shut my phone down when it hits the auto power thresold (so I turned it off) but I still want to get some kind of indication, like a notification tell me that my battery is low and I should connect my charger.

I know there are apps out there can do this already, don't you think it makes more sense to make something like that a part of blex? :)
[Magisk] neekless:
Actually methods not reverting only happens in very specific circumstances:

--run-daemon is executed so stat_saved=default

--manual is executed so revert=true

batt_level is between thresholds so test $(batt_level) -ge $(thr_disable) and test $(batt_level) -le $(thr_enable) never triggers, so stat_saved is not changed

If the Auto Switch conditions are never triggered, then by default the phone will charge normally, that's why I suggested to revert switch to --ON.

The only way to go against this would be if someone

triggered $(batt_level) -ge $(thr_disable) so switch --OFF and stat_saved=OFF (or triggered $(batt_level) -le $(thr_enable) so switch --ON and stat_saved=ON)

then used the phone till batt_level is between thresholds (or charged the phone till batt_level is between thresholds)

THEN execute --run-daemon again so that stat_saved=default

This way switch remains --OFF/--ON and after manual enable/disable it will not switch back to --OFF/--ON due to stat_saved=default. I think these circumstances are too specific to not just revert switch to --ON for simplicity. But if you still think it's not a good idea, then I think detect charging state before running daemon and revert to that when $revert is true is good.
[Magisk] Jaymin.0.1910:
Well, if the user had battery below the power threshold and they still needed to use the phone, the best way for them is to toggle the Auto Power feature OFF, and the 2 minutes wait was implemented just for the same reason.

Yeah, notifying rather than shutting down is great, and I will love to have an app for that, but still, that's gonna require a lot of time to build GUI, and I currently got my college exams going on, but as soon as I got enough time, I will plan for that, too.

Oh yes, you're totally right about the default charging state being ON when the daemon ran, and I also totally agree with when $revert will not work properly, and that might make switch --ON the default behavior when $revert, but still...

I will have to think about the another case, when some task manager killed blex daemon, or it got destroyed anyhow, and the user had to restart it with --mkdaemon, then the default charging state will be last charging state of the previous daemon (which was killed), and that will be unknown, so I think I will go with detecting the state before running the daemon.

A very big thanks for the sharing of ideas and giving me a reflection of user-experience associated with blex :highfive:

Best regards.
[Magisk] blue whale:
hello,

I am not sure if blex is working well for my phone. My phone is a xiaomi redmi 3 pro SE, ie a KATE, and I run resurrction remix 7.1 with magisk 17.1 I just installed blex, so it is the latest version.

I put ''blex --configure'' and it asks if the phone is charging, I press enter to mean yes [phone is 98% of battery and charging], then after some time, the shell goes back and blex is configures. But even after a restart of the phone, when the phone is charging with a little yellow lightning in the status bar, blex reports the battery is not charging.

And in ''settings-> battery'', it is reported has ''not charging''.

So is blex well configures or not?

this is the output of ''--info''
[Magisk] blue whale:
I add that the software ''ampere'' reports indeed that the phone is charging, even when blex claims ''not charging''
[Magisk] Dead-neM:
Well, i just tried it and phone got directly into a blackscreen. No android is shutting off screen etc...

The way it shutdown the phone is not good at all actually.

I can suggest you something

https://android.stackexchange.com/q...-to-restart-android-via-command-line-remotely

In this link you have a nice discussion about how to reboot phone with proper command

On KitKat and lower version it's{<=4.4}

i don't know for the power off i have no device under marshmallow. We just need to find what to put at the place of "REBOOT"

su -c 'am start -a android.intent.action.REBOOT'

On lollipop and above it's{>=5.0}

This command will power off the device just like you do normally

su -c 'svc power shutdown'

As said in the link

I'll add that as the blex script run as rooted i don't think we need to put "su -c" at the beggining of the command. But im not sure about this
[Magisk] Jamess27:
I have quiet a bit battery drain after charging to the set value and keeping the phone connected.
[Magisk] Jaymin.0.1910:
blex actually communicates with the kernel using the sysfs interface, which is a filesystem emulation everything the kernel can be set for. So, if blex said it was not charging, it's the kernel which said that. The Ampere application might be measuring something else than the standard sysfs, I'm not sure, but I doubt blex was right at the time.

Yeah, I actually don't have an older device to test that, so I directly browsed through the BusyBox poweroff applets source code in patched by topjohnwu. But I will test suggestion from the link too and make necessary changes. BTW, the Auto Power feature will shut the phone down directly without a GUI notification, that's normal, but if you have any issues rebooting back, then that indicates the last shutdown had errors.

A recent commit (possibly) addresses this issue :)

Best regards.
[Magisk] neekless:
I found this chart (attached). Seems like using sys.powerctl is a pretty low level method and not a graceful shutdown. I do not know if it will cause any problems but from what I've read 'svc power shutdown' is a graceful shutdown method. Then again, 'adb reboot' is also a very ungraceful shutdown method yet people do it all the time, so it probably isn't too harmful.

Some links for reading:

https://android.stackexchange.com/a/130063

https://android.stackexchange.com/a/138057

On a separate note, I have some questions for you:

1) Under --run-daemon),

elif $revert; then
switch --$stat_saved
revert=$write
fi

What is the reason to use revert=$write instead of revert=false? Revert is only true when using manual method, so after that, switch goes back to --$stat_saved, so there's no need for revert to be true anymore right?

2) 'Invert charging state only twice': Why twice instead of just once?

I do not have any problems with these, I'm just genuinely curious.
[Magisk] underskyzx:
This is one of the best threads ever, shame how I was skeptical about it just for not having an app
[Magisk] underskyzx:
I'm getting a error message saying Blex files are corrupted, I'm on Lineage 15.1 using their SU (non-Magisk) tried to flash Blex multiple times in recovery but same error, any ideas?
[Magisk] jonta0:
My Moto E 2015 surnia stopped to charging after I installed and configure blex magisk module. I uninstalled blex, restart my phone and it's not charging anymore! Minutes ago after installing blex was charging normally. What to do to fix it?
[Magisk] minhduongtrung:
Hello, you tell me how to set the battery charge to 90% and power off at 15% battery level. My phone only charges 80% and power off at 20%. Tks!
[Magisk] kishd:
blex --enforce --update switch 90 70

blex --enforce power 15

I think these might help you.
[Magisk] kalawy:
Great module .. but avery 2 restarts i get that magisk doesn't recognize the modupe anymore.. the module files read 0 byte in the blex folder.. the binaries are good though..

I've noticed that the module has a busybox of it's own.. does in install it the the system??

I have busybox installed elsewhere by a magisk module

Although last night the module was working fine but the phone stopped at 89 % and it supposed to go to 90 .. and it stopped that way half the day which indicated it charged fully to 100 but the reads not accurate .. after half a day it started to go below 89%
[Magisk] kishd:
Check other modules that you have installed. In my case the cloudfare DNS module prevented blex from loading properly.
[Magisk] kalawy:
That and many other modules .. it even messed up my magisk image and deleted most modules randomly!

This mod has seroius comptability issues!!
[Magisk] psychoela:
Same issue here
[Magisk] donjoe0:
This is still clear as mud, just like the module's help text. What's "auto switch" supposed to mean?

How do I make the phone stop charging every time it reaches 90%, for example?

[MENTION=8570610]JayminSuthar[/MENTION]

Please update the --help text with clear descriptions of what the module can do to the phone (i.e. automatically stop charging, show a warning, automatically shut down) and with examples of commands that make it do that based on some reasonable threshold values. What's there now is incomprehensible.

Also, please remove the changelog from the --help text: on some terminal apps this will fill the buffer with information we don't need and push out the information at the top, which we do need. In my terminal if I scroll all the way up to see the beginning of the respose to blex --help I find only a part of the explanation of the parameters, the rest is cut out.

Thanks!
[Magisk] neekless:
What? If this is still unclear I don't know what else to tell you.

From the VERY FIRST POST (please read it):

To stop at 90%:

I don't know how to make this any clearer. I literally described what the commands do.
[Magisk] Dead-neM:
What you said is clear and you helped someone that didn't ask for it. But the other doesn't seems to want to learn...

---------- Post added at 05:29 PM ---------- Previous post was at 05:23 PM ----------

Wow really! What are you here for? Just watch, try and learn. This is a community developement thread and the dev is a student.

You are lost with some simple terminal command? Then Google it or buy/read any book about it

Dual boot your pc with any Linux distribution and you'll learn how to deal with a terminal too

All the other info are on the OP or the github readme...

Calm down boi. If it's unclear for you just step back, there's something you are missing to understand
[Magisk] Jaymin.0.1910:
I think using BusyBox' poweroff will not cause any expensive (or even cheaper) issues with the system, the Android system will take care of most stuff itself :)

1) revert=$write is done because we can't be sure whether charging state has changed in the first attempt, so we need to keep reverting until the switch() function denies to write and that's, when charging state has changed.

2) Just the same as above, one more attempt to make sure we succeed in what we're wishing for.

I personally do prefer an app, but you know, the college stuff.....

Update to latest version ;)

Changes made by blex do not survive a reboot, it must be something else and you need to find that out.

blex --enforce --update switch 90
blex --update power 15

You already fixed the corruption (thanks to [MENTION=4313092]kishd[/MENTION]).

The BusyBox is just used by blex, it's not installed into system.

blex uses reading from the kernel and not the Android system, sometimes the Java part of Android runs a little behind the kernel, please quote me back if I misunderstood you.

I have used threshold based on the command triggered in examples, so I think that should be fine, but if possible, kindly suggest me with what you're expecting :D

The next version will filter out the Changelog :).

Best regards to all.
[Magisk] donjoe0:
I see it now. It's in the part of the text I usually skip because it's mostly generic stuff that doesn't help me understand the specific options in the command I want to write. Instead, in this help text some of the most important explanations are in the generic introduction instead of being below them near the "--" options where they're directly relevant. If I want to write commands I scroll down straight to the options list. But in this case what I find there is just a bunch of talk about "auto switch" that doesn't make any sense.

Let me quote the main description of the app for you:

I read this, I expect the options list to also talk about "enable charging" and "disable charging", not "auto switch". (What switch? What's being switched?) This option name has nothing to do with the stated purpose of the app. It's just a bad name for the functionality, given the main description of what the app does. That's why the help text is hard to understand: it renames the main functionality of the app to something strange, and then provides the key to understanding the new name in paragraph 3.5.x.y.z of some generic descriptive text that's not even dealing with the command options yet. You skip that exact paragraph, you won't understand anything by just reading what each option does.
[Magisk] Jaymin.0.1910:
OK, I get it now :o, I will do recommended changes once my exams finish.

Best regards.