private void DrawDataTypes(HKDataType startType, HKDataType endType) { for (int i = (int)startType; i <= (int)endType; i++) { DrawDataType((HKDataType)i); } }
void Awake() { //dropdown.ClearOptions(); List <string> opts = new List <string>(); for (int i = 0; i <= (int)HKDataType.HKQuantityTypeIdentifierUVExposure; i++) { HKDataType dataType = (HKDataType)i; opts.Add(HealthKitDataTypes.GetIdentifier(dataType)); } opts.Add("———"); for (int i = (int)HKDataType.HKCharacteristicTypeIdentifierBiologicalSex; i <= (int)HKDataType.HKCharacteristicTypeIdentifierWheelchairUse; i++) { HKDataType dataType = (HKDataType)i; opts.Add(HealthKitDataTypes.GetIdentifier(dataType)); } opts.Add("———"); for (int i = (int)HKDataType.HKCorrelationTypeIdentifierBloodPressure; i <= (int)HKDataType.HKCorrelationTypeIdentifierFood; i++) { HKDataType dataType = (HKDataType)i; opts.Add(HealthKitDataTypes.GetIdentifier(dataType)); } opts.Add("———"); opts.Add(HealthKitDataTypes.GetIdentifier(HKDataType.HKWorkoutTypeIdentifier)); //dropdown.AddOptions(opts); }
// Characteristic types /*! @brief Read a characteristic. * @details * @param dataType The datatype to read. * @param handler Called when the function finishes executing. */ public void ReadCharacteristic(HKDataType dataType, ReceivedCharacteristic handler) { this.receivedCharacteristicHandlers[dataType] = handler; string identifier = HealthKitDataTypes.GetIdentifier(dataType); _ReadCharacteristic(identifier); }
public void ReadData() { Debug.Log("read data..."); if (!reading) { // string selectedName = dropdown.options[dropdown.value].text; try { HKDataType dataType = HKDataType.HKQuantityTypeIdentifierStepCount; reading = true; DateTimeOffset now = DateTimeOffset.UtcNow; // for this example, we'll read everything from the past 24 hours // commented prev example, trying to read current pedo value // DateTimeOffset start = now.AddDays(-1); ReadPedometer(now); // if (dataType <= HKDataType.HKQuantityTypeIdentifierUVExposure) { // // quantity-type // Debug.Log("reading quantity-type..."); // ReadQuantityData(dataType, start, now); // } // else if (dataType <= HKDataType.HKCategoryTypeIdentifierMindfulSession) { // // category-type // Debug.Log("reading category-type..."); // } // else if (dataType <= HKDataType.HKCharacteristicTypeIdentifierWheelchairUse) { // // characteristic-type // Debug.Log("reading characteristic-type..."); // ReadCharacteristic(dataType); // } // else if (dataType <= HKDataType.HKCorrelationTypeIdentifierFood) { // // correlation-type // Debug.Log("reading correlation-type..."); // ReadCorrelationData(dataType, start, now); // } // else if (dataType == HKDataType.HKWorkoutTypeIdentifier) { // // finally, workout-type // Debug.Log("reading workout-type..."); // ReadWorkoutData(dataType, start, now); // } else { // Debug.LogError(string.Format("data type {0} invalid", HealthKitDataTypes.GetIdentifier(dataType))); // } } catch (ArgumentException) { // they just selected a divider; nothing to worry about Debug.Log("Try for ReadData Failed"); } // Or: // ReadSteps(start, now); // ReadFlights(start, now); // or alternatively // ReadSleep(start, now); // or... // ReadPedometer(now); } }
/*! @brief The default constructor. * @param xmlString the XML string to create this object from. */ public HealthData(string xmlString) { xml = new XmlDocument(); xml.LoadXml(xmlString); XmlNode node = xml.FirstChild["datatype"]; if (node != null && node.InnerText != null) { this.datatype = (HKDataType)System.Enum.Parse(typeof(HKDataType), (string)node.InnerText); } else { Debug.LogError("datatype node is missing or invalid"); } if (this.datatype == HKDataType.HKWorkoutTypeIdentifier) { node = xml.FirstChild["workoutType"]; if (node != null && node.InnerText != null) { this.workoutType = (WorkoutActivityType)Enum.ToObject(typeof(WorkoutActivityType), node.InnerText); } else { Debug.LogError("workoutType node is missing or invalid"); } } }
private void InitializeEntry(HKDataType type, string typeName, bool writable = true) { string key = GetIdentifier(type); if (!data.ContainsKey(key)) { data[key] = new HKNameValuePair(typeName, writable); } }
private void ReadCorrelation(HKDataType dataType, DateTimeOffset startDate, DateTimeOffset endDate, bool combineSamples) { if (this.IsHealthDataAvailable()) { string identifier = HealthKitDataTypes.GetIdentifier(dataType); _ReadCorrelation(identifier, DateTimeBridge.DateToString(startDate), DateTimeBridge.DateToString(endDate), combineSamples); } else { Debug.LogError("Error: no health data is available. Are you running on an iOS device that supports HealthKit?"); } }
/*! @brief Write a category sample. * @details * @param dataType The datatype to write. * @param value the (integer) value to use to create a sample. * @param startDate The starting date of the sample to write. * @param endDate The ending date of the sample to write. * @param handler Called when the function finishes executing. */ public void WriteCategorySample(HKDataType dataType, int value, DateTimeOffset startDate, DateTimeOffset endDate, WroteSample handler) { this.wroteSampleHandlers[dataType] = handler; if (this.IsHealthDataAvailable()) { string identifier = HealthKitDataTypes.GetIdentifier(dataType); _WriteCategory(identifier, value, DateTimeBridge.DateToString(startDate), DateTimeBridge.DateToString(endDate)); } else { Debug.LogError("Error: no health data is available. Are you running on an iOS device that supports HealthKit?"); } }
// reading a Characteristic private void ReadCharacteristic(HKDataType dataType) { string typeName = HealthKitDataTypes.GetIdentifier(dataType); Debug.LogFormat("reading {0}", typeName); this.healthStore.ReadCharacteristic(dataType, delegate(Characteristic characteristic) { Debug.Log("FINISHED"); string text = string.Format("{0} = {1}", dataType, characteristic); this.resultsLabel.text = text; // all done reading = false; }); }
// a generic example of reading correlation data. If you're interested in nutritional correlations, you'd probably tailor your delegate to the specific nutritional information you're looking at. private void ReadCorrelationData(HKDataType dataType, DateTimeOffset start, DateTimeOffset end) { this.healthStore.ReadCorrelationSamples(dataType, start, end, delegate(List <CorrelationSample> samples) { string text = ""; foreach (CorrelationSample sample in samples) { Debug.Log(" - " + sample); text = text + "- " + sample + "\n"; } this.resultsLabel.text = text; // all done reading = false; }); }
private void ReadWorkoutData(HKDataType dataType, DateTimeOffset start, DateTimeOffset end) { this.healthStore.ReadWorkoutSamples(WorkoutActivityType.Fencing, start, end, delegate(List <WorkoutSample> samples) { string text = ""; foreach (WorkoutSample sample in samples) { Debug.Log(" - " + sample); text = text + "- " + sample + "\n"; } this.resultsLabel.text = text; // all done reading = false; }); }
private void ReadQuantity(HKDataType dataType, DateTimeOffset startDate, DateTimeOffset endDate, bool combineSamples) { if (this.IsHealthDataAvailable()) { string identifier = HealthKitDataTypes.GetIdentifier(dataType); string startStamp = DateTimeBridge.DateToString(startDate); string endStamp = DateTimeBridge.DateToString(endDate); Debug.LogFormat("reading quantity from:\n-{0} ({1})\nto:\n-{2} ({3})", startDate, startStamp, endDate, endStamp); _ReadQuantity(identifier, startStamp, endStamp, combineSamples); } else { Debug.LogError("Error: no health data is available. Are you running on an iOS device that supports HealthKit?"); } }
// A basic example of reading Quantity data. private void ReadQuantityData(HKDataType dataType, DateTimeOffset start, DateTimeOffset end) { string typeName = HealthKitDataTypes.GetIdentifier(dataType); Debug.LogFormat("reading {0} from {1} to {2}", typeName, start, end); double sum = 0; this.healthStore.ReadQuantitySamples(dataType, start, end, delegate(List <QuantitySample> samples) { if (samples.Count > 0) { Debug.Log("found " + samples.Count + " samples"); bool cumulative = (samples[0].quantityType == QuantityType.cumulative); string text = ""; foreach (QuantitySample sample in samples) { Debug.Log(" - " + sample); if (cumulative) { sum += Convert.ToInt32(sample.quantity.doubleValue); } else { text = text + "- " + sample + "\n"; } } if (cumulative) { if (sum > 0) { this.resultsLabel.text = typeName + ":" + sum; } } else { this.resultsLabel.text = text; } } else { Debug.Log("found no samples"); } // all done reading = false; }); }
/*! @brief returns authorization status for a given datatype. * @details See [HKAuthorizationStatus](https://developer.apple.com/documentation/healthkit/hkauthorizationstatus) in the Apple documentation. * More useful for write permission; will not tell you if the user denies permission to read the data; it will merely appear as if there is no data. * @param dataType the HealthKit datatype to query */ public HKAuthorizationStatus AuthorizationStatusForType(HKDataType dataType) { HKAuthorizationStatus status = HKAuthorizationStatus.NotDetermined; #if UNITY_IOS && !UNITY_EDITOR string identifier = HealthKitDataTypes.GetIdentifier(dataType); try { status = (HKAuthorizationStatus)_AuthorizationStatusForType(identifier); } catch (System.Exception) { Debug.LogErrorFormat("error parsing authorization status: '{0}'", identifier); } #endif return(status); }
private void DrawDataType(HKDataType dataType) { GUILayout.BeginHorizontal(); Dictionary <string, HKNameValuePair> data = obj.data; if (data != null) { string key = HealthKitDataTypes.GetIdentifier(dataType); if (data.ContainsKey(key)) { EditorGUILayout.LabelField(data[key].name, GUILayout.MaxWidth(240)); EditorGUI.BeginChangeCheck(); bool readValue = EditorGUILayout.Toggle(data[key].read, GUILayout.MaxWidth(40)); if (EditorGUI.EndChangeCheck()) { data[key].read = readValue; string saveData = obj.Save(); this.saveDataProperty.stringValue = saveData; } if (!data[key].writable) { GUI.enabled = false; } EditorGUI.BeginChangeCheck(); bool writeValue = EditorGUILayout.Toggle(data[key].write, GUILayout.MaxWidth(40)); if (EditorGUI.EndChangeCheck()) { data[key].write = writeValue; // EditorUtility.SetDirty(prop.serializedObject.targetObject); string saveData = obj.Save(); this.saveDataProperty.stringValue = saveData; } GUI.enabled = true; } else { EditorGUILayout.LabelField(key, GUILayout.MaxWidth(240)); EditorGUILayout.LabelField("ERROR", GUILayout.MaxWidth(80)); } } GUILayout.EndHorizontal(); }
// Correlation types /*! @brief Read correlation samples & return a list of CorrelationSamples. * @details * @param dataType The datatype to read. * @param startDate The date to start reading samples from. * @param endDate The end date to limit samples to. * @param handler Called when the function finishes executing. */ public void ReadCorrelationSamples(HKDataType dataType, DateTimeOffset startDate, DateTimeOffset endDate, ReceivedCorrelationSamples handler) { this.receivedCorrelationSamplesHandlers[dataType] = handler; ReadCorrelation(dataType, startDate, endDate, false); }
/*! @brief Convenience method to get a HKDataType as a string value */ public static string GetIdentifier(HKDataType type) { return(Enum.GetName(typeof(HKDataType), type)); }
// Category types /*! @brief Read category samples & return a list of CategorySamples. * @details * @param dataType The datatype to read. * @param startDate The date to start reading samples from. * @param endDate The end date to limit samples to. * @param handler Called when the function finishes executing. */ public void ReadCategorySamples(HKDataType dataType, DateTimeOffset startDate, DateTimeOffset endDate, ReceivedCategorySamples handler) { this.receivedCategorySamplesHandlers[dataType] = handler; this.ReadCategory(dataType, startDate, endDate); }
/*! @brief Read quantity samples & return a list of QuantitySamples. * @details * @param dataType The datatype to read. * @param startDate The date to start reading samples from. * @param endDate The end date to limit samples to. * @param handler Called when the function finishes executing. */ public void ReadQuantitySamples(HKDataType dataType, DateTimeOffset startDate, DateTimeOffset endDate, ReceivedQuantitySamples handler) { this.receivedQuantitySamplesHandlers[dataType] = handler; this.ReadQuantity(dataType, startDate, endDate, false); }