Appy Builder: Using the BatteryStatus component #AppInventor #AppyBuilder #STEM #Programming

A reader asked if there is a way to determine the status of the Android device battery in MIT App Inventor. Unfortunately, App Inventor does not provide a way to check the battery status … but Appy Builder does!

To answer that question, our first Appy Builder app is a simple tutorial showing how to use the BatteryStatus component in Appy Builder.

User Interface

The output is displayed after pressing the Start button and shows that my phone’s battery is at 99% and was plugged in to the AC power charger.

In this screen shot, the phone was unplugged from the battery charger and the “plugged in” status has changed to UNKNOWN.

Designer View

I created this screen by adding a Button control, and then adding six HorizontalLayouts. Inside each layout, I dragged two Label controls, setting one to the descriptive text (such as “Battery level:” and the second to be used to display the status for that property).

Blocks View

The BatteryManager has several properties that describe aspects of the battery.

Before the properties may be used you must activate the BatteryManager by setting the .StartMonitoring property to true; this must be done separately as shown below. Errors occurred when I put that code inside the button’s .Click handler. Instead, I moved the StartMonitoring assignment to the Screen1.Initialize event handler and everything worked fine.

  • BatteryLevel – the battery’s current level in percent
  • BatteryTemperature – the temperature of the battery in degrees Celsius
  • BatteryPresent – true (my phone does not permit me to remove the battery)
  • BatteryPlugged – returns “AC” if the device is connected to a charger, or UNKNOWN if the device is not plugged in. This might also return “WIRELESS” if the device is currently using a wireless charger (based on Android BatteryManager API).
  • BatteryVoltage – battery voltage. A typically value will be near 4 volts plus or minus for most phones.
  • BatteryHealth – GOOD. I presume this returns a value corresponding to the Android BatteryManager API, which includes COLD, DEAD, GOOD, OVERHEAT, OVER_VOLTAGE, UNKNOWN and UNSPECIFIED_FAILURE.

Download Source

Right-click (or Ctrl-click on Mac OS X) and use your browser’s feature to save this file to your computer: BatteryStatus.aia

Then, use Projects | Import selected project (.aia) from my computer … to load the file into Appy Builder

Measuring Android Power Consumption

I did my software engineering Masters thesis (2012) on Android power management and I learned, as part of my research, that most app developers have no idea how much power their app demands from their Android device’s battery.  Some who thought they were looking at power were confusing other metrics with power. At the time, tools for measuring power were not easy to obtain (costly, and hard to measure power inside a phone).

Now there is an app that provides many metrics about what apps are doing on Android, including power.

Go to the Google Play store, search for Trepn Profiler and install that app (from Qualcomm, who makes the Snapdragon processor used in most phones).

For a quick look at battery power in a strip chart format, select Network Activity and see a chart like this:

Screenshot_20151102-160041

Then select CPU Usage Monitor and identify what percent of CPU time is being used by each app. Initially the display will show “No applications are running” – wait a moment and gradually, apps will begin to show up in the list, with the % of CPU core they are using, shown at right. This may help you spot apps that are using an unexpected amount of power.

If possible, delete apps that you never or rarely use. Many lurk in the background and periodically run a fraction of a second, infrequently. While each individual app has little impact, the total of all apps may start to have an impact that you notice.

For more information about Trepn and estimating power consumption, read the linked article, below.

Trepn Profiler

Source: How to Measure Power Consumption Using Free Software | Mostly-Tech