2015-10-20 125 views
1

使用MonacaHealthKit plug-in已獲取步驟數。
直到打開HealthCare應用程序纔會更新獲取的步驟數。
無法通過從插件發送命令進行更新?
請幫幫我。
謝謝你的建議。如何更新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); 
}; 
+0

相關鏈接:http://ja.stackoverflow.com/questions/17709/ – Myaku

回答

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