This repository has been archived by the owner on Nov 21, 2023. It is now read-only.
forked from emmavray/HealthKit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
361 lines (330 loc) · 11.5 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<!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>
<button onclick="requestAuthorization()">Request auth</button>
<br/><br/>
<button onclick="checkAuthStatus()">Check auth status</button>
<br/><br/>
<button onclick="readDateOfBirth()">Read birthdate</button>
<button onclick="readGender()">Read gender</button>
<br/><br/>
<button onclick="readBloodType()">Read bloodtype</button>
<br/><br/>
<button onclick="querySampleType()">Query Sample</button>
<br/><br/>
<button onclick="saveHeight()">Save Height</button>
<button onclick="readHeight()">Read Height</button>
<br/><br/>
<button onclick="saveWeight()">Save Weight</button>
<button onclick="readWeight()">Read Weight</button>
<br/><br/>
<button onclick="saveWorkout()">Save Workout</button>
<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() {
window.plugins.healthkit.requestAuthorization(
{
'readTypes': ['HKCharacteristicTypeIdentifierDateOfBirth',
'HKCategoryTypeIdentifierSleepAnalysis', 'HKQuantityTypeIdentifierActiveEnergyBurned', 'HKQuantityTypeIdentifierHeight'],
'writeTypes': ['HKQuantityTypeIdentifierDietaryEnergyConsumed', 'HKQuantityTypeIdentifierDietaryFatTotal', 'HKQuantityTypeIdentifierStepCount', 'HKQuantityTypeIdentifierActiveEnergyBurned', 'HKQuantityTypeIdentifierHeight', 'HKQuantityTypeIdentifierDistanceCycling']
},
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 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': 'HKQuantityTypeIdentifierStepCount', // anything in HealthKit/HKTypeIdentifiers.h
'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>