Android Question Reading mA current drain

Peter Simpson

Expert
Licensed User
Longtime User
Hello all,
It's been a long time since I've actually asked a question on here as lots of questions have already been answered or it's pretty easy to figure things out by yourself in B4a.

Question:
Anyway, what's the best way to read the average battery current drain on a device?

I already know that you can use the 'Phone Library' to read the current battery level, but I need more than that, I'm after a way to actually read the device milliamps(mA) drain. I've noticed that their are plenty of apps on the 'Play Store' that gives you the current device discharge mA reading, that's exactly what I'm after. Even now I'm typing this on my phone and I can see at the top of the screen that my device is currently using -270mA (- is discharge).

UPDATE: I do believe that it's got something to do with the android.os.BatteryManager though, I'll look more into it before going out for my yearly new year drink.

Thanks in advance for your responses and happy new year to everyone who uses this great community
 
Last edited:

Peter Simpson

Expert
Licensed User
Longtime User
Thank you @Erel but I had already looked at that, I should stated that in my original post, sorry.

The problem is that I need to access BATTERY_PROPERTY_CURRENT_AVERAGE and BATTERY_PROPERTY_CURRENT_NOW every 2 seconds or so which you can't get via
Intent.ExtrasToString, Intent.ExtrasToString is great for getting charging data though :)

I tried accessing them via JavaObject but for some reason I had no joy :mad:

I'm using API 25
 
Last edited:
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
I don't think these properties can be read in the BATTERY_CHANGED event. Only the ones starting with EXTRA_...

These properties are not available on all devices, look HERE. The ones which are not implemented return 0 or Interger.MIN, depending on wether you ask for getIntProperty or getLogProperty

For instance, Nexus 5 devices only has PROPERTY_CAPACITY available, and the next ones (NEXUS 6,7..) all of them. However, these figures depend on the circuit charger model, and in many models it is either not available or not updated into these properties

I have tested it in 2 devices: HTC ONe M7 and Xiaomi Redmi Note 4. Only in the first one I could get the battery capacity. The rest of properties are not implemented by the vendor.

B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim J As JavaObject
   Dim Timer1 As Timer
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim lb1 As Label
   Dim lb2 As Label
   Dim lb3 As Label
   Dim lb4 As Label
  
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")
   J.InitializeContext
  
   J=J.InitializeNewInstance("android.os.BatteryManager",Null)
  
   lb1.Initialize("")
   Activity.AddView(lb1,20dip, 40dip,100%X-20dip,40dip)
   lb2.Initialize("")
   Activity.AddView(lb2,20dip, 80dip,100%X-20dip,40dip)
   lb3.Initialize("")
   Activity.AddView(lb3,20dip, 120dip,100%X-20dip,40dip)
   lb4.Initialize("")
   Activity.AddView(lb4,20dip, 160dip,100%X-20dip,40dip)

   Timer1.Initialize("Timer1",1000)

End Sub

Sub Activity_Resume
   Timer1.Enabled=True
End Sub

Sub Timer1_Tick

   Dim property As Int
   '2: INSTANT
   property=2
   Dim instantCurrentConsumptionuA As Int=J.RunMethod("getIntProperty",Array(property))    ' 2=BATTERY_PROPERTY_CURRENT_NOW in microAmperes
   Log("Consumption is: "&instantCurrentConsumptionuA)
   '3: AVERAGE
   property=3
   Dim averageCurrentConsumptionuA As Int =J.RunMethod("getIntProperty",Array(property))  
   Log("Average Consumption is: "&averageCurrentConsumptionuA)
   '4: CAPACITY
   property=4
   Dim batteryCapacity As Int =J.RunMethod("getIntProperty",Array(property))   
   Log("Capacity: "&batteryCapacity)
   '5: COUNTER (long)
   property=5
   Dim batteryEnergyCounternWh As Long =J.RunMethod("getLongProperty",Array(property)) 
   Log("Energy counter: "&batteryEnergyCounternWh)
  
   lb1.text = "Instant: "&instantCurrentConsumptionuA
   lb2.text = "Average: "&averageCurrentConsumptionuA
   lb3.text = "Capacity: "&batteryCapacity
   lb4.text = "Energy Counter: "&batteryEnergyCounternWh

End Sub

Sub Activity_Pause (UserClosed As Boolean)
   Timer1.Enabled=False
End Sub
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Hey @JordiCP that's exactly what I was looking do do with JavaObject. I've just got home and started looking into this again when I thought I would have a look at my post again, so thank you that's perfect, I see where I went wrong with JO now, dam fool :mad:.

Anyway my Nexus 5, 6P and 7 can't manipulate BATTERY_PROPERTY_CURRENT_AVERAGE, but my S5 can.

Cheers :cool:
 
Upvote 0
Top