如何更新是HealthKit的步数



使用Monaca和HealthKit插件已获得的步数。
在打开医疗保健应用程序之前,不会更新获取的步数。
将无法通过从插件发送命令更新?
请帮帮我。
谢谢你的建议。

var hk = null;
var isHK = false;
document.addEventListener("deviceready", function(){hk_init();}, false);
/**
 * HealthKit Initialized and Support Check.
 **/
function hk_init(){
    //plugin is not found.
    if(typeof window.plugins.healthkit === 'undefined' || window.plugins.healthkit == null){return false;}
    if(typeof hk === 'undefined' || hk == null){hk=window.plugins.healthkit;}
    hk.available(
        function(res){ if(res){ isHK=true; console.log("healthkit ready."); } },
        function(e){ console.log(e); }
    );
    return true;
}
/**
 * request the authority to HealthKit
 **/
function requestHealthKit(callback){
    window.plugins.healthkit.requestAuthorization(
        {'readTypes'  : ['HKQuantityTypeIdentifierStepCount']},
        callback,
        hk_error
    );
}
/**
 * Re-initialization
 **/
function hk_noinit(){
    hk_init();
    alert("Please try again later.");
}
/**
 * Get the steps
 * @param start     Date        Measurement start
 * @param end       Date        Measurement end
 * @param callback  function    Function that receives the return value
 **/
function call_stepcount(start,end,callback){
    if(!isHK){ hk_noinit(); }
    if(typeof start === 'undefined'){ return false; }
    if(typeof end === 'undefined'){ end=new Date(); }
    if(typeof callback === 'undefined'){ callback=function(res){console.log(res);}; }
    window.plugins.healthkit.querySampleType(
        {
            'startDate' : start,
            'endDate'   : end,
            'sampleType': 'HKQuantityTypeIdentifierStepCount',
            'unit'      : 'count'
        },
        callback,
        hk_error
    );
}
/**
 * accumulated the number of steps
 * @param res Array Array of HealthKit
 * @return Integer total number
 **/
function analyze_step(res){
    var steps = 0;
    try{
        for(var n=0,len=res.length;n<len;n++){
            steps += res[n]["quantity"];
        }
        return steps;
    }catch(e){
        return 0;
    }
}
/**
 * Get today's total number
 * @param callback function
 **/
function today_steps(callback){
    var now=new Date();
    var start = new Date(now.getFullYear()+"/"+(now.getMonth()+1)+"/"+now.getDate());
    call_stepcount(start,now,function(res){callback(analyze_step(res));});
}
/**
 * error callback
 * @param result Object
 **/
function hk_error(result) {
    console.log("Error: n" + JSON.stringify(result));
    requestHealthKit(function(ev){console.log(ev);});
    alert("failed to access the HealthKit.");
};
/**
 * Debug func
 **/
function debug_hk(){
    today_steps(hk_success);
}
/**
 * For debug, the result output
 **/
function hk_success(result) {
    console.log("success : " + result);
};

我能理解!

window.plugins.healthkit.monitorSampleType(
    {'sampleType': 'HKQuantityTypeIdentifierStepCount'},
    function(res){
        window.plugins.healthkit.querySampleType(
            {
                'startDate' : start,
                'endDate'   : end,
                'sampleType': 'HKQuantityTypeIdentifierStepCount',
                'unit'      : 'count'
            },
            callback,
            hk_error
        );
    },
    hk_error
);

最新更新