Battery consumption is a critical factor when developing applications that use device sensors. To prolong battery life, the Samsung Health Sensor SDK allows you to implement batching trackers, which send multiple data points in a single callback event. Another way you can extend battery life is by stopping sensor tracking when it is not needed, such as when the watch is not being worn.
This blog describes the “HR Tracker” sample application, which implements the batching heart rate tracker in combination with the off-body sensor. The application enables heart rate tracking only when the watch is worn. If the user tries to start tracking without wearing the watch, they are informed to put it on. If the watch is removed during tracking, the tracking is stopped. If you have a Galaxy Watch4 or higher device running Wear OS powered by Samsung, you can download the sample application and test it on your device.
HR Tracker 1.2.0
(139,3KB)Aug 20, 2024
Prerequisites
To implement off-body sensor and heart rate tracking in the application:
In the AndroidManifest.xml file for the application, declare the body sensors permission:
In the HeartRateActivity.java application code file, check whether the user has granted permission to use the body sensors on their Galaxy Watch. If they have not, request it.
if (checkSelfPermission(Manifest.permission.BODY_SENSORS)
== PackageManager.PERMISSION_DENIED) {
requestPermissions(new String[]{Manifest.permission.BODY_SENSORS}, OFFBODY_REQUEST);
}
Implementing on and off-body detection
To detect when the Galaxy Watch is being worn on the wrist:
In the onPause() method, unregister the event listener when the application is in the background:
mSensorManager.unregisterListener(this);
Override the onSensorChanged() method to react to the offBodyData sensor value change when the watch is worn or removed from the wrist:
Because the sensor value is a float, you must convert the value to an integer:
final float offBodyDataFloat = sensorEvent.values[0];
final int offBodyData = Math.round(offBodyDataFloat);
If the value of offBodyData is 1, the watch is being worn. We set the variable, which describes worn status, to true:
deviceWorn.set(true);
If the value of offBodyData is 0, the watch is not being worn. Set the status variable to false and if measurement was being done at that time - end it and notify the user:
Set an event listener to receive and handle the tracked heart rate data:
if (!isHandlerRunning) {
heartRateHandler.post(() -> heartRateTracker.setEventListener(heartRateListener));
isHandlerRunning = true;
}
A listener is an object, which will process incoming data. In this blog we will focus on one of the functions included in it - onDataReceived(). It contains data gathered by the Samsung Health Sensor SDK as list of DataPoint type:
public void onDataReceived(@NonNull List<DataPoint> list) {
for (DataPoint data : list) {
updateHeartRate(data);
}
}
For the heart rate sensor, each tracking result consists of a heart rate reading in beats per minute and its status parameter, and a list of inter-beat intervals and the corresponding status for each value. Because we are interested in the heart rate, extract the beats per minute reading and status:
final int status = data.getValue(ValueKey.HeartRateSet.HEART_RATE_STATUS);
int heartRateValue = data.getValue(ValueKey.HeartRateSet.HEART_RATE);
trackerDataSubject.notifyHeartRateTrackerObservers(status, heartRateValue);
If the heart rate reading was successful, the status value is “1”. To interpret other status values, see the API documentation.
Display the heart rate and status value on the application screen:
If the measurement is stopped by either user input, or watch being removed - stop the heart rate tracker:
if (heartRateListener != null) {
heartRateListener.stopTracker();
}
This demonstration has shown how you can combine multiple strategies to enhance battery life when implementing sensor tracking in your watch applications, such as using batching tracker, and to disable tracking when the user is not wearing the device. We encourage you to try doing it by yourself and explore other available features provided by Samsung Health Sensor SDK.
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.