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 /// HeartRateMonitor Class. Used for registering callbacks for heart rate monitor and getting heart rate data
16 public class HeartRateMonitor : Sensor
18 private const string HRMKey = "http://tizen.org/feature/sensor.heart_rate_monitor";
21 /// Gets the value of the heart rate monitor.
23 public int HeartRate { get; private set; }
26 /// Returns true or false based on whether heart rate monitor is supported by device.
28 public static bool IsSupported
32 Log.Info(Globals.LogTag, "Checking if the HeartRateMonitor is supported");
33 return CheckIfSupported(SensorType.HeartRateMonitor, HRMKey);
38 /// Returns the number of heart rate monitors available on the device.
40 public static int Count
44 Log.Info(Globals.LogTag, "Getting the count of heart rate monitors");
50 /// Initializes a new instance of the <see cref="Tizen.Sensor.HeartRateMonitor"/> class.
52 /// <param name='index'>
53 /// Index. Default value for this is 0. Index refers to a particular heart rate monitor in case of multiple sensors
55 public HeartRateMonitor(int index = 0) : base(index)
57 Log.Info(Globals.LogTag, "Creating HeartRateMonitor object");
60 internal override SensorType GetSensorType()
62 return SensorType.HeartRateMonitor;
66 /// Event Handler for storing the callback functions for event corresponding to change in heart rate monitor data.
69 public event EventHandler<HeartRateMonitorDataUpdatedEventArgs> DataUpdated;
71 private static int GetCount()
75 int error = Interop.SensorManager.GetSensorList(SensorType.HeartRateMonitor, out list, out count);
76 if (error != (int)SensorError.None)
78 Log.Error(Globals.LogTag, "Error getting sensor list for heart rate");
82 Interop.Libc.Free(list);
86 protected override void EventListenStart()
88 int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, SensorEventCallback, IntPtr.Zero);
89 if (error != (int)SensorError.None)
91 Log.Error(Globals.LogTag, "Error setting event callback for heart rate monitor");
92 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for heart rate");
96 protected override void EventListenStop()
98 int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
99 if (error != (int)SensorError.None)
101 Log.Error(Globals.LogTag, "Error unsetting event callback for heart rate monitor");
102 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for heart rate");
106 private void SensorEventCallback(IntPtr sensorHandle, IntPtr sensorPtr, IntPtr data)
108 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(sensorPtr);
109 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
110 HeartRate = (int)sensorData.values[0];
112 DataUpdated?.Invoke(this, new HeartRateMonitorDataUpdatedEventArgs((int)sensorData.values[0]));