Tizen provides APIs to monitor the movement, location, etc. of Galaxy Watch and measure the user's heart rate in real time. Galaxy Watch doesn't support all sensors, but it does support sensors that are useful to the users. Since the availability of the sensor depends on the support of the relevant hardware/software, you need to check the availability of the sensor on a device if you want to access the sensors in your app.
Checking sensor's availability
Not all Galaxy Watch support all sensors, so you need to make sure that the sensors are supported in the current device before using it in your application.
First add the reference to Tizen.Sensors:
using Tizen.Sensor;
There are two ways to check sensor availability:
use the feature key
bool pedometerSupported;
const string pedometerFeature = "http://tizen.org/feature/sensor.pedometer";
if (!Tizen.System.Information.TryGetValue<bool>(pedometerFeature, out pedometerSupported))
{
// Check if the name of the specified feature key is right
}
if (pedometerSupported)
{
// You can use the pedometer sensor data
}
use sensor's IsSupported property
if (Pedometer.IsSupported)
{
// You can use the pedometer sensor data
}
Declaring device privilege and requesting user permission
Tizen protects user information such as contacts, calendar, and user's app usage history and certain device resources such as camera, microphone, location, sensor, and media storage from being used without your consent. In order for an app to access sensitive information or resources related to the user, you must declare device privileges in the app manifest file and then request user's permission at runtime for privacy privileges, which are related to sensitive user data.
Declare device privilege
MySteps app requires user's step count provided by the Pedometer class. To use the APIs related to the user's health information such as HeartRateMonitor, Pedometer, and SleepMonitor, healthinfo privilege must be declared in the tizen-manifest.xml file of the app. Take the following steps to declare healthinfo privilege:
Select the tizen-manifest.xml in the Solution Explorer of Visual Studio.
Click Privileges in the left sidebar and Click Add button.
In the list of Platform defined privileges, find http://tizen.org/privilege/healthinfo or type healthinfo in the search bar, select the privilege and press OK button.
You can ask the user's permission as shown in the following example:
...
public partial class App : Application
{
....
protected override void OnStart()
{
// The app has to request the permission to access health data
RequestPermissionHealthInfoAsync();
}
private async void RequestPermissionHealthInfoAsync()
{
var result = await RequestAsync("http://tizen.org/privilege/healthinfo");
if (!result)
{
await MainPage.DisplayAlert("Alert", "This app cannot access your health information. So it will be terminated.", "OK");
Application.Current.Quit();
}
}
/// <summary>
/// Asks the user to grant the permission at runtime
/// </summary>
/// <param name="privilege">The privilege name to check for</param>
/// <returns>bool value that indicates whether the permission has been granted or not</returns>
private async Task<bool> RequestAsync(string privilege)
{
// first make sure the user has given permission to use the privacy-related permissions in the app.
switch (PrivacyPrivilegeManager.CheckPermission(privilege))
{
case CheckResult.Allow:
// already allowed
return true;
case CheckResult.Deny:
case CheckResult.Ask:
var tcs = new TaskCompletionSource<bool>();
var response = PrivacyPrivilegeManager.GetResponseContext(privilege);
PrivacyPrivilegeManager.ResponseContext context = null;
if (response.TryGetTarget(out context))
{
context.ResponseFetched += (s, e) =>
{
bool result = false;
if (e.result == RequestResult.AllowForever)
{
result = true;
}
tcs.SetResult(result);
};
}
// Ask the user to give the app permission to use the user's personal information
PrivacyPrivilegeManager.RequestPermission(privilege);
return await tcs.Task;
default:
return false;
}
}
}
You should ask the user to give your app permission to use the user's personal information by calling PrivacyPrivilegeManager.RequestPermission(privilege). If you rerun the application, a new pop-up screen will appear. Click the check mark on the right.
Tracking the user's steps using pedometer
When the user clicks the Start button in the MainPage, we want to monitor the user's steps with a pedometer sensor in Galaxy Watch. To activate the pedometer sensor, write StepCountPage.xaml.cs as below. The code demonstrates how to get a controller instance of the device's pedometer and activate the sensor:
public partial class StepCountPage : ContentPage
{
Pedometer pedometer;
public StepCountPage()
{
...
InitializeComponent();
pedometer = new Pedometer();
pedometer.Start();
}
}
We want to display the step count data on the Label whenever the step count changes. To do this, we need to access the Label in our code behind. Use the x:Name attribute in XAML to add a unique reference which can be accessed in code behind. Add a x:Name attribute to Label and assign a string "stepCountLabel":
The code-behind file can access the Label object defined in XAML using the stepCountLabel name assigned with the x:Name attribute. Now you can update the text of Label to show the updated step count.
MySteps can subscribe to the events of pedometer's data updates by attaching an event handler(UpdateData()) to the pedometer.DataUpdated event. The following code shows you how to do this:
In order to test the pedometer function on an emulator, you need to trigger the virtual sensor of the emulator:
Right-click anywhere on an emulator and click Control Panel.
Click the Next button at the bottom right of the Tizen Emulator Control Panel window.
Press the Pedometer and select Walk to update the step count for your application.
In the last tutorial of the Get Started series, we will introduce the specialized UIs for Samsung Galaxy Watch.
Manage Your Cookies
We use cookies to improve your experience on our website and to show you relevant
advertising. Manage you settings for our cookies below.
Essential Cookies
These cookies are essential as they enable you to move around the website. This
category cannot be disabled.
Company
Domain
Samsung Electronics
.samsungdeveloperconference.com
Analytical/Performance Cookies
These cookies collect information about how you use our website. for example which
pages you visit most often. All information these cookies collect is used to improve
how the website works.
Company
Domain
LinkedIn
.linkedin.com
Meta (formerly Facebook)
.samsungdeveloperconference.com
Google Inc.
.samsungdeveloperconference.com
Functionality Cookies
These cookies allow our website to remember choices you make (such as your user name, language or the region your are in) and
tailor the website to provide enhanced features and content for you.
Company
Domain
LinkedIn
.ads.linkedin.com, .linkedin.com
Advertising Cookies
These cookies gather information about your browser habits. They remember that
you've visited our website and share this information with other organizations such
as advertisers.
Company
Domain
LinkedIn
.linkedin.com
Meta (formerly Facebook)
.samsungdeveloperconference.com
Google Inc.
.samsungdeveloperconference.com
Preferences Submitted
You have successfully updated your cookie preferences.