1 // Copyright 2016 by Samsung Electronics, Inc.,
3 // This software is the confidential and proprietary information
4 // of Samsung Electronics, Inc. ("Confidential Information"). You
5 // shall not disclose such Confidential Information and shall use
6 // it only in accordance with the terms of the license agreement
7 // you entered into with Samsung.
11 namespace Tizen.Sensor
14 /// Pedometer Sensor Class. Used for registering callbacks for pedometer and getting pedometer data
16 public class Pedometer : Sensor
18 private static string PedometerKey = "http://tizen.org/feature/sensor.pedometer";
21 /// Gets the step count
23 public int StepCount { get; private set; }
26 /// Gets the walking step count
28 public int WalkStepCount { get; private set; }
31 /// Gets the running step count
33 public int RunStepCount { get; private set; }
36 /// Gets the moving distance
38 public float MovingDistance { get; private set; }
41 /// Gets the calorie burned
43 public float CalorieBurned { get; private set; }
46 /// Gets the last speed
48 public float LastSpeed { get; private set; }
51 /// Gets the last stepping frequency
53 public float LastSteppingFrequency { get; private set; }
56 /// Gets the last step status
58 public PedometerState LastStepStatus { get; private set; }
61 /// Returns true or false based on whether pedometer sensor is supported by device.
63 public static bool IsSupported
67 Log.Info(Globals.LogTag, "Checking if the Pedometer sensor is supported");
68 return CheckIfSupported(SensorType.Pedometer, PedometerKey);
73 /// Returns the number of pedometer sensors available on the device.
75 public static int Count
79 Log.Info(Globals.LogTag, "Getting the count of pedometer sensors");
85 /// Initializes a new instance of the <see cref="Tizen.Sensor.Pedometer"/> class.
88 /// For accessing pedometer, app should have http://tizen.org/privilege/healthinfo privilege.
90 /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
91 /// <exception cref="NotSupportedException">Thrown when the sensor is not supported</exception>
92 /// <exception cref="UnauthroizedAccessException">Thrown when the app has no privilege to use the sensor</exception>
93 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
94 /// <param name='index'>
95 /// Index. Default value for this is 0. Index refers to a particular pedometer sensor in case of multiple sensors
97 public Pedometer(int index = 0) : base(index)
99 Log.Info(Globals.LogTag, "Creating Pedometer object");
102 internal override SensorType GetSensorType()
104 return SensorType.Pedometer;
108 /// Event Handler for storing the callback functions for event corresponding to change in pedometer sensor data.
111 public event EventHandler<PedometerDataUpdatedEventArgs> DataUpdated;
113 private static int GetCount()
117 int error = Interop.SensorManager.GetSensorList(SensorType.Pedometer, out list, out count);
118 if (error != (int)SensorError.None)
120 Log.Error(Globals.LogTag, "Error getting sensor list for pedometer");
124 Interop.Libc.Free(list);
128 protected override void EventListenStart()
130 int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, SensorEventCallback, IntPtr.Zero);
131 if (error != (int)SensorError.None)
133 Log.Error(Globals.LogTag, "Error setting event callback for pedometer sensor");
134 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for pedometer");
138 protected override void EventListenStop()
140 int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
141 if (error != (int)SensorError.None)
143 Log.Error(Globals.LogTag, "Error unsetting event callback for pedometer sensor");
144 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for pedometer");
148 private void SensorEventCallback(IntPtr sensorHandle, IntPtr sensorPtr, IntPtr data)
150 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(sensorPtr);
151 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
152 StepCount = (int)sensorData.values[0];
153 WalkStepCount = (int)sensorData.values[1];
154 RunStepCount = (int)sensorData.values[2];
155 MovingDistance = sensorData.values[3];
156 CalorieBurned = sensorData.values[4];
157 LastSpeed = sensorData.values[5];
158 LastSteppingFrequency = sensorData.values[6];
159 LastStepStatus = (PedometerState)sensorData.values[7];
161 DataUpdated?.Invoke(this, new PedometerDataUpdatedEventArgs(sensorData.values));