Support batteryless devices

This page describes how Android handles products that have either removable batteries or no internal batteries. The latter devices are instead connected to an external power source, such as an AC power outlet or USB port on another device.

Is a battery present?

The following code may be used by applications to detect whether the device has a battery present:

```
final Intent batteryInfo = registerReceiver(null, new
IntentFilter(Intent.ACTION_BATTERY_CHANGED));

return batteryInfo.getBooleanExtra(BatteryManager.EXTRA_PRESENT, true);
```

Batteryless device behavior

If Android does not detect a battery device for your product, then the following battery-related default values are used. Note the defaults have changed in the Android 9 release. This table shows the differences.

Battery state Android 9 and higher Android 8.1 and lower
Present false true
Status unknown charging
Remaining capacity 0 100%
Health unknown good
AC charger online status not modified forced to true

Manufacturers may alter the default settings using a kernel power_supply driver or Health HAL.

Android 9 and higher

Android 9 removes some previous code for batteryless devices that by default pretended a battery was present, was being charged at 100%, and was in good health with a normal temperature reading on its thermistor.

Most framework APIs that deal with this information continue to handle common situations the same as previously: the system is considered to be charging (that is, not running on battery power), and won't be considered to have a low battery. If the user interface draws the battery icon, it will appear with an exclamation point, and battery percentage is shown as 0%. But the device won't shut down due to low battery, and jobs that require charging or good battery are scheduled.

Android 8.1 and lower

Because the battery status is unknown, the Android framework APIs will consider the system to be charging (or, not running on battery power) and won't be considered to have a low battery. If the user interface renders the battery icon, it will appear with an exclamation point, and battery percentage is shown as 0%. But the device won't shut down due to low battery, and jobs that require charging or good battery are scheduled.

Implementation

The Android 9 default code may work properly for your device, but it's recommended to make either a kernel or a HAL change to accurately reflect the power and battery state for your product, as described above. If Android 9 and higher does not detect a Linux power supply class charger device, then by default all charger types (AC, USB, Wireless) will have status offline. If all chargers are offline but there is no battery device detected, the system will still be considered to be charging in the sense that it is running on external, not battery power, as described previously.

If your product does not have a battery and is always connected to a power source, it's best to implement a Linux kernel power_supply class charger driver for the AC or USB power source that sets its online sysfs attribute to true. Or you can configure the AC charger online property in a Health HAL for your device. To do this implement a Health HAL as described in Implementing Health 2.0.

This custom Health HAL implements a custom version of Health::getHealthInfo() that modifies the value of BatteryProperties.chargerAcOnline = true.

To get started, copy file hardware/interfaces/health/2.0/default/Health.cpp to your own Health HAL implementation and modify it according to the Health 2.0 README.