Getting Started

To track Galaxy Watch's health sensor data with the Samsung Health Sensor SDK, do the following.

Import Library to Project

To use the library, you have to add it to the project. You can do that by adding implementation in a build.gradle file:

implementation files('path/to/library/samsung-health-sensor-api.aar')

Request Permission

The following permissions are related to the SDK:

You can check required permissions for each HealthTrackerType. In order to perform measurements, application requires the Sensors permission. Make sure to include it in your application's AndroidManifest.xml:

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

And request the permission in your application:

if (ActivityCompat.checkSelfPermission(getApplicationContext(), "android.permission.ACTIVITY_RECOGNITION")
        == PackageManager.PERMISSION_DENIED) {
    requestPermissions(this, new String[]{Manifest.permission.ACTIVITY_RECOGNITION, 0);
}

Connect to Health Platform

The Health Platform provides the health data tracking service. To track sensor data, your watch application needs to connect with the Health Platform.
Include the Samsung Health Sensor SDK library in your application project and prepare a connection listener:

private final ConnectionListener connectionListener = new ConnectionListener() {
    @Override
    public void onConnectionSuccess() {
       //Process connection activities here
    }

    @Override
    public void onConnectionEnded() {
       //Process disconnection activities here
    }

    @Override
    public void onConnectionFailed(HealthTrackerException e) {
	if (e.getErrorCode() == HealthTrackerException.OLD_PLATFORM_VERSION
        || e.getErrorCode() == HealthTrackerException.PACKAGE_NOT_INSTALLED)
    	Toast.makeText(getApplicationContext(), 
    			"Health Platform version is outdated or not installed",
            	Toast.LENGTH_LONG)
            	.show();
  		if (e.hasResolution()) {
    			e.resolve(MainActivity.this);
 		}
    }
};

It's worth noting that onConnectionFailed() provides HealthTrackerException that may be able to fix a connection problem by itself.
If the installed Health Platform version is outdated or is not installed on a watch, calling resolve() is helpful, as it will redirect the user to the Health Platform download or update page. Before jumping to the page, we recommend displaying a guide message with the error information and what will happen.

Connect to the Health Platform:

HealthTrackingService healthTrackingService = 
	new HealthTrackingService(connectionListener, getApplicationContext());
healthTrackingService.connectService();

Check Capabilities

Before tracking sensor data with the SDK, it's good to know if the device you are using can perform the measurement you want to make. You can check its capabilities with:

  • HealthTrackerCapability.getSupportHealthTrackerTypes()
List<HealthTrackerType> availableTrackers = 
healthTrackingService.getTrackingCapability().getSupportHealthTrackerTypes();

The list returned by the function includes all the available health tracker types from the watch. If the list does not contain a desired health tracker type, you can inform the user that it is not possible to perform the desired measurement on the watch.
For example, a capability check for HealthTrackerType.ACCELEROMETER_CONTINUOUS can be the following:

if (!availableTrackers.contains(HealthTrackerType.ACCELEROMETER_CONTINUOUS)) {
    Toast.makeText(getApplicationContext(),
    "Accelerometer Tracking not supported on device", 
    Toast.LENGTH_LONG)
    .show();
    Log.e(APP_TAG, "This watch does not support accelerometer tracking.");
}

Getting Health Tracker Type

As HealthTrackerType.ACCELEROMETER_CONTINUOUS is available on the watch, create a HealthTracker instance:

private HealthTracker tracker = 
healthTrackingService.getHealthTracker(HealthTrackerType.ACCELEROMETER_CONTINUOUS);

Set a Tracker Event Listener

To receive sensor data, set up a tracker event listener. An example of generic listener:

HealthTracker.TrackerEventListener listener = new HealthTracker.TrackerEventListener() {
    @Override
    public void onDataReceived(@NonNull List<DataPoint> list) {
		//Process your data
    }
    @Override
    public void onFlushCompleted() {
 		//Process flush completion
    }
    @Override
    public void onError(HealthTracker.TrackerError trackerError) {
        Log.i(APP_TAG, " onError called");
        if (trackerError == HealthTracker.TrackerError.PERMISSION_ERROR) {
            runOnUiThread(() -> Toast.makeText(getApplicationContext(),
                    "Permissions Check Failed", Toast.LENGTH_SHORT).show());
        }
        if (trackerError == HealthTracker.TrackerError.SDK_POLICY_ERROR) {
            runOnUiThread(() -> Toast.makeText(getApplicationContext(),
                    "SDK Policy denied", Toast.LENGTH_SHORT).show());
        }
    }
};

Sensor data is received in onDataReceived() and its DataPoint has ValueKey objects. The value key can be one of the following sets depending on the health tracker type:

  • AccelerometerSet
  • BiaSet
  • EcgSet
  • HeartRateSet
  • PpgSet
  • SkinTemperatureSet
  • SpO2Set
  • SweatLossSet

Error handling is available in the onError() event of the listener. If SDK_POLICY_ERROR is received, and if you are testing the application without the application's release key, enable the Health Platform's developer mode.

Once we have a listener ready, tracking sensor data is available by setting an event listener.
An example of a tracker that is going to read accelerometer data:

private HealthTracker tracker = 
healthTrackingService.getHealthTracker(HealthTrackerType.ACCELEROMETER_CONTINUOUS);
tracker.setEventListener(accelerometerListener);

If you would like to get another kind of sensor data, create a new HealthTracker instance and add a separate listener to it.

Flushing Data

If the watch display is turned off, the SDK starts collecting data in batches. We can use the flush function to force the data to be sent immediately.

tracker.flush();

The flushed data is received by the listener's onDataReceived().
The flush also resets the counter for sampling before data is automatically pushed. For example, if data is normally sent every 300 samples and flush is used after collecting 200 samples, next push happens 300 samples after calling the flush. This functionality is best used with batching data types that collect many samples before pushing them.

Finishing Data Tracking

Unsetting listeners from trackers, as well as disconnecting from the SDK, is mandatory. It is important to stop unnecessary sensor measurements to prevent battery drain. After we are done reading data, we need to unset the listener:
tracker.unsetEventListener();

After your work with the API is done, you need to disconnect from the Health Platform:

if (healthTrackingService != null)
    healthTrackingService.disconnectService();

That's everything you need to know before starting to develop you application and making use of the Samsung Health Sensor SDK. In case you want to further develop your knowledge about SDK, we encourage you to get familiar with our sample applications.