来自HealthKit的Xamarin iOS阅读步骤计数

时间:2017-10-03 13:57:00

标签: c# xamarin xamarin.ios health-kit

我正在尝试从用户那里追溯到365天之后的步骤,然后将其上传到服务器。但我目前仍然在提取数据,我正确地从iOS健康套件获得了许可,但我的数据的返回类型只是得到“[0:] HealthKit.HKSample []”

public void GetSteps()
{        
    var healthKitStore = new HKHealthStore();
    var stepRateType = HKQuantityType.Create(HKQuantityTypeIdentifier.StepCount);
    var sort = new NSSortDescriptor(HKSample.SortIdentifierStartDate, true);
    var q = new HKSampleQuery(stepRateType, HKQuery.GetPredicateForSamples(NSDate.Now.AddSeconds(TimeSpan.FromDays(-365).TotalSeconds), NSDate.Now.AddSeconds(TimeSpan.FromDays(1).TotalSeconds), HKQueryOptions.None), 0, new NSSortDescriptor[] { },
        new HKSampleQueryResultsHandler((HKSampleQuery query2,HKSample[] results, NSError error2) =>
        {
            var query = results; //property created within the model to expose later.
            Debug.WriteLine(query);
            Debug.WriteLine(results);
        }));
    healthKitStore.ExecuteQuery(q); 
}

1 个答案:

答案 0 :(得分:0)

I think I know why you are getting "[0:] HealthKit.HKSample[]", you are trying to Debug.WriteLine an array of objects. The results variable is an array. Loop through the array instead and extract out the "Quantity", "StartDate", and "EndDate" among other fields that are available:

foreach (var item in results)
{
    var sample = (HKQuantitySample) item;
    var hkUnit = HKUnit.Count;
    var quantity = sample.Quantity.GetDoubleValue(hkUnit);
    var startDateTime = sample.StartDate.ToDateTime().ToLocalTime();
    var endDateTime = sample.EndDate.ToDateTime().ToLocalTime();
    Debug.WriteLine(quantity);
    Debug.WriteLine(startDateTime);
    Debug.WriteLine(endDateTime);
}