Reputation: 21
I'm setting up a Ionic V4 app and trying to visualize the systolic and diastolic blood pressure in a chart. Therefore, I'm trying with health-kit following the link https://github.com/dariosalvi78/cordova-plugin-health to make a query to my Apple Health App.
The queries are working fine so far for steps, activities, sizes and all the stuff but it's not working for the blood pressure. Following is my constructor which is starts the authentication with loading the page:
constructor(private healthKit: HealthKit, private plt: Platform) {
this.plt.ready().then(() => {
console.log('platform ready');
this.healthKit.available().then(available => {
console.log('health kit ready: ', available);
if (available) {
// Request all permissions up front if you like to
const options: HealthKitOptions = {
readTypes: [
'HKQuantityTypeIdentifierBloodPressureSystolic',
'HKQuantityTypeIdentifierBloodPressureDiastolic',
],
writeTypes: [
'HKQuantityTypeIdentifierBloodPressureSystolic',
'HKQuantityTypeIdentifierBloodPressureDiastolic',
]
}
this.healthKit.requestAuthorization(options).then(_ => {
this.loadHealthData();
});
}
});
});
}
The function loadHealthData starts after the authentication the query to the health App:
loadHealthData() {
const bloodPressureOptions = {
startDate: new Date(new Date().getTime() - 24 * 60 * 60 * 1000),
endDate: new Date(),
correlationType: 'HKCorrelationTypeIdentifierBloodPressure',
unit: 'mmHg'
}
this.healthKit.queryCorrelationType(bloodPressureOptions
).then( value => {
console.log('blood pressure data: ', value);
}, err => {
console.log('error getting blood pressure: ', err);
});
}
Unfortunately the logged value is always empty although I have many blood pressure data in my Health App. When I'm adding 'HKCorrelationTypeIdentifierBloodPressure' to the read permissions I'm getting an error that this cannot be authorized. So I read it can be made by getting permissions for systolic and diastolic and doing the correlation than with these two permissions. I also tried to make a sampleQuery for systolic and diastolic pressure only but the response is also empty there. Furthermore, I've seen that in my Health App my own app is not listed in the permissions for the blood pressure but I have no idea for this.
I would really appreciate any help with this issue!
Upvotes: 2
Views: 318
Reputation: 153
I tried using the below code.
constructor(
private healthKit: HealthKit,
private platform: Platform,
private zone: NgZone
) {
platform.ready().then(() => {
this.platform.resume.subscribe(async () => {
await this.test();
});
});
}
async test() {
this.healthKit.available().then((available) => {
if (available) {
const options: HealthKitOptions = {
readTypes: [ 'HKQuantityTypeIdentifierBloodPressureSystolic',
'HKQuantityTypeIdentifierBloodPressureDiastolic']
};
this.healthKit.requestAuthorization(options).then((_) => {
this.loadHealthData();
});
}
});
}
loadHealthData(){
const bloodPressureOptions = {
startDate: new Date(new Date().getTime() - 30 * 24 * 60 * 60 * 1000),
endDate: new Date(),
correlationType: 'HKCorrelationTypeIdentifierBloodPressure',
unit: 'mmHg'
}
this.healthKit.queryCorrelationType(bloodPressureOptions
).then(data => {
this.zone.run(() => {
console.log(data)
}
)
})
}
The above code returns me the below logs:
[log] - [{"metadata":{"HKWasUserEntered":1},"samples":[{"unit":"mmHg","sampleType":"HKQuantityTypeIdentifierBloodPressureSystolic","endDate":"2022-06-03T13:02:00+05:30","startDate":"2022-06-03T13:02:00+05:30","UUID":"0B7BAD5A-3A54-4680-85E0-13B92AAE01D3","value":200,"metadata":{"HKWasUserEntered":1}},{"unit":"mmHg","sampleType":"HKQuantityTypeIdentifierBloodPressureDiastolic","endDate":"2022-06-03T13:02:00+05:30","startDate":"2022-06-03T13:02:00+05:30","UUID":"2CD76ADC-2C33-42A9-804B-EB0B4B6927F1","value":100,"metadata":{"HKWasUserEntered":1}}],"endDate":"2022-06-03T13:02:00+05:30","startDate":"2022-06-03T13:02:00+05:30","UUID":"384254F3-81EC-4B3C-877B-00F19EAAAB3B","correlationType":"HKCorrelationTypeIdentifierBloodPressure"},{"metadata":{"HKWasUserEntered":1},"samples":[{"unit":"mmHg","sampleType":"HKQuantityTypeIdentifierBloodPressureSystolic","endDate":"2022-06-05T12:21:00+05:30","startDate":"2022-06-05T12:21:00+05:30","UUID":"53136DF5-9190-4401-B599-94EDFA6204CD","value":100,"metadata":{"HKWasUserEntered":1}},{"unit":"mmHg","sampleType":"HKQuantityTypeIdentifierBloodPressureDiastolic","endDate":"2022-06-05T12:21:00+05:30","startDate":"2022-06-05T12:21:00+05:30","UUID":"14632D4C-8D32-4D2E-9420-4E4FA73CC951","value":100,"metadata":{"HKWasUserEntered":1}}],"endDate":"2022-06-05T12:21:00+05:30","startDate":"2022-06-05T12:21:00+05:30","UUID":"3B0F0434-83CC-41D2-955F-9F1195D7D3CF","correlationType":"HKCorrelationTypeIdentifierBloodPressure"},{"metadata":{"HKWasUserEntered":1},"samples":[{"unit":"mmHg","sampleType":"HKQuantityTypeIdentifierBloodPressureDiastolic","endDate":"2022-06-05T18:54:00+05:30","startDate":"2022-06-05T18:54:00+05:30","UUID":"EBDC9CF9-D273-4439-9A4B-3C92B692D377","value":100,"metadata":{"HKWasUserEntered":1}},{"unit":"mmHg","sampleType":"HKQuantityTypeIdentifierBloodPressureSystolic","endDate":"2022-06-05T18:54:00+05:30","startDate":"2022-06-05T18:54:00+05:30","UUID":"0679C83C-2089-491D-B482-F95453F41824","value":110,"metadata":{"HKWasUserEntered":1}}],"endDate":"2022-06-05T18:54:00+05:30","startDate":"2022-06-05T18:54:00+05:30","UUID":"32CC62C0-E5B0-48B0-AEA7-0F5FA4D72E6B","correlationType":"HKCorrelationTypeIdentifierBloodPressure"},{"metadata":{"HKWasUserEntered":1},"samples":[{"unit":"mmHg","sampleType":"HKQuantityTypeIdentifierBloodPressureSystolic","endDate":"2022-06-06T10:26:00+05:30","startDate":"2022-06-06T10:26:00+05:30","UUID":"88AAB09B-1F26-49CF-88D1-D7E5B5B4BE31","value":120,"metadata":{"HKWasUserEntered":1}},{"unit":"mmHg","sampleType":"HKQuantityTypeIdentifierBloodPressureDiastolic","endDate":"2022-06-06T10:26:00+05:30","startDate":"2022-06-06T10:26:00+05:30","UUID":"6BD456A5-EE3F-426E-A34B-66AA774FBEB5","value":100,"metadata":{"HKWasUserEntered":1}}],"endDate":"2022-06-06T10:26:00+05:30","startDate":"2022-06-06T10:26:00+05:30","UUID":"DA3F0EAC-B325-4916-B9CD-A4FAFBD5C508","correlationType":"HKCorrelationTypeIdentifierBloodPressure"},{"metadata":{"HKWasUserEntered":1},"samples":[{"unit":"mmHg","sampleType":"HKQuantityTypeIdentifierBloodPressureSystolic","endDate":"2022-06-06T10:45:00+05:30","startDate":"2022-06-06T10:45:00+05:30","UUID":"7E8157F0-6051-42F7-ACA2-F47B51C3638A","value":125,"metadata":{"HKWasUserEntered":1}},{"unit":"mmHg","sampleType":"HKQuantityTypeIdentifierBloodPressureDiastolic","endDate":"2022-06-06T10:45:00+05:30","startDate":"2022-06-06T10:45:00+05:30","UUID":"5FEE364B-3EBB-4B12-A07D-8D8BE0897F50","value":105,"metadata":{"HKWasUserEntered":1}}],"endDate":"2022-06-06T10:45:00+05:30","startDate":"2022-06-06T10:45:00+05:30","UUID":"3C32FBB4-C432-4119-895A-05E3AA9CFCB4","correlationType":"HKCorrelationTypeIdentifierBloodPressure"},{"metadata":{"HKWasUserEntered":1},"samples":[{"unit":"mmHg","sampleType":"HKQuantityTypeIdentifierBloodPressureDiastolic","endDate":"2022-06-06T10:49:00+05:30","startDate":"2022-06-06T10:49:00+05:30","UUID":"380F2276-E980-476D-9D29-618FC2E332A4","value":120,"metadata":{"HKWasUserEntered":1}},{"unit":"mmHg","sampleType":"HKQuantityTypeIdentifierBloodPressureSystolic","endDate":"2022-06-06T10:49:00+05:30","startDate":"2022-06-06T10:49:00+05:30","UUID":"C458EAF5-9342-4963-B2C5-65D52E94760D","value":120,"metadata":{"HKWasUserEntered":1}}],"endDate":"2022-06-06T10:49:00+05:30","startDate":"2022-06-06T10:49:00+05:30","UUID":"87845254-3483-46F1-908E-10D34253BF24","correlationType":"HKCorrelationTypeIdentifierBloodPressure"},{"metadata":{"HKWasUserEntered":1},"samples":[{"unit":"mmHg","sampleType":"HKQuantityTypeIdentifierBloodPressureSystolic","endDate":"2022-06-07T11:49:00+05:30","startDate":"2022-06-07T11:49:00+05:30","UUID":"4D437E52-AC2C-4C16-85FE-ECA34F8D4717","value":111,"metadata":{"HKWasUserEntered":1}},{"unit":"mmHg","sampleType":"HKQuantityTypeIdentifierBloodPressureDiastolic","endDate":"2022-06-07T11:49:00+05:30","startDate":"2022-06-07T11:49:00+05:30","UUID":"30DB0A7B-ACEB-43E8-B94B-3739A30716C6","value":110,"metadata":{"HKWasUserEntered":1}}],"endDate":"2022-06-07T11:49:00+05:30","startDate":"2022-06-07T11:49:00+05:30","UUID":"6B8CF463-A314-42F5-B41C-561F1D93AB58","correlationType":"HKCorrelationTypeIdentifierBloodPressure"}]
I have used ngZone to update the value on the App dynamically, if the data on the Health App changes. Let me know if any queries, I will be happy to help.
Upvotes: 0