2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 namespace Tizen.Sensor
22 /// The HeartRateMonitor class is used for registering callbacks for the heart rate monitor and getting the heart rate data.
24 public sealed class HeartRateMonitor : Sensor
26 private const string HRMKey = "http://tizen.org/feature/sensor.heart_rate_monitor";
29 /// Gets the value of the heart rate monitor.
31 /// <since_tizen> 3 </since_tizen>
32 /// <value> The heart rate. </value>
33 public int HeartRate { get; private set; } = int.MinValue;
36 /// Returns true or false based on whether the heart rate monitor is supported by the device.
38 /// <since_tizen> 3 </since_tizen>
39 /// <value><c>true</c> if supported; otherwise <c>false</c>.</value>
40 public static bool IsSupported
44 Log.Info(Globals.LogTag, "Checking if the HeartRateMonitor is supported");
45 return CheckIfSupported(SensorType.HeartRateMonitor, HRMKey);
50 /// Returns the number of heart rate monitors available on the device.
52 /// <since_tizen> 3 </since_tizen>
53 /// <value> The count of heart rate monitors. </value>
54 public static int Count
58 Log.Info(Globals.LogTag, "Getting the count of heart rate monitors");
64 /// Initializes a new instance of the <see cref="Tizen.Sensor.HeartRateMonitor"/> class.
66 /// <since_tizen> 3 </since_tizen>
67 /// <privilege>http://tizen.org/privilege/healthinfo</privilege>
68 /// <privlevel>public</privlevel>
69 /// <feature>http://tizen.org/feature/sensor.heart_rate_monitor</feature>
70 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
71 /// <exception cref="NotSupportedException">Thrown when the sensor is not supported.</exception>
72 /// <exception cref="UnauthorizedAccessException">Thrown when the application has no privilege to use the sensor.</exception>
73 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
74 /// <param name='index'>
75 /// Index. Default value for this is 0. Index refers to a particular heart rate monitor in case of multiple sensors.
77 public HeartRateMonitor(uint index = 0) : base(index)
79 Log.Info(Globals.LogTag, "Creating HeartRateMonitor object");
82 internal override SensorType GetSensorType()
84 return SensorType.HeartRateMonitor;
88 /// An event handler for storing the callback functions for the event corresponding to the change in the heart rate monitor data.
90 /// <since_tizen> 3 </since_tizen>
91 public event EventHandler<HeartRateMonitorDataUpdatedEventArgs> DataUpdated;
93 private static int GetCount()
97 int error = Interop.SensorManager.GetSensorList(SensorType.HeartRateMonitor, out list, out count);
98 if (error != (int)SensorError.None)
100 Log.Error(Globals.LogTag, "Error getting sensor list for heart rate");
104 Interop.Libc.Free(list);
108 private static Interop.SensorListener.SensorEventCallback _callback;
110 internal override void EventListenStart()
112 _callback = (IntPtr sensorHandle, IntPtr eventPtr, IntPtr data) => {
113 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(eventPtr);
115 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
116 HeartRate = (int)sensorData.values[0];
118 DataUpdated?.Invoke(this, new HeartRateMonitorDataUpdatedEventArgs((int)sensorData.values[0]));
121 int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, _callback, IntPtr.Zero);
122 if (error != (int)SensorError.None)
124 Log.Error(Globals.LogTag, "Error setting event callback for heart rate monitor");
125 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for heart rate");
129 internal override void EventListenStop()
131 int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
132 if (error != (int)SensorError.None)
134 Log.Error(Globals.LogTag, "Error unsetting event callback for heart rate monitor");
135 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for heart rate");