UNPKG

fitatu.telerik.plugins.healthkit

Version:

Interact with the iOS HealthKit SDK.

396 lines (361 loc) 12.6 kB
<!DOCTYPE html> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <html> <head> <meta charset="utf-8"/> <meta name="format-detection" content="telephone=no"/> <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 --> <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi"/> <link rel="stylesheet" type="text/css" href="css/index.css"/> <meta name="msapplication-tap-highlight" content="no"/> <title>Hello World</title> </head> <body> <div class="app"> <h1>Apache Cordova</h1> <div id="deviceready" class="blink"> <p class="event listening">Connecting to Device</p> <p class="event received">Device is Ready</p><br/> <button onclick="available()">Health available?</button> &nbsp;&nbsp; <button onclick="requestAuthorization()">Request auth</button> <br/><br/> <button onclick="checkAuthStatus()">Check auth status</button> <br/><br/> <button onclick="readDateOfBirth()">Read birthdate</button> &nbsp;&nbsp; <button onclick="readGender()">Read gender</button> <br/><br/> <button onclick="readBloodType()">Read bloodtype</button> &nbsp;&nbsp; <button onclick="readFitzpatrickSkinType()">Fitzp. Skin type</button> <br/><br/> <button onclick="querySampleType()">Query Sample</button> <br/><br/> <button onclick="querySampleTypeAggregated()">Query Sample Aggregated</button> <br/><br/> <button onclick="saveHeight()">Save Height</button> &nbsp;&nbsp; <button onclick="readHeight()">Read Height</button> <br/><br/> <button onclick="saveWeight()">Save Weight</button> &nbsp;&nbsp; <button onclick="readWeight()">Read Weight</button> <br/><br/> <button onclick="saveWorkout()">Save Workout</button> &nbsp;&nbsp; <button onclick="findWorkouts()">Find Workouts</button> <br/><br/> <button onclick="monitorSampleType()">Monitor sample type</button> <br/><br/> <button onclick="sumQuantityType()">Sum quantity type</button> <br/><br/> <button onclick="saveQuantitySample_StepCount()">Save quantity sample (step count)</button> <br/><br/> <button onclick="saveQuantitySample_Energy()">Save quantity sample (energy)</button> <br/><br/> <button onclick="saveCorrelation()">Save correlation</button> <br/><br/> <button onclick="queryCorrelationTypeFood()">Query correlation food</button> <br/><br/> <button onclick="queryCorrelationTypeBloodPressure()">Query correlation blood pressure</button> <br/><br/> </div> </div> <script type="text/javascript" src="cordova.js"></script> <script type="text/javascript" src="js/index.js"></script> <script type="text/javascript"> window.onerror = function (a, b, c) { alert(a); alert(c); }; app.initialize(); var callback = function (msg) { // wrapping in a timeout because of a possbile native UI element blocking the webview setTimeout(function () { alert(JSON.stringify(msg)) }, 0); }; var yesterday = new Date(); yesterday.setDate(yesterday.getDate() - 1); var tomorrow = new Date(); tomorrow.setDate(tomorrow.getDate() + 1); function available() { window.plugins.healthkit.available( callback, callback ); } function requestAuthorization() { var supportedTypes = [ 'HKQuantityTypeIdentifierHeight', 'HKQuantityTypeIdentifierStepCount', 'HKQuantityTypeIdentifierDistanceWalkingRunning', 'HKCategoryTypeIdentifierSleepAnalysis', 'HKQuantityTypeIdentifierDietaryEnergyConsumed', 'HKQuantityTypeIdentifierDietaryFatTotal' ]; window.plugins.healthkit.requestAuthorization( { readTypes: supportedTypes, writeTypes: supportedTypes }, callback, callback ); } function checkAuthStatus() { window.plugins.healthkit.checkAuthStatus( { 'type': 'HKQuantityTypeIdentifierHeight' }, callback, callback ); } function readDateOfBirth() { window.plugins.healthkit.readDateOfBirth( callback, callback ); } function readGender() { window.plugins.healthkit.readGender( callback, callback ); } function readBloodType() { window.plugins.healthkit.readBloodType( callback, callback ); } function readFitzpatrickSkinType() { window.plugins.healthkit.readFitzpatrickSkinType( callback, callback ); } function saveWeight() { window.plugins.healthkit.saveWeight({ 'requestReadPermission': false, // use if your app doesn't need to read 'unit': 'kg', 'amount': 78.5, 'date': new Date() // is 'now', which is the default as well }, callback, callback ); } function readWeight() { window.plugins.healthkit.readWeight( { 'requestWritePermission': true, // use if your app doesn't need to write 'unit': 'kg' }, callback, callback ); } function saveHeight() { window.plugins.healthkit.saveHeight({ 'requestReadPermission': false, 'unit': 'cm', // m|cm|mm|in|ft 'amount': 187 }, callback, callback ); } function readHeight() { window.plugins.healthkit.readHeight({ 'requestWritePermission': false, 'unit': 'cm' // m|cm|mm|in|ft }, callback, callback ); } function findWorkouts() { window.plugins.healthkit.findWorkouts({}, callback, callback ); } function saveWorkout() { window.plugins.healthkit.saveWorkout({ //'requestReadPermission' : false, 'activityType': 'HKWorkoutActivityTypeCycling', // HKWorkoutActivityType constant (https://developer.apple.com/library/ios/documentation/HealthKit/Reference/HKWorkout_Class/#//apple_ref/c/tdef/HKWorkoutActivityType) 'quantityType': 'HKQuantityTypeIdentifierDistanceCycling', 'startDate': new Date(), // mandatory 'endDate': null, // optional, use either this or duration 'duration': 3600, // in seconds, optional, use either this or endDate 'energy': 300, // 'energyUnit': 'kcal', // J|cal|kcal 'distance': 11, // optional 'distanceUnit': 'km' // probably useful with the former param // 'extraData': "", // Not sure how necessary this is }, callback, callback ); } function querySampleType() { window.plugins.healthkit.querySampleType( { 'startDate': new Date(new Date().getTime() - 3 * 24 * 60 * 60 * 1000), // three days ago 'endDate': new Date(), // now 'sampleType': 'HKQuantityTypeIdentifierBasalBodyTemperature', //'sampleType': 'HKQuantityTypeIdentifierStepCount', // anything in HealthKit/HKTypeIdentifiers.h 'unit': 'degF' // make sure this is compatible with the sampleType }, callback, callback ); } function querySampleTypeAggregated() { window.plugins.healthkit.querySampleTypeAggregated( { 'startDate': new Date(new Date().getTime() - 3 * 24 * 60 * 60 * 1000), // three days ago 'endDate': new Date(), // now 'aggregation': 'week', // 'hour', 'week', 'year' or 'day', default 'day' 'sampleType': 'HKQuantityTypeIdentifierStepCount', // any HKQuantityType 'unit': 'count' // make sure this is compatible with the sampleType }, callback, callback ); } // this is work in progress function monitorSampleType() { window.plugins.healthkit.monitorSampleType( { 'sampleType': 'HKCategoryTypeIdentifierSleepAnalysis' }, function (value) { // this gets called when a new sample is available (can then be fetched by a different function) console.log("Sleep (webview): " + value); }, callback ); } function sumQuantityType() { window.plugins.healthkit.sumQuantityType( { 'startDate': new Date(new Date().getTime() - 3 * 24 * 60 * 60 * 1000), // three days ago 'endDate': new Date(), // now 'sampleType': 'HKQuantityTypeIdentifierStepCount' }, function (value) { alert("Success for running step query " + value); }, callback ); } function saveQuantitySample_StepCount() { window.plugins.healthkit.saveQuantitySample( { 'startDate': new Date(new Date().getTime() - 24 * 60 * 60 * 1000), // a day ago 'endDate': new Date(), // now 'sampleType': 'HKQuantityTypeIdentifierStepCount', // make sure you request write access beforehand 'unit': 'count', 'amount': 300 }, function (value) { alert("Success running saveQuantitySample, result: " + value); }, callback ); } function saveQuantitySample_Energy() { window.plugins.healthkit.saveQuantitySample( { 'startDate': new Date(), // now 'endDate': new Date(), // now 'sampleType': 'HKQuantityTypeIdentifierDietaryEnergyConsumed', // make sure you request write access beforehand 'unit': 'kcal', 'amount': 64 }, function (value) { alert("Success running saveQuantitySample, result: " + value); }, callback ); } function saveCorrelation() { window.plugins.healthkit.saveCorrelation( { 'startDate': new Date(), // now 'endDate': new Date(), // now 'metadata': {'a': 'b'}, 'correlationType': 'HKCorrelationTypeIdentifierFood', // don't request write permission for this 'samples': [ { 'startDate': new Date(), 'endDate': new Date(), 'sampleType': 'HKQuantityTypeIdentifierDietaryEnergyConsumed', // make sure you request write access beforehand 'unit': 'kcal', 'amount': 500 }, { 'startDate': new Date(), 'endDate': new Date(), 'sampleType': 'HKQuantityTypeIdentifierDietaryFatTotal', // make sure you request write access beforehand 'unit': 'g', 'amount': 25 } ] }, function (value) { alert("Success running saveCorrelation, result: " + value); }, callback ); } function queryCorrelationTypeFood() { window.plugins.healthkit.queryCorrelationType( { 'startDate': new Date(new Date().getTime() - 24 * 60 * 60 * 1000), // a day ago 'endDate': new Date(), // now 'correlationType': 'HKCorrelationTypeIdentifierFood', // don't request read permission for this 'unit': 'g' }, function (value) { alert("Success running queryCorrelationType, result: " + JSON.stringify(value)); }, callback ); } function queryCorrelationTypeBloodPressure() { window.plugins.healthkit.queryCorrelationType( { 'startDate': new Date(new Date().getTime() - 24 * 60 * 60 * 1000), // a day ago 'endDate': new Date(), // now 'correlationType': 'HKCorrelationTypeIdentifierBloodPressure', // don't request read permission for this 'unit': 'Pa' }, function (value) { alert("Success running queryCorrelationType, result: " + JSON.stringify(value)); }, callback ); } </script> </body> </html>