> ## Documentation Index
> Fetch the complete documentation index at: https://docs.spikeapi.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Android SDK for Samsung Health

> Start getting Spike data in 3 steps using Spike SDK for Samsung Health Data on Android platform.

## Requirements and Limitations

<Note>
  Samsung Health Data is available on Android devices only!
</Note>

* Samsung Health Data SDK runs on devices with Android 10 (API level 29) or above. It is available on all Samsung smartphones and non-Samsung Android smartphones.
* Samsung Health Data SDK works with Samsung Health. Samsung Health version 6.30.2 or higher is required.
* Samsung Health Data SDK supports Java 17 or higher version.
* The emulator is not supported.
* Data obtained using Samsung Health Data SDK is for fitness and wellness information only. It is not for the diagnosis or treatment of any medical condition.

## Step 1: Create a Spike Connection

<Warning>If you already set up [Health Connect integration](/sdk-docs/android/usage-guide) in your app, you should [skip this step](#step-2%3A-ask-user-for-permissions) and use the same `SpikeSDK` connection object.</Warning>

To set up the Spike SDK create `SpikeConnectionV3` instance with your Spike application id, application user id and signature unique to each of your application users (more on generating signatures [here](/api-docs/authentication)):

```kotlin theme={null}

val spikeConnection = SpikeConnectionAPIv3.createConnection(
    applicationId = 1000,
    signature = "signature",
    endUserId = "user-id",
    context = context
)
```

## Step 2: Ask User for Permissions

If you want to read data from Samsung Health, you have to ensure the user gives your app permissions.
First, you have to check if Samsung Health is available on users' phone using
[checkSamsungHealthDataAvailability](https://spike_api.gitlab.io/spike-android-sdk/sdk/com.spikeapi.apiv3/-spike-connection-a-p-iv3/check-samsung-health-data-availability.html) method:

```kotlin theme={null}
val availability = spikeConnection.checkSamsungHealthDataAvailability(activity = activity)
```

where

```kotlin theme={null}
public data class SamsungHealthDataAvailability(
    public val status: SamsungHealthDataAvailabilityStatus,
    public val errorCode: Int,
    public val message: String
)

public enum class SamsungHealthDataAvailabilityStatus(public val value: String) {
    /**
     * Samsung Health is not installed. Ask the user to install it.
     */
    NOT_INSTALLED("NOT_INSTALLED"),

    /**
     * The version of Samsung Health is too old. Ask users to update it.
     */
    UPDATE_REQUIRED("UPDATE_REQUIRED"),

    /**
     * The Samsung Health Data is installed but is disabled.
     */
    DISABLED("DISABLED"),

    /**
     * Samsung Health has been installed, but the user didn't perform an initial process, such as
     * agreeing to the Terms and Conditions.
     */
    NOT_INITIALIZED("NOT_INITIALIZED"),

    /**
     * Samsung Health returned the other error.
     */
    ERROR_OTHER("ERROR_OTHER"),

    /**
     * Samsung Health Data is available.
     */
    INSTALLED("INSTALLED"),
}

```

If Samsung Health is installed, you can ask the user for permissions using [requestPermissions](https://spike_api.gitlab.io/spike-android-sdk/sdk/com.spikeapi.apiv3.samsung/-samsung-health-data-permission-manager/request-permissions.html) method:

```kotlin theme={null}
// Samsung Health integration has to be enabled in Spike SDK connection before
// using further methods for reading data or managing permissions:
spikeConnection.enableSamsungHealthDataIntegration(activity = activity)

spikeConnection.getSamsungHealthDataPermissionManager().requestPermissions(
    statisticsTypes = setOf(StatisticsType.STEPS, StatisticsType.DISTANCE_TOTAL)
)
```

<Note>
  Please note that users might only grant partial permissions. In such cases, it’s up to you to decide whether your app can function effectively with limited access.
  The Spike SDK itself will still operate even without full permissions; however, it may result in no data being returned in certain scenarios.
</Note>

<Info>
  You can now use `StatisticsFilter(providers = listOf(Provider.SAMSUNG_HEALTH_DATA))` to specifically retrieve data from Samsung Health. Alternatively, you can omit the
  providers parameter entirely and allow Spike to choose the most suitable data source based on your request.
</Info>

## Step 3: Get Data

There are four types of data you can retrieve from Spike:

* **[Statistics](https://spike_api.gitlab.io/spike-android-sdk/sdk/com.spikeapi.apiv3/-spike-connection-a-p-iv3/get-statistics.html)** are calculated values derived from records.
* **[Activities](https://spike_api.gitlab.io/spike-android-sdk/sdk/com.spikeapi.apiv3/-spike-connection-a-p-iv3/get-activities.html)** are data about user's activities or workouts.
* **[Sleep](https://spike_api.gitlab.io/spike-android-sdk/sdk/com.spikeapi.apiv3/-spike-connection-a-p-iv3/get-sleep.html)** is data about user's sleep.
* **[Records](https://spike_api.gitlab.io/spike-android-sdk/sdk/com.spikeapi.apiv3/-spike-connection-a-p-iv3/get-records.html)** consist of the raw data points collected from user devices or applications.

### Statistics

Get daily statistics for steps and total distance from Samsung Health using the [getStatistics](https://spike_api.gitlab.io/spike-android-sdk/sdk/com.spikeapi.apiv3/-spike-connection-a-p-iv3/get-statistics.html) method:

```kotlin theme={null}
val dailyStatistics = spikeConnection.getStatistics(
    types = setOf(StatisticsType.STEPS, StatisticsType.DISTANCE_TOTAL),
    from = LocalDate.now().minusWeeks(1).atStartOfDay(ZoneId.systemDefault()).toInstant(),
    to = Instant.now(),
    interval = StatisticsInterval.DAY,
    filter = StatisticsFilter(providers = listOf(Provider.SAMSUNG_HEALTH_DATA))
)
```

References:

* [StatisticType](https://spike_api.gitlab.io/spike-android-sdk/sdk/com.spikeapi.apiv3.datamodels/-statistics-type/index.html)
* [StatisticsInterval](https://spike_api.gitlab.io/spike-android-sdk/sdk/com.spikeapi.apiv3.datamodels/-statistics-interval/index.html)
* [StatisticsFilter](https://spike_api.gitlab.io/spike-android-sdk/sdk/com.spikeapi.apiv3.datamodels/-statistics-filter/index.html)
* [Statistic](https://spike_api.gitlab.io/spike-android-sdk/sdk/com.spikeapi.apiv3.datamodels/-statistic/index.html)

### Records

Get all records we have from Samsung Health using the [getRecords](https://spike_api.gitlab.io/spike-android-sdk/sdk/com.spikeapi.apiv3/-spike-connection-a-p-iv3/get-records.html) method:

```kotlin theme={null}
val records = spikeConnection.getRecords(
    types = setOf(MetricType.STEPS_TOTAL, MetricType.CALORIES_BURNED_TOTAL),
    from = LocalDate.now().minusWeeks(1).atStartOfDay(ZoneId.systemDefault()).toInstant(),
    to = Instant.now(),
    filter = StatisticsFilter(providers = listOf(Provider.SAMSUNG_HEALTH_DATA))
)
```

## Background Delivery

Samsung Health data can be delivered in the background the same way as Apple HealthKit or Android Health Connect.
If you want to use background delivery to get Samsung Health data, you have to enable Samsung Health Data integration first:

```kotlin theme={null}
spikeConnection.enableSamsungHealthDataIntegration(activity = activity)
```

After enabling Samsung Health Data integration, you can use background delivery normally.
See the [background delivery section](/sdk-docs/android/background-delivery) for more details.

## Developer Mode for Testing

To test Samsung Health Data integration on your phone, you have to enable developer mode
in the Samsung Health app:

1. Tap the ‘⋮’ button of Samsung Health in the top-right.
2. Select Settings > About Samsung Health.
3. Tap the version line region quickly 10 times or more.
   If you are successful, the Developer mode (Samsung Health Data SDK) button is displayed.
4. Select Developer mode (Samsung Health Data SDK) .
5. Agree with the Notice about usage of the Developer mode.
6. To read data from Samsung Health with Samsung Health Data SDK, turn Developer Mode for Data Read on.

After your app is ready for release, you should apply for a Samsung Partnership Agreement. For more information, please contact Spike support.

<Note>
  The Samsung Health developer mode is ONLY intended for testing or debugging your app. It is NOT for app users. Do not provide a developer mode guide to app users.
</Note>

## See Also

* [Samsung error codes](https://spike_api.gitlab.io/spike-android-sdk/sdk/com.spikeapi.apiv3.samsung/-samsung-health-data-availability/error-code.html)
* [SamsungHealthDataPermissionManager](https://spike_api.gitlab.io/spike-android-sdk/sdk/com.spikeapi.apiv3.samsung/-samsung-health-data-permission-manager/index.html)
* [SamsungHealthDataAvailabilityStatus](https://spike_api.gitlab.io/spike-android-sdk/sdk/com.spikeapi.apiv3.samsung/-samsung-health-data-availability-status/index.html)
* [SpikeConnectionV3.enableSamsungHealthDataIntegration](https://spike_api.gitlab.io/spike-android-sdk/sdk/com.spikeapi.apiv3/-spike-connection-a-p-iv3/enable-samsung-health-data-integration.html)
* [SpikeConnectionV3.disableSamsungHealthDataIntegration](https://spike_api.gitlab.io/spike-android-sdk/sdk/com.spikeapi.apiv3/-spike-connection-a-p-iv3/disable-samsung-health-data-integration.html)
* [SpikeConnectionV3.isSamsungHealthDataIntegrationEnabled](https://spike_api.gitlab.io/spike-android-sdk/sdk/com.spikeapi.apiv3/-spike-connection-a-p-iv3/is-samsung-health-data-integration-enabled.html)
