Track Deadlift Exercise on Galaxy Watch


Objective

Create a native app for Galaxy Watch, 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 Services provides a simple and unified way for accessing a wide range of health and wellness related data. With Health Services API, 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.

See Health Platform descriptions for detailed information.

Set up your environment

You will need the following:

  • Galaxy Watch4 or newer
  • 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
(132.83 KB)

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

    • Debug over Wi-Fi

    • Turn off automatic Wi-Fi

Connect your Galaxy 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 Watch 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 can run the app directly on your watch.

Start your project

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

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

Check dependency and app manifest

In the dependencies section of Gradle Scripts > build.gradle (Module :app) file, see the appropriate dependency for Health Services.


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

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 an exercise, you need to check its capabilities.

Go to app > java > com.samsung.sdc21.deadlift. Open the DeadliftUtil.java file and navigate to the checkCapabilities() method.

An inner class C definition implements the methods of the FutureCallback interface.

Within this definition, define the onSuccess() method to retrieve the exercise type capabilities:

public void onSuccess(ExerciseCapabilities result) {
    Objects.requireNonNull(result);
    Log.i(TAG, "Got exercise capabilities");
    /***********************************************************************************
     * [Practice 1] Define the onSuccess() method
     *
     *  - (Hint) Uncomment lines below and replace TODO 1
     *  Call getExerciseTypeCapabilities() method of result object,
     *  passing already initialized t as an argument:
     **********************************************************************************/
    final ExerciseType t = ExerciseType.DEADLIFT;
    // final ExerciseTypeCapabilities capabilities = "TODO 1"
    // final ExerciseConfig.Builder builder = ExerciseConfig.builder(t);
    // builder.setDataTypes(capabilities.getSupportedDataTypes());
    // exerciseConfigBuilder = builder;
}

Next, implement the findCapabilitesFuture() method to get a callback with ExerciseCapabilities:

  • getCapabilitiesAsync() returns the ExerciseCapabilities of the ExerciseClient for the device.
static ListenableFuture<ExerciseCapabilities> findCapabilitiesFuture(
        ExerciseClient client) {
    /*******************************************************************************************
     * [Practice 1] Create a ListenableFuture object that will get a callback with
     *              with Exercise Capabilities. Choose the correct method from ExerciseClient
     *
     * - (Hint) Uncomment line and replace null with TODO 2
     * For checking capabilities use getCapabilitiesAsync() method.
     ******************************************************************************************/
    return null; //"TODO 2";
}

Start the exercise

Inside the startExercise() method, there is a call to the Futures.addCallback() method. This method adds a callback function that executes when the asynchronous operation of starting the exercise completes.

Set an update callback for the exercise client within the onSuccess() method of the callback function:

public void onSuccess(Void result) {
    Log.i(TAG, "Successfully started");
    /***************************************************************************
     * [Practice 2] Set an update callback
     *
     * - (Hint) Uncomment lines below and fill TODOs:
     * (1) Make appropriate call of setUpdateCallback method
     *     and pass ExerciseUpdateListener object as an argument
     * (2) Change isMeasurementRunning flag value to true
     **************************************************************************/
    
    // exerciseClient.setUpdateCallback("TODO 3 (1)");
    Log.i(TAG, "Successfully set update listener");
    // "TODO 3 (2)"
}

In the Deadlift.java file, call the startExercise() method in onButtonClickHelper():

public void onButtonClickHelper() {
    /*******************************************************************************************
     * [Practice 2] Start the exercise using a method from DeadliftUtil.java
     *
     * - (Hint) Uncomment line below and fill TODO 4:
     * Call startExercise() method on util object:
     * ****************************************************************************************/
    
    // "TODO 4"
}

Get the results

Go to the DeadliftUtil.java file, and in the getNewRepsValue() method, call the getLatestMetrics() method from the ExerciseUpdate class to get the data collected during the exercise.

Store the data in the resultList, where the last element holds the most up-to-date value of all your repetitions.

public long getNewRepsValue(ExerciseUpdate update, DeltaDataType<Long, IntervalDataPoint<Long>> dataType) {
    /*******************************************************************************************
     * [Practice 3] Get the data collected during exercise
     *
     * - (Hint) Uncomment lines below and fill TODO 5
     * Call getLatestMetrics() method of ExerciseUpdate object.
     * Then, get the data of appropriate type.
     * For this, you can use dedicated method getData(), passing dataType as an argument
     * ****************************************************************************************/
    
    // final List<IntervalDataPoint<Long>> resultList = "TODO 5"
    // if (!resultList.isEmpty()) {
    //    final int lastIndex = resultList.size() - 1;
    //    return resultList.get(lastIndex).getValue();
    // }
    return NO_NEW_VALUE;
}

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

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
(132.42 KB)

Learn more by going to Health Platform.