Transfer heart rate data from Galaxy Watch to a mobile device with Samsung Privileged Health SDK


Objective

Create a health app for Galaxy Watch, operating on Wear OS powered by Samsung, to measure Heart Rate and Inter-Beat Interval (IBI), send data to a paired Android phone, and create an Android application for receiving data sent from a paired Galaxy Watch.

Partnership Request

In this Code Lab, you will use a specially prepared mock library. It has limited functionality and uses dummy data instead of real-time data. You will need the full version of the Samsung Privileged Health SDK library to get real values, which is available to Samsung partners. Apply as a partner by checking out the Partner App Program for exclusive Samsung Privileged Health SDK access.

Overview

With this Code Lab, you can measure various health data using Samsung Privileged Health SDK and send it to a paired Android mobile device for further processing. Using a paired mobile device will make the data more organized by taking advantage of a bigger screen and saving health data to a local database or remote server.

See Samsung Privileged Health SDK descriptions for detailed information.

Set up your environment

You will need the following:

  • Galaxy Watch4 or newer, powered by Wear OS
  • Android mobile device
  • Android Studio (latest version recommended)
  • Java SE Development Kit (JDK) 17 or later

Sample Code

Here is a sample code for you to start coding in this Code Lab. Download it and start your learning experience!

Heart Rate Data Transfer Sample Code
(196.70 KB)

Connect your Galaxy Watch to Wi-Fi

  1. Go to Settings > Connection > Wi-Fi and make sure that the Wi-Fi is enabled.

  2. From the list of available Wi-Fi networks, choose and connect to the same one as your PC.

Turn on Developer Mode and adjust its settings

  1. On your watch, go to Settings > About watch > Software and tap on Software version 5 times.

  2. Upon successful activation of Developer mode, a toast message will display as on the image below.

  3. Afterwards, Developer options will be visible under Settings.

  4. Tap Developer options and enable the following options:

    • ADB debugging

    • In Developer options find Wireless debugging

    • Turn on Wireless debugging

    • Check Always allow on this network and tap Allow

    • Go back to Developer options and click Turn off automatic Wi-Fi

Connect your Galaxy Watch to Android Studio

  1. Go to Settings > Developer options > Wireless debugging and choose Pair new device.

  2. Take note of the Wi-Fi pairing code, IP address & Port.

  3. In Android Studio, go to Terminal and type:

    adb pair <IP address>:<port> <Wi-Fi pairing code>
    
  4. When prompted, tap Always allow from this computer to allow debugging.

  5. After successfully pairing, type:

    adb connect <IP address of your watch>:<port>
    

    Upon successful connection, you will see the following message in the terminal:

    connected to <IP address of your watch>
    

    Now, you can run the app directly on your watch.

Turn on Developer Mode for Health Platform

This step is only applicable to registered partners of Samsung Privileged Health SDK. You can replace priv-health-tracking-mock-2023.aar in app > libs with the real library to receive real sensor data with the application. This requires Health Platform to be set in developer mode:

  1. On your watch go to Settings > Apps > Health Platform.

  2. Quickly tap Health Platform title for 10 times. This enables developer mode and displays [Dev mode] below the title.

  3. To stop using developer mode, quickly tap Health Platform title for 10 times to disable it.

Set up your Android device

Click on the following links to setup your Android device:

  1. Enable developer options
  2. Run apps on a hardware device
  3. Connect the Galaxy Watch with you Samsung mobile phone

Start your project

After downloading the sample code containing the project files, in Android Studio click Open to open an existing project.

Locate the downloaded Android project (HRDataTransfer-code-lab) from the directory and click OK.

Parse data from the watch

After opening the sample project, you need to parse the IBI data collected while tracking the heart rate-related data.

The updateListener collects DataPoint instances from the watch, which contains a collection of ValueKey objects. Those objects contain heart rate, IBI values, and IBI statuses. There's always one value for heart rate while the number of IBI values vary from 0-4. Both IBI value and IBI status lists have the same size.

Go to the wear > java > data > com.samsung.health.hrdatatransfer.

Under IBIDataParsing.kt, provide the implementation for the function below:

/*******************************************************************************
 * [Practice 1] Get list of valid Inter-beat interval values from a DataPoint
 *  - return ArrayList<Int> of valid IBI values (validIbiList)
 *  - if no IBI value is valid, return an empty ArrayList
 *
 *  var ibiValues is a list representing ibiValues (up to 4)
 *  var ibiStatuses is a list of their statuses (has the same size as ibiValues)
 -------------------------------------------------------------------------------
 *  - (Hints)
 * Use local function: isIBIValid(status, value) to check validity of IBI
 * ****************************************************************************/

fun getValidIbiList(dataPoint: DataPoint): ArrayList<Int> {

    val ibiValues = dataPoint.getValue(ValueKey.HeartRateSet.IBI_LIST)
    val ibiStatuses = dataPoint.getValue(ValueKey.HeartRateSet.IBI_STATUS_LIST)
    val validIbiList = ArrayList<Int>()

    //TODO 1

    return validIbiList
}

Check data sending capabilities for the watch

Once the heart rate tracker can collect data, set it up so it can send data to a paired Android mobile device.

First, check if the devices within range have proper capabilities. The capability data type is a string and is used when sending a message on both devices.

Go to the wear > java > com.samsung.health.hrdatatransfer > data.

In CapabilityRepositoryImpl.kt, and fill in the function below:

/**************************************************************************************
 * [Practice 2] Check capabilities for reachable remote nodes (devices)
 *  - return a Set of Node objects out of all capabilities represented by 2nd function
 *    argument, having the capability represented by 1st function argument.
 *  - return empty Set if no node has the capability
 --------------------------------------------------------------------------------------
 *  - (Hints)
 *  You might want to use filterValues function on the given allCapabilities Map
 * ***********************************************************************************/

override suspend fun getNodesForCapability(
    capability: String,
    allCapabilities: Map<Node, Set<String>>
): Set<Node> {
    //TODO 2
}

The purpose of this exercise is to identify which nodes have the exact capability you are looking for (represented by capability argument). You need those nodes later to send the message to them.

Encode message for the watch

Before sending the results of the heart rate and IBI to the paired mobile device, you need to encode the message into a string.

Go to the wear > java > com.samsung.health.hrdatatransfer > domain.

In SendMessageUseCase.kt, fill in the function below and use JSON format to encode the list of results (ArrayList<TrackedData>) into a string:

/***********************************************************************
 * [Practice 3] - Encode Heart Rate & Inter-beat interval into String
 *  - encode function argument (trackedData) into Json format.
 *  - Return the encoded string
 -----------------------------------------------------------------------
 *  - (Hint)
 *  Use Json.encodeToString function
 **********************************************************************/

fun encodeMessage(trackedData: ArrayList<TrackedData>): String {

    //TODO 3
}

Run unit tests

For your convenience, you will find an additional Unit Tests package. This will let you verify your code changes even without using a physical watch or mobile device. See the instruction below on how to run unit tests:

  1. Right click on com.samsung.health.hrdatatransfer (test), and execute Run 'Tests in 'com.samsung.health.hrdatatransfer" command. Do the same step with the mobile module.

  1. If you have completed all the tasks correctly, you will see all the unit tests pass successfully.

Run the app

After building the APKs, you can run the applications on your watch to measure heart rate and IBI values, and on your mobile device to collect the data from your watch.

  1. Once the app starts, allow the app to receive data from the body sensors.
  2. Afterwards, it shows the application's main screen. To get the heart rate and IBI values, tap the Start button.
  3. Tap the Send button to send the data to your mobile device.

You’re done!

Congratulations! You have successfully achieved the goal of this Code Lab. Now, you can create a health app on a watch to measure heart rate and IBI, and develop a mobile app that receives that health data! If you face any trouble, you may download this file:

Heart Rate Data Transfer Complete Project
(195.74 KB)

To learn more about Samsung Health, visit:
developer.samsung.com/health