CIQ 6.2.1 getBodyBatteryHistory unexpected type error

Since I updated to CIQ 6.2.1 I am getting unexpected type errors from getBodyBatteryHistory.  Here is my code.

Before I call the function valBodyB I am making sure SensorHistory is present and getBodyBatteryHistory is present check as well.

if((Toybox has :SensorHistory)!=true||(Toybox.SensorHistory has :getBodyBatteryHistory)!=true)....then don't allow the call to valBodyB.

Once the watch is confirmed to have Body Battery then we can call the function below.

    hidden function valBodyB() {
        var bodyBat=sh.getBodyBatteryHistory({});
        if (bodyBat!=null) {
            var bodyb=bodyBat.next().data;
            return (bodyb==null)?"":bodyb.format("%d");
        }
        return "";
    }
Any help would be appreciated.
Thanks...
  • when you do "var bodyb...", you first want to null check it, before you try to get data

    so 

    var bodyb1=BodyBar.next();

    if(bodybat1!=null) {return bodybat1.data.format("%d");}

    else {return whatever;}

  • Thank you for the quick response.  I think I have incorporated what you are saying and added in a range check and a period in the get...history.  How does this look?

        hidden function valBodyB() {
            var bodyBat=sh.getBodyBatteryHistory({:period=>1});
            if (bodyBat!=null) {
                var bodyb=bodyBat.next();
                if (bodyb!=null) {          
                    var bb=bodyb.data;
                    return (bb!=null && bb>=0 && bb<=100)?bb.format("%d"):"";
                } else {
                    return "";
                }
            }
            return "";
        }
  • Looks ok to me.  Have you tried it?

  • Yes, in the simulator but not on a watch yet.  Still working on refining the code.  I think this is the final version that is running in the simulator.

        hidden function valBodyB() {
            var bodyBat=sh.getBodyBatteryHistory({:period=>1});
            var bodyb=(bodyBat!=null)?bodyBat.next():null;
            var bb=(bodyb!=null)?bodyb.data:null;
            return (bb!=null&&bb>=0&&bb<=100)?bb.format("%d"):"";
        }
  • What are your recommendations on these two functions.  One reuses the same variable....

    hidden function valBodyB() {
            var bodyBat=sh.getBodyBatteryHistory({:period=>1});
            var bodyb=(bodyBat!=null)?bodyBat.next():null;
            var bb=(bodyb!=null)?bodyb.data:null;
            return (bb!=null&&bb>=0&&bb<=100)?bb.format("%d"):"";
    vs
    hidden function valBodyB() {
            var bodyBat=sh.getBodyBatteryHistory({:period=>1});
            bodyBat=(bodyBat!=null)?bodyBat.next():null;
            bodyBat=(bodyBat!=null)?bodyBat.data:null;
            return (bodyBat!=null&&bodyBat>=0&&bb<=100)?bodyBat.format("%d"):"";