Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Sample for current Battery Charge

ANSWERED

Hi Guys,

 

I'm working on my 1st watchface, so far it look great! Is there an example of getting the current battery charge percentage?

 

Thanks 

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

The Power API is coming in the next beta update, and it will be:

 

import { charger, battery } from "power";
console.log(Math.floor(battery.chargeLevel) + "%");
console.log("The charger " + (charger.connected ? "is" : "is not") + " connected");

View best answer in original post

Best Answer
7 REPLIES 7

The Power API is coming in the next beta update, and it will be:

 

import { charger, battery } from "power";
console.log(Math.floor(battery.chargeLevel) + "%");
console.log("The charger " + (charger.connected ? "is" : "is not") + " connected");
Best Answer

Oh that makes sense. I was under the impression that it was already implemented. May I ask approximately when the update regarding the Power API will be released? 

Best Answer

@JonFitbit Thanks

Best Answer
0 Votes

Tried this on my ionic v27.32.4.19 but get NaN as chargeLevel.

Got any hints?

Best Answer
0 Votes

Can you provide the relevant code so we can look at it?

Best Answer
0 Votes

Found the error. My variable was named battery.Smiley Embarassed

Best Answer

Mine is Ionic

I have recently started using the Fitbit Studio to create my own personal watch face, it was good fun and have learnt a lot.

Regarding the battery indicator using either a progress bar or simply displaying the charge level in text format or a radial ring or whatever you may think of, the usual answer one would get from the Fitbit Developer page regarding the Power API (https://dev.fitbit.com/build/reference/device-api/power/) or from someone who pasted fragment of their own code, there is one important point I have figured out and not mentioned by many "solved solution".

This leads to a problem many have encountered, that is the battery level seem not able to update itself in the watch face, but when swipe up to see the status check or to an app then back to the watch face, the battery level is updated.

The important point I figured out is where you put the following code in the "index.js", assuming to use a variable named "battLevel" (the the following code is to insert the actual battery level):

 

  battLevel = battery.chargeLevel;

 

Now, the question is, at what location this "battery.chargeLevel" should be put at.

I have seen codes putting this line as part of the initial lines of "index.js" way before any call for any functions:

let ......

let ......

let batLevel = battery.chargeLevel;

 

If this is done so, the reading of the battery level is fed into the "battLevel" when the page "index.js" is called (at start up of the watch, swipe back from other pages etc.), and that's it, the reading will not change no matter how many times you look at the watch face, because if not going to other pages (including the swipe up) then back, I found the only parameters that keep updating the watch face are either:

 

clock.ontick = function(evt) {}

or

setInterval(function, 1000);

 

Then I realized the idea place to put "battery.chargeLevel" should be within the clock.ontick = function(evt) {}, also wrap it inside a condition, only perform the battery level update when the screen is put on:

 

function updateBattery() {

  ...

  ...

  myBattlvl = battery.chargeLevel;

  ...

  ...

  if (display.on) {
    update_battery_function();
  }

}

 

That's what I do and so whenever I turn my wrist, the watch face shows up, the above code activates the battery level check and my battery indicator shows the actual battery level.

P_20181105_095038.jpg

Best Answer