Transfer heart rate data from Galaxy Watch to a mobile device


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.

Overview

With this Code Lab, you can measure various health data using Samsung Health Sensor SDK and send it to a paired Android mobile device for further processing.

Samsung Health Sensor SDK tracks various health data, but it cannot save or send collected results. Meanwhile, Wearable Data Layer allows you to synchronize data from your Galaxy Watch to an Android mobile device. Using a paired mobile device allows the data to be more organized by taking advantage of a bigger screen and better performance.

See Samsung Health Sensor SDK descriptions for detailed information.

Set up your environment

You will need the following:

  • Galaxy Watch4 or newer
  • 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
(213.7 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

To use the app, you need to enable Developer Mode in the Health Platform.

  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

In Android Studio, click Open to open an existing project.

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

You should see both devices and applications available in Android Studio as in the screenshots below:

Initiate heart rate tracking

  1. First, you need to connect to the HealthTrackingService. To do that:
    1. Create connectionListener,
    2. Create HealthTrackingService object by invoking HealthTrackingService(connectionListener, context)
    3. Invoke HealthTrackingService.connectService()
  2. When connected to the Health Tracking Service, check the tracking capability. The available trackers may vary depending on Samsung Health Sensor SDK, Health Platform versions or watch hardware version. Use the getTrackingCapability() function of the HealthTrackingService object.
  3. Obtain heart rate tracker object using the function HealthTrackingService.getHealthTracker(HealthTrackerType.HEART_RATE_CONTINUOUS)
  4. Define event listener: HealthTracker.TrackerEventListener, where the heart rate values will be collected.
  5. Start tracking. The tracker starts collecting heart rate data when HealthTracker.setEventListener(updateListener) is invoked, using the event listener.

Collect heart data from the watch

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 wear > java > data > com.samsung.health.hrdatatransfer > data.

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 up the Wearable Data Layer so it can send data to a paired Android mobile device. Wearable Data Layer API provides data synchronization between Wear OS and Android devices.

To determine if a remote mobile device is available, the Wearable Data Layer API uses concept of capabilities (not to be confused with Samsung Health Sensor SDK’s tracking capabilities, providing information about available tracker types). Using the Wearable Data Layer's CapabilityClient, you can get information about Nodes (remote devices) being able to consume messages from the watch.

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

In CapabilityRepositoryImpl.kt, and fill in the function below. The purpose of this part is to filter all capabilities (represented by allCapabilities argument) by capability argument and return the set of nodes (Set<Node>) having this capability. Later on, we need those nodes to send the message to them.

/**************************************************************************************
 * [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
}

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. For sending data to the paired mobile device we are using Wearable Data Layer API’s MessageClient object and its function sendMessage (String nodeId, String path, byte[] message).

Go to 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.

  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 Code
(213.9 KB)

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