如何更新HealthKit的步骤数?

时间:2015-10-20 02:44:02

标签: ios cordova health-kit monaca

使用MonacaHealthKit plug-in获得了步数 在打开HealthCare应用程序之前,不会更新获取的步骤数 无法通过从插件发送命令来更新?
请帮帮我 谢谢你的建议。

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);
};

1 个答案:

答案 0 :(得分:3)

我能理解!
它的工作原理如下。

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
);