Ionic 4 | How to Check Battery Percentage Using Cordova and Ionic 4.X Native Plugin

In Ionic Application, we can check battery Status of the mobile device using Cordova and Ionic Native plugins. By using these plugin we can call methods to get battery percentage and we can also check if the device is plugged to a power source.

We can have many states to show user or check some tasks according to the current battery status of the device.

Let’s start… we will cover these two things:

How to Check Battery Status in Ionic 4.X Applications?

How to Check if Device is Plugged in Power Source or in Charging State?

Step 1) Create an Ionic 4 Application using CLI

Make sure you have the latest version of Ionic CLI installed.

Create a new application by running following command in CMD

 

Step 2) Install Cordova and Ionic Native Plugins

Step 3) Import Plugins in App Module FILE_URI

In app.module.ts, import plugins and add in providers array

 

Step 4) Call Method in Home Component

Now we will import plugin in home.page.ts file and call method to get battery status and a boolean state to check if the device is plugged in power source.

There are three methods provided by the plugin

onChange: Watch the change in battery level
onLow: Watch when the battery level goes low
onCritical: Watch when the battery level goes to critical

These methods can be subscribed and unsubscribed as per application requirements

Leave a Reply

avatar
  Subscribe  
Notify of