Page 1 of 3

Ampere

Posted: 03 Nov 2016 15:23
by akapelis
Hi friends, there's an app on the store called "ampere" that measures milliamperes of the battery during charging or discharging. Is there a way to do this with Automagic?
Thank you very much.

Re: Ampere

Posted: 04 Nov 2016 10:42
by heilong
If you have root access, you could:

Code: Select all

cat /sys/class/power_supply/battery/current_avg

or

Code: Select all

cat /sys/class/power_supply/battery/current_now
I suspect the unit is microamps.

Battery info in Android can be obtained from BatteryManager.

Re: Ampere

Posted: 04 Nov 2016 11:37
by akapelis
Thanks heilong, unfortunately my device is not rooted, i will try to study "battery manager" to figure it out.

Re: Ampere

Posted: 04 Nov 2016 11:52
by heilong
Most of the info, but not the charge currents, are easily obtained by creating a flow triggered by General Broadcast (android.intent.action.BATTERY_CHANGED), check "Receive initial sticky broadcast" - this allows you to run the flow manually any time and get the last broadcasted intent's value. You could either call this flow from another flow and get the results back, or you could set this flow up to store battery info properties in global variables. E.g. get intent extras

Code: Select all

global_battery_level = getInt('level', -1);
- that works. But there's no extras for current.

Current info has been added to BatteryManager since API Level 21 (Android 5.0), to get them you need to get the BatteryManager system service. I've been able to get that info using Automagic's experimental functions for calling native Java code. An example flow is attached. If you're just using it for yourself, it should work as is. But ideally the script should first check if the API Level is >= 21.

Re: Ampere

Posted: 04 Nov 2016 11:55
by heilong
Rooted way is less reliable, too, because on different devices the /sys paths to the battery info are different.
On the other hand, Automagic's Java methods have a disclaimer that they are experimental and the API may change. I'm pretty sure if they're used in a wrong way, Automagic might crash.

Re: Ampere

Posted: 04 Nov 2016 13:06
by akapelis
Oh my god, it was so simple...there's already the variable in Automagic!

Re: Ampere

Posted: 04 Nov 2016 16:01
by ariloc
That variable isn't available to me. Why is that happening?

Re: Ampere

Posted: 04 Nov 2016 16:49
by akapelis
ariloc wrote:That variable isn't available to me. Why is that happening?
Try this...

Re: Ampere

Posted: 04 Nov 2016 18:10
by heilong
Oh yeah... Looks like the battery level condition provides all possible battery info.
So all my digging with callJavaMethod stuff was useless :(
Slight correction to your flow - your condition won't work if the battery level is exactly 100% (just unplugged). You should add the "false" branch (going same way as your "true" branch). You can also use {battery_current_now/1000} directly, no need for a script.

Re: Ampere

Posted: 04 Nov 2016 20:30
by MURTUMA
heilong wrote:You should add the "false" branch (going same way as your "true" branch).
Even better would be to config it to higher than 0%.

This same procedure is highly usable in many different cases with different conditions.