/// <summary> /// Factory that creates a StepCountData object from a Lumia StepCount /// </summary> /// <param name="stepCount"></param> /// <returns></returns> public static StepCountData FromLumiaStepCount(StepCount stepCount) { StepCountData steps = new StepCountData(); steps.RunningCount = stepCount.RunningStepCount; steps.WalkingCount = stepCount.WalkingStepCount; steps.UnknownCount = 0; return(steps); }
/// <summary> /// Factory that creates a StepCountData object from PedometerReadings /// </summary> /// <param name="readings"></param> /// <returns></returns> public static StepCountData FromPedometerReadings(IReadOnlyList <PedometerReading> readings) { StepCountData steps = new StepCountData(); // Get the most recent batch of 3 readings (one per StepKind) for (int i = 0; i < readings.Count && i < 3; i++) { var reading = readings[readings.Count - i - 1]; switch (reading.StepKind) { case PedometerStepKind.Running: steps.RunningCount = (uint)reading.CumulativeSteps; break; case PedometerStepKind.Walking: steps.WalkingCount = (uint)reading.CumulativeSteps; break; case PedometerStepKind.Unknown: steps.UnknownCount = (uint)reading.CumulativeSteps; break; default: break; } } // Subtract the counts from the earliest batch of 3 readings (one per StepKind) for (int i = 0; i < readings.Count && i < 3; i++) { var reading = readings[i]; switch (reading.StepKind) { case PedometerStepKind.Running: steps.RunningCount -= (uint)reading.CumulativeSteps; break; case PedometerStepKind.Walking: steps.WalkingCount -= (uint)reading.CumulativeSteps; break; case PedometerStepKind.Unknown: steps.UnknownCount -= (uint)reading.CumulativeSteps; break; default: break; } } return(steps); }
/// <summary> /// Factory that creates a StepCountData object from PedometerReadings /// </summary> /// <param name="readings"></param> /// <returns></returns> public static StepCountData FromPedometerReadings(IReadOnlyList<PedometerReading> readings) { StepCountData steps = new StepCountData(); // Get the most recent batch of 3 readings (one per StepKind) for (int i = 0; i < readings.Count && i < 3; i++) { var reading = readings[readings.Count - i - 1]; switch (reading.StepKind) { case PedometerStepKind.Running: steps.RunningCount = (uint)reading.CumulativeSteps; break; case PedometerStepKind.Walking: steps.WalkingCount = (uint)reading.CumulativeSteps; break; case PedometerStepKind.Unknown: steps.UnknownCount = (uint)reading.CumulativeSteps; break; default: break; } } // Subtract the counts from the earliest batch of 3 readings (one per StepKind) for (int i = 0; i < readings.Count && i < 3; i++) { var reading = readings[i]; switch (reading.StepKind) { case PedometerStepKind.Running: steps.RunningCount -= (uint)reading.CumulativeSteps; break; case PedometerStepKind.Walking: steps.WalkingCount -= (uint)reading.CumulativeSteps; break; case PedometerStepKind.Unknown: steps.UnknownCount -= (uint)reading.CumulativeSteps; break; default: break; } } return steps; }
/// <summary> /// Factory that creates a StepCountData object from a Lumia StepCount /// </summary> /// <param name="stepCount"></param> /// <returns></returns> public static StepCountData FromLumiaStepCount(StepCount stepCount) { StepCountData steps = new StepCountData(); steps.RunningCount = stepCount.RunningStepCount; steps.WalkingCount = stepCount.WalkingStepCount; steps.UnknownCount = 0; return steps; }
/// <summary> /// Gets number of steps for current day /// </summary> /// <returns><c>true</c> if steps were successfully fetched, <c>false</c> otherwise</returns> private async Task<bool> GetStepsAsync() { // First try the pedometer try { var readings = await Pedometer.GetSystemHistoryAsync(DateTime.Now.Date, DateTime.Now - DateTime.Now.Date); _steps = StepCountData.FromPedometerReadings(readings); return true; } catch (Exception) { // Continue to the fallback } // Fall back to using Lumia Sensor Core. StepCounter stepCounter = null; try { stepCounter = await StepCounter.GetDefaultAsync(); StepCount count = await stepCounter.GetStepCountForRangeAsync( DateTime.Now.Date, DateTime.Now - DateTime.Now.Date); _steps = StepCountData.FromLumiaStepCount(count); } catch (Exception e) { _lastError = SenseHelper.GetSenseError(e.HResult); return false; } finally { if (stepCounter != null) { stepCounter.Dispose(); } } return true; }