When a user downloads an application for their device, they are more conscious of the battery usage of the application. So as a mobile application developer, you should consider the battery usage of your application.
If you are developing a web application for a mobile device then your choice is to use HTML5’s Battery Status API if you are concerned about the user’s device battery status/charging levels. Yes, HTML5 provides an API for a device's battery.
W3.org says: “The Battery Status API specification defines a means for web developers to programmatically determine the battery status of the hosting device”.
Consider that you are developing a web application that has a huge number of transactions in a page. If you start the transaction without knowing the battery charge level then if the battery level is low, the transaction may fail if the battery is dead.
So in this scenario, the HTML’s Battery Status API can help mobile web application developers.
Check for Battery Status API
You can check whether the battery status API is supported by the browser or not as shown below.
- var battery = navigator.battery || navigator.webkitBattery || navigator.mozBattery || navigator.msBattery;
- if (battery) {
-
- }
The battery status API is currently supported by the latest version of Chrome and Firefox.
Properties
There are four basic properties available in the battery status API.
In the preceding example, we handle all the events and update the text based on the battery property values.
For a live example, please access the
jsfiddle in the mobile.
Source:
W3.org