Track Deadlift Exercise on Galaxy Watch


Objective

Create a native app for Galaxy Watch4, operating on Wear OS powered by Samsung, using Health Services to track deadlift exercise. This app measures repetition count, calories burned, and time spent during the exercise.

Overview

Health Platform provides a simple and unified way for accessing a wide range of health and wellness related data. With Health Services of Health Platform, you will no longer need to develop your own algorithms processing sensors data in order to compute metrics like heart rate, steps counts, distance, calories burned, and other more. These are now accessible through Health Services embedded on wearables operating on Wear OS powered by Samsung.

For detailed information about Health Services, see:

https://developer.android.com/training/wearables/health-services

Set up your environment

You will need the following:

  • Galaxy Watch4 or Watch4 Classic
  • Android Studio (latest version recommended)
  • Java SE Development Kit (JDK) 11 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!

Health Track Deadlift Sample Code
(123.24 KB)

Turn on Developer Mode and adjust its settings

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

  2. Upon successful activation of Developer mode, a toast message is displayed 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

    • Debug over Wi-Fi

    • Turn off automatic Wi-Fi

Connect your watch to Wi-Fi

  1. Go to Settings > Connection > Wi-Fi and make sure that Wi-Fi is enabled.
  2. From the list of available Wi-Fi networks, choose and connect to the same one as your PC.
  3. When successfully connected, tap a Wi-Fi network name, swipe down and note the IP address. You will need this to connect your watch over ADB from your PC.

Connect your Galaxy Watch4 to Android Studio

In Android Studio, go to Terminal and type:


adb connect <IP address as mentioned in previous step>

When prompted, tap Always allow from this computer to allow debugging.

Upon successful connection, you will see the following message in Android Studio’s Terminal:


connected to <IP address of your watch>

Now, you will be able to run the app directly on your watch.

Start your project

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

Locate the downloaded Android Project (deadlift) from the directory and click OK.

Check dependency and app manifest

In the dependencies section of deadlift/app/build.gradle file, see the appropriate dependency for Health Services.


dependencies {
	implementation 'androidx.health:health-services-client:1.0.0-alpha02'
	// ...
}

In AndroidManifest.xml file, note the following:

  • <queries> element

<queries>
    <package android:name="com.google.android.wearable.healthservices" />
</queries>

  • section with requests for necessary permissions

<uses-permission android:name="android.permission.BODY_SENSORS" />
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />

Check capabilities

To check what can be measured during exercise, you need to check its capabilities. Follow these steps to obtain them:

Open Deadlift.java and navigate to checkCapabilities function. Prepare a ListenableFuture<ExerciseCapabilities> instance that will query for deadlift capabilities.

  • getCapabilities() checks exercise capabilities in exerciseClient object.

public void checkCapabilities() {
    ListenableFuture<ExerciseCapabilities> capabilitiesListenableFuture = null;
    /******************************************************************************************
     * [Practice 1] Create ListenableFuture object that will get a callback with Exercise
     * Capabilities. Choose correct function from ExerciseClient
     *--------------------------------------------------------------------------------------
     * Uncomment line below and fill TODO 1
     *  (1) For checking capabilities use getCapabilities()
     ****************************************************************************************/
    //capabilitiesListenableFuture = exerciseClient.TODO 1;
    Futures.addCallback(capabilitiesListenableFuture,
            new FutureCallback<ExerciseCapabilities>() {
                @Override
                public void onSuccess(@Nullable ExerciseCapabilities result) {
                    Log.i(TAG,"Got exercise capabilities");
                    try {
                        exerciseCapabilitiesSet = deadliftReader.getExerciseCapabilities(result);
		    }	
		    

Next, go to DeadliftReader.java and navigate to getExerciseCapabilities function.

  • getSupportedDataTypes() gets measurable data types in exerciseTypeCapabilities.

public Set<DataType> getExerciseCapabilities(ExerciseCapabilities result) throws DeadliftException
{
    if(result == null)
        throw new DeadliftException("ExerciseCapabilities is null");
    ExerciseTypeCapabilities exerciseTypeCapabilities =
            result.getExerciseTypeCapabilities(exerciseType);
    Set<DataType> dataTypeSet = null;
    /****************************************************************************************
     *  [Practice 1] Read set of deadlift capabilities. Choose correct function from Exercise
     *  Type Capabilities.
     * --------------------------------------------------------------------------------------
     *  Uncomment line below and fill TODO 2
     *      - For checking Deadlift capabilities use getSupportedDataTypes()
     */
    //dataTypeSet = exerciseTypeCapabilities.TODO 2;
    return dataTypeSet;
}

Register an event for deadlift state update

In Deadlift.java, navigate to startExerciseUpdateListener() function.

  • To get exercise updates, you need to set a listener using setUpdateListener(exerciseUpdateLisener).

public void startExerciseUpdateListener() throws DeadliftException
{
    if(exerciseClient == null)
        throw new DeadliftException("Exercise Client is null");
    /****************************************************************************************
     *  [Practice 2] Create ListenableFuture object that will get a callback with Exercise
     *  Capabilities. Choose correct function from ExerciseClient
     *--------------------------------------------------------------------------------------
     *  Uncomment line below and fill TODO 3
     *  For setting update listener use setUpdateListener(exerciseUpdateListener)
     */
    ListenableFuture<Void> updateListenableFuture = null;
    //updateListenableFuture = exerciseClient.TODO 3;
    if(updateListenableFuture == null)
        throw new DeadliftException("Update is null");
    Futures.addCallback(updateListenableFuture,
            new FutureCallback<Void>() {
                @Override
                public void onSuccess(@NullableDecl Void result) {
                    Log.i(TAG, "Successfully set update listener");
                    isMeasurementRunning = true;
                    butStart.setText("Stop");
                }
		
		

Check the updated event

To work with the data provided by an exercise, you need to get the latest reading metrics. In Deadlift.java, navigate to updateRepCount() function.

  • getLatestMetrics() gets the latest readings from an Exercise update.

public boolean updateRepCount(ExerciseUpdate update) throws DeadliftException
{
    /***************************************************************************************
     *  [Practice 3] Create map object that will get readings from the update
     *  Choose correct function from ExerciseUpdate class
     * -------------------------------------------------------------------------------------
     *  Uncomment line below and fill TODO 4
     *  For reading latest values from update use getLatestMetrics()
     */
    Map<DataType, List<DataPoint>> map = null;
    if(update == null)
        throw new DeadliftException("Exercise update is null");
    //map = update.TODO 4;
    if(map.isEmpty())
        return false;
	

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. See instruction below on how to run unit tests:

  1. Right click on com.samsung.sdc21.deadlift (test) > DeadliftUnitTest and execute Run 'DeadliftUnitTest' command.

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

Run the app on Galaxy Watch4

After building the APK, you can run the application on a connected device to measure actual deadlift parameters.

  1. Right after the app is started, it will request for the user permission. Allow the app to receive data of the activity.

  1. Afterwards, the application main screen will be shown. Before doing deadlifts, press the START button to track your exercise. When done, tap on the STOP button.

You're done!

Congratulations! You have successfully achieved the goal of this Code Lab. Now, you can create a deadlift exercise tracker app by yourself! If you're having trouble, you may download this file:

Health Track Deadlift Complete Code
(123.22 KB)

Learn more by going to: www.developer.samsung.com/health