How to Use Sensors on a Galaxy Watch

Sungsu Kim

Engineer

Many Tizen .NET developers have asked how to use sensor features on a Galaxy Watch. This blog introduces sensors that are supported for Galaxy Watch and how developers can use them.

Environment

Target devices

The following Samsung wearables can be used with the Tizen.NET NuGet package:

The Accelerometer, Barometer, Gyro, HR, and Light sensors are supported.

Tizen .NET NuGet package

To use sensor features on Galaxy Watch and other wearable applications, refer to the Tizen.NET NuGet package. The Tizen.Wearable.CircularUI provides the Xamarin.Forms extension controls, which are used for wearables.

<PackageReference Include="Tizen.Wearable.CircularUI" Version="1.1.0" />

NuGet

APIs

The Tizen.NET NuGet package contains the Tizen.Sensor namespace, which provides the sensor API.

In the following table, find the API class that matches the sensor you want to use:

Sensor name Class name
Accelerometer Accelerometer
Barometer PressureSensor
Gyro Gyroscope
HR HeartRateMonitor
Light LightSensor

Usage

  1. Declare namespace.
using Tizen.Sensor;
  1. Create instance.
new Accelerometer();
  1. Use sensor.
Accelerometer.IsSupported; //properties
Accelerometer.Count;
Accelerometer.Start(); //method
Accelerometer.Stop();
Accelerometer.DataUpdated += (s, e) => { }; //event

Example

The following example shows how to use the accelerometer on the application:

using Tizen.Sensor;
using Tizen.Wearable.CircularUI.Forms;

public class AccelerometerPage : CirclePage
{
	public Accelerometer Accelerometer { get; private set; }

	public AccelerometerPage()
	{
		if (Accelerometer.IsSupported && Accelerometer.Count)
		{
			Accelerometer = new Accelerometer();
			Accelerometer.DataUpdated += (s, e) =>
			{
				// use event argument
			};
		}
	}

	protected override void OnAppearing()
	{
		base.OnAppearing();
		Accelerometer?.Start();
	}

	protected override void OnDisappearing()
	{
		base.OnDisappearing();
		Accelerometer?.Stop();
	}
}

Checking permission for HR sensor

For HR sensors, the developer needs to declare privilege and check permission. Refer to Privacy-related Permissions and the following sample code.

  • Declaring privilege: http://tizen.org/privilege/healthinfo

manifest

  • Checking permission in your app code.
using Tizen.Security;
using Tizen.Sensor;
using Tizen.Wearable.CircularUI.Forms;

public partial class HRMPage : CirclePage
{
	private const string hrmPrivilege = "http://tizen.org/privilege/healthinfo";

	public HRMPage()
	{
		CheckResult result = PrivacyPrivilegeManager.CheckPermission(hrmPrivilege);
		switch (result)
		{
			case CheckResult.Allow:
				CreateHRM();
				break;

			case CheckResult.Deny:
				break;

			case CheckResult.Ask:
				PrivacyPrivilegeManager.RequestPermission(hrmPrivilege);
				break;
		}
	}
}

Demo

accelerometer
gyro
hr
light
barometer

We provide several sample applications for wearables here, including the Sensor sample used in this post.

If you have any questions about how to configure and use sensors on Galaxy Watch apps, use Issues to contact us.