Ampere

Post your questions and help other users.

Moderator: Martin

akapelis
Posts: 41
Joined: 13 Jul 2016 20:51

Ampere

Post by akapelis » 03 Nov 2016 15:23

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.

User avatar
heilong
Posts: 77
Joined: 18 Oct 2016 15:20

Re: Ampere

Post by heilong » 04 Nov 2016 10:42

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.
Last edited by heilong on 04 Nov 2016 11:50, edited 1 time in total.

akapelis
Posts: 41
Joined: 13 Jul 2016 20:51

Re: Ampere

Post by akapelis » 04 Nov 2016 11:37

Thanks heilong, unfortunately my device is not rooted, i will try to study "battery manager" to figure it out.

User avatar
heilong
Posts: 77
Joined: 18 Oct 2016 15:20

Re: Ampere

Post by heilong » 04 Nov 2016 11:52

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.
Attachments
flow_BatteryGetCurrents_20161104_144849.xml
(3.79 KiB) Downloaded 845 times

User avatar
heilong
Posts: 77
Joined: 18 Oct 2016 15:20

Re: Ampere

Post by heilong » 04 Nov 2016 11:55

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.

akapelis
Posts: 41
Joined: 13 Jul 2016 20:51

Re: Ampere

Post by akapelis » 04 Nov 2016 13:06

Oh my god, it was so simple...there's already the variable in Automagic!
Attachments
Capture+_2016-11-04-15-03-44.png
Capture+_2016-11-04-15-03-44.png (198.33 KiB) Viewed 20299 times

ariloc
Posts: 109
Joined: 05 Jun 2016 21:36

Re: Ampere

Post by ariloc » 04 Nov 2016 16:01

That variable isn't available to me. Why is that happening?

akapelis
Posts: 41
Joined: 13 Jul 2016 20:51

Re: Ampere

Post by akapelis » 04 Nov 2016 16:49

ariloc wrote:That variable isn't available to me. Why is that happening?
Try this...
Attachments
Capture+_2016-11-04-18-37-45.png
Capture+_2016-11-04-18-37-45.png (213.51 KiB) Viewed 20292 times

User avatar
heilong
Posts: 77
Joined: 18 Oct 2016 15:20

Re: Ampere

Post by heilong » 04 Nov 2016 18:10

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.

User avatar
MURTUMA
Posts: 697
Joined: 05 Mar 2013 22:43

Re: Ampere

Post by MURTUMA » 04 Nov 2016 20:30

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.

Post Reply