Measure Blood Oxygen Level on Galaxy Watch


Objective

Create a health app for Galaxy Watch, operating on Wear OS powered by Samsung, utilizing Samsung Health Sensor SDK to trigger and obtain blood oxygen level (SpO2) measurement results.

Overview

Samsung Health Sensor SDK provides means of accessing and tracking health information contained in the health data storage. Its tracking service gives raw and processed sensor data such as accelerometer and body composition data sent by the Samsung BioActive sensor. The latest BioActive sensor of Galaxy Watch runs powerful health sensors such as photoplethysmogram (PPG), electrocardiogram (ECG), bioelectrical impedance analysis (BIA), sweat loss, and SpO2.

See Samsung Health Sensor SDK 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!

Measuring Blood Oxygen Level Sample Code
(146.3 KB)

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.

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 Android Studio’s 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

  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.

Start your project

In Android Studio, click Open to open existing project.

Locate the downloaded Android project from the directory and click OK.

Check capabilities

For the device to track data with the Samsung Health Sensor SDK, it must support a given tracker type – blood oxygen level. To check this, get the list of available tracker types and verify that the tracker is on the list.

In the ConnectionManager.java file, navigate to the isSpO2Available() function, use a provided HealthTrackingService object to create a HealthTrackerCapability instance, send it to the checkAvailableTrackers function, and assign its result to the availableTrackers list.

  • getTrackingCapability() returns a HealthTrackerCapability instance in the HealthTrackingService object
HealthTrackingService
HealthTrackingService initiates a connection to Samsung's health tracking service and provides a HealthTracker instance to track a HealthTrackerType.
public HealthTrackerCapability getTrackingCapability()
Provide a HealthTrackerCapability instance to get a supporting health tracker type list.

/******************************************************************************************
 * [Practice 1] Check capabilities to confirm SpO2 availability
 *
 * ----------------------------------------------------------------------------------------
 *
 * (Hint) Replace TODO 1 with java code
 * Get HealthTrackerCapability object from HealthTrackingService
 * Send the object to checkAvailableTrackers()
 ******************************************************************************************/
public boolean isSpO2Available(HealthTrackingService healthTrackingService) {
    if (healthTrackingService == null)
        return false;
    List<HealthTrackerType> availableTrackers = null;

    //"TODO 1"

    if (availableTrackers == null)
        return false;
    else
        return availableTrackers.contains(HealthTrackerType.SPO2_ON_DEMAND);
}

Check connection error resolution

Using Samsung Health Sensor SDK API, resolve any error when connecting to Health Tracking Service.

In the ConnectionManager.java file, navigate to the processTrackerException() function, and check if the provided HealthTrackerException object has a resolution. Assign the result to hasResolution variable.

  • hasResolution() function in the HealthTrackerException object checks if the API can fix the error
HealthTrackerException
HealthTrackerException contains error codes and checks the error's resolution. If there is a resolution, solving the error is available by calling resolve(Activity).
boolean hasResolution()
Checks whether the given error has a resolution.
/*******************************************************************************************
* [Practice 2] Resolve HealthTrackerException error
*
* -----------------------------------------------------------------------------------------
*
* (Hint) Replace TODO 2 with java code
* Call hasResolution() on HealthTrackerException object
******************************************************************************************/
public void processTrackerException(HealthTrackerException e) {
boolean hasResolution = false;

//"TODO 2"

if (hasResolution)
    e.resolve(callingActivity);
if (e.getErrorCode() == HealthTrackerException.OLD_PLATFORM_VERSION || e.getErrorCode() == HealthTrackerException.PACKAGE_NOT_INSTALLED)
    ObserverUpdater.getObserverUpdater().notifyConnectionObservers(R.string.NoValidHealthPlatform);
else
    ObserverUpdater.getObserverUpdater().notifyConnectionObservers(R.string.ConnectionError);
Log.e(TAG, "Could not connect to Health Tracking Service: " + e.getMessage());
}
  

Initialize SpO2 tracker

Before the measurement starts, initialize the SpO2 tracker by obtaining the proper health tracker object.

In the SpO2Listener.java file, navigate to the init() function. Using the provided HealthTrackingService object, create an instance of the SpO2 tracker and assign it to the spo2Tracker object.

  • getHealthTracker() with HealthTrackerType.SPO2_ON_DEMAND as an argument will create a HealthTracker instance
HealthTrackingService
HealthTrackingService initiates a connection to Samsung's health tracking service and provides a HealthTracker instance to track a HealthTrackerType.
HealthTracker getHealthTracker(HealthTrackerType healthTrackerType)
Provides a HealthTracker instance for the given healthTrackerType.

/*******************************************************************************************
* [Practice 3] Initialize SpO2 tracker
*
* ----------------------------------------------------------------------------------------
*
* (Hint) Replace TODO 3 with java code
* Initialize spo2Tracker with proper Samsung Health Sensor SDK functionality
* Call getHealthTracker() on HealthTrackingService object
* Use HealthTrackerType.SPO2_ON_DEMAND as an argument
******************************************************************************************/
void init(HealthTrackingService healthTrackingService) {
//"TODO 3"
}


Perform measurement

For the client app to start obtaining the data through the SDK, it has to set a listener method on the HealthTracker. The application setups the listener when the user taps on the Measure button. Each time there is new data, the listener callback receives it. After the measurement is completed, the listener has to be disconnected.

Due to battery drain, on-demand measurement should not last more than 30 seconds. The measurement is cancelled if the final value is not delivered in time. Note that the sensor needs a few seconds to warm up and provide correct values, which adds to the overall measurement time.

The blood oxygen level values come in the onDataReceived callback of TrackerEventListener. In SpO2Listener.java file, you can see the code for reading the value:

private final HealthTracker.TrackerEventListener spo2Listener = new HealthTracker.TrackerEventListener() {
    @Override
    public void onDataReceived(@NonNull List<DataPoint> list) {
        for (DataPoint data : list) {
            updateSpo2(data);
        }
    }
};

private void updateSpo2(DataPoint data) {
    int status = data.getValue(ValueKey.SpO2Set.STATUS);
    int spo2Value = 0;
    if (status == MEASUREMENT_COMPLETED)
        spo2Value = data.getValue(ValueKey.SpO2Set.SPO2);
    ObserverUpdater.getObserverUpdater().notifyTrackerObservers(status, spo2Value);
}

Run unit tests

For your convenience, you can find an additional Unit Tests package. This lets you verify your code changes even without using a physical watch. See instructions below on how to run unit tests:

  1. Right click on com.samsung.health.spo2tracking (test) and execute Run 'Tests in 'com.samsung.health.spo2tracking'' command.

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

Run the app

After building the APK, you can run the application on a connected device to measure blood oxygen level.

  1. Right after the app is started, it requests for user permission. Allow the app to receive data from the body sensors.

  1. Afterwards, it shows the application's main screen. To get the blood oxygen level, tap on the Measure button. To stop the measurement, tap on the Stop button.

You're done!

Congratulations! You have successfully achieved the goal of this Code Lab. Now, you can create a health app that measures blood oxygen level by yourself! If you're having trouble, you may download this file:

Measuring Blood Oxygen Level Complete Code
(146.2 KB)

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