I am very new to objc and swift.
I am trying to access step count from HealthKit.
Can someone tell me whether my query is correct?
And can I call this query directly from my JS file or do I need to append it to an object in objc?
RCT_EXPORT_METHOD(updateStepsCount:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
@objc
func updateStepsCount(_ statisticsCollection: HKStatisticsCollection, _ resolve: @escaping RCTPromiseResolveBlock,
rejecter reject: @escaping (RCTPromiseRejectBlock) -> Void) {
let stepType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!
let startDate = Calendar.current.date(byAdding: .day, value: -7, to: Date())
let anchorDate = Date.mondayAt12AM()
let daily = DateComponents(day: 1)
let predicate = HKQuery.predicateForSamples(withStart: startDate, end: Date(), options: .strictStartDate)
let query = HKStatisticsCollectionQuery(quantityType: stepType, quantitySamplePredicate: predicate, options: .cumulativeSum, anchorDate: anchorDate, intervalComponents: daily)
healthStore.execute(query)
}
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
I need to append 2 JSON.
Currently it is printing as two separate Json.
How do I combine both of my reading???? I want Bothe readings in one JSON
let query = HKSampleQuery(sampleType: glucoseType!, predicate: predicate, limit: 5, sortDescriptors: nil) { (query, results, error) in
for result in results as! [HKQuantitySample] {
let bloodGlucose = result.quantity.description
let bloodGlucoseItem = HealthDataItem(endDate: result.endDate, value: String(bloodGlucose), startDate: result.endDate, type: .bloodGlucose)
let healthData = [bloodGlucoseItem]
do {
let data = try JSONEncoder().encode(healthData)
let json = String(data: data, encoding: String.Encoding.utf8)
print("json data:", json as Any )
resolve(json)
} catch {
//error handling
}
}
}
Console:
json data: Optional("[{\"value\":\"9 mg\\/dL\",\"endDate\":635308200,\"startDate\":635308200,\"type\":\"bloodGlucose\"}]")
json data: Optional("[{\"value\":\"4 mg\\/dL\",\"endDate\":635311800,\"startDate\":635311800,\"type\":\"bloodGlucose\"}]")
I am running a Healthkit query.
I can get the value but how do I access the uuid??? I want to access "90A68DE6-A0E1-473B-944B-1132F447C57D".
How do I get this???
results:
([6 mg/dL 90A68DE6-A0E1-473B-944B-1132F447C57D "Health" (14.2), "iPhone12,1" (14.2)metadata: {
HKWasUserEntered = 1;
} (2021-02-22 09:12:00 +0800 - 2021-02-22 09:12:00 +0800)
let query = HKSampleQuery(sampleType: glucoseType!, predicate: predicate, limit: 20, sortDescriptors: nil) { (query, results, error) in
for result in results as! [HKQuantitySample] {
let bloodGlucose = result.quantity.description // this gives the value "6mg/dL"
}
I am very new to swift.
I need to create the below JSON format
		“BloodPressure:” {
			"endDate" : "2020-01-25",
			“systolicValue” : "122",
			"diastolicValue" : "62"
			"startDate" : "2020-01-25"
		},
	 “HeartRate:” {
			"endDate" : "2020-01-25",
			“Value” : "78",
			"startDate" : "2020-01-25"
		},
	“BMI:” {
			"endDate" : "2020-01-25",
			“Value” : "23",
			"startDate" : "2020-01-25"
		}		
	]
}
Currently I am using enum to convert my data to JSON format. The issue with this is that my blood pressure has different keys (systolic and diastolic). Also I need to display the key names like "Items", "BloodPressure", "HearRate".
How can I achieve my desired JSON format???
		case bloodPressure
		case heartRate
		case bmi
}
struct HealtDataItem: Codable {
		let endDate: Date
		let value: Double
		let startDate: Date
		let type: HealthDataType
}
let bloodPressureItem = HealtDataItem(endDate: end, value: bloodPressureValue, startDate: start, type: .bloodPressure)
let bmiItem = HealtDataItem(endDate: end, value: bmiValue, startDate: start, type: .bmi)
let healthData = [bloodPressureItem, bmiItem]
do {
		let data = try JSONEncoder().encode(healthData)
} catch {
		 //error handling
}
My Blood Pressure query isn't working correctly. Front end is React Native. Trying to access healthkit through RN's native modules.
In the below code if I remove the predicate, I get ALL the values (I can log it) with an error:
"Illegal callback invocation from native module. This callback type only permits a single invocation from native code."
If I add a predicate I do not get any error, it just doesn't work.
I want it to return just today's BP values. Where am I going wrong here?
func getBloodPressure(_ resolve: @escaping RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) {
		guard let type = HKQuantityType.correlationType(forIdentifier: HKCorrelationTypeIdentifier.bloodPressure),
					let systolicType = HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bloodPressureSystolic),
					let diastolicType = HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bloodPressureDiastolic) else {
				return
		}
		
		let startDate = Date()
		let endDate = Date()
		let predicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: .strictStartDate)
		
		let sampleQuery = HKSampleQuery(sampleType: type, predicate: predicate, limit: 0, sortDescriptors: nil) { (sampleQuery, results, error) in
				if let dataList = results as? [HKCorrelation] {
						for data in dataList {
								if let data1 = data.objects(for: systolicType).first as? HKQuantitySample,
									 let data2 = data.objects(for: diastolicType).first as? HKQuantitySample {
										
										let value1 = data1.quantity.doubleValue(for: HKUnit.millimeterOfMercury())
										let value2 = data2.quantity.doubleValue(for: HKUnit.millimeterOfMercury())
										
										print("BP:", "\(value1) / \(value2)")
										resolve("\(value1) / \(value2)")
								}
						}
				}
		}
		healthStore.execute(sampleQuery)
}
Log:
2021-01-22 15:20:48.155422+0800 nativehealth[67473:3946249] Metal API Validation Enabled
BP: 118.0 / 65.0
BP: 122.0 / 78.0
2021-01-22 15:20:51.598068+0800 nativehealth[67473:3946249] [native] Illegal callback invocation from native module. This callback type only permits a single invocation from native code.
2021-01-22 15:20:51.601640+0800 nativehealth[67473:3946264] [javascript] 118
My HealthKit query is not working.
I am trying to get the steps count and I am confused on which part of my code is in-correct.
When I build the app, it either throws "updateStepsCount is not a recognised objc method" or "Unhandled promise rejection".
Where am I going wrong here!!!
Controller.swift:
@objc
	func updateStepsCount(_ statisticsCollection: HKStatisticsCollection, _ resolve: @escaping RCTPromiseResolveBlock,
												rejecter reject: @escaping (RCTPromiseRejectBlock) -> Void) {
			
			let stepType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!
			
			let startDate = Calendar.current.date(byAdding: .day, value: -7, to: Date())
			
			let anchorDate = Date.mondayAt12AM()
			
			let daily = DateComponents(day: 1)
			
			let predicate = HKQuery.predicateForSamples(withStart: startDate, end: Date(), options: .strictStartDate)
			
			let query = HKStatisticsCollectionQuery(quantityType: stepType, quantitySamplePredicate: predicate, options: .cumulativeSum, anchorDate: anchorDate, intervalComponents: daily)
					healthStore.execute(query)
		
		struct Step {
				let id = UUID()
			var count: Int?
				var date: Date?
		}
			let endDate = Date()
			
		statisticsCollection.enumerateStatistics(from: startDate!, to: endDate) { (statistics, stop) in
					
					let count = statistics.sumQuantity()?.doubleValue(for: .count())
					let steps = Int(count ?? 0)
				
				var stepCount = [Step]()
				var tempStepCount = Step(count: steps, date: Date())
					
				tempStepCount.count = steps
				tempStepCount.date = startDate
				stepCount.append(tempStepCount)
			}
		resolve(Step())
	}
Controller.m
RCT_EXTERN_METHOD(updateStepsCount: (HKStatisticsCollection)someStatisticsCollection
									resolve: (RCTPromiseResolveBlock)resolve
									rejecter:(RCTPromiseRejectBlock)reject)
JS side:
clickHandler = async() => {
		const login = HealthkitController.updateStepsCount()
		.then(result => console.warn(result));
	}