Search code examples
cordovaionic2battery

displaying battery status in ionic 2 view


I am trying to display battery status in my ionic view. I got display network connection solution. but could not get to display battery status, I have installed ionic 2 battery status plugin

HTML

<h2>Battery status: {{status?.level}}</h2>
<h2>Battery is plugged:{{status?.isPlugged}}</h2>

TypeScript

status:any;
constructor(public alert:AlertController, public platform: Platform) { 
  this.platform.ready().then(()=>{
    let subscription = BatteryStatus.onChange().subscribe( (status) => { 
      console.log(status.level, status.isPlugged); 
      this.status=status.level;
    });
  });
} 

You can clone from the below bitbucket git

$ git clone https://bitbucket.org/maniselvam/ionic2battery.git 

Solution

  • Import the Battery Status on your TS file.

    import { BatteryStatus } from 'ionic-native';
    

    Declare a variable for battery status

    Level:any = 0;
    

    Initialise the plugin

    let subscription = BatteryStatus.onChange().subscribe(
      (status) => {
        this.Level = status.level;
        console.log(status.level, status.isPlugged);
      }
    );
    

    HTML

    <h1>Battery Level: {{Level}}</h1>