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
-
Go to Settings > Connection > Wi-Fi and make sure that the Wi-Fi is enabled.
-
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
-
On your watch, go to Settings > About watch > Software and tap on Software version 5 times.
-
Upon successful activation of Developer mode, a toast message will display as on the image below.
-
Afterwards, Developer options will be visible under Settings.
-
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
NoteThere may be differences in settings depending on your One UI version.
Connect your Galaxy Watch to Android Studio
-
Go to Settings > Developer options > Wireless debugging and choose Pair new device.
-
Take note of the Wi-Fi pairing code, IP address & Port.
-
In Android Studio, go to Terminal and type:
adb pair <IP address>:<port> <Wi-Fi pairing code>
-
When prompted, tap Always allow from this computer to allow debugging.
-
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.
-
On your watch go to Settings > Apps > Health Platform.
-
Quickly tap Health Platform title for 10 times. This enables developer mode and displays [Dev mode] below the title.
-
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:
- Enable developer options
- Run apps on a hardware device
- 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
NoteYou may refer to this blog post for more detailed analysis of the heart rate tracking using Samsung Health Sensor SDK.
- First, you need to connect to the
HealthTrackingService
. To do that:
- Create
connectionListener
,
- Create
HealthTrackingService
object by invoking HealthTrackingService(connectionListener, context)
- Invoke
HealthTrackingService.connectService()
- 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.
- Obtain heart rate tracker object using the function
HealthTrackingService.getHealthTracker(HealthTrackerType.HEART_RATE_CONTINUOUS)
- Define event listener:
HealthTracker.TrackerEventListener
, where the heart rate values will be collected.
- 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.
NoteTo know more about Wearable Data Layer API, go here.
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
}
NoteTrackedData
is an object, containing data received from Heart Rate tracker’s single DataPoint
object:
@Serializable
data class TrackedData(
var hr: Int,
var ibi: ArrayList<Int> = ArrayList()
)
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:
- Right click on com.samsung.health.hrdatatransfer (test), and execute Run 'Tests in 'com.samsung.health.hrdatatransfer" command.
- 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.
- Once the app starts, allow the app to receive data from the body sensors.
- Afterwards, it shows the application's main screen. To get the heart rate and IBI values, tap the Start button.
- Tap the Send button to send the data to your mobile device.
NoteThe watch keeps last ~40 values of heart rate and IBI.
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