Step 1: Create Spike connection

If you already set up Health Connect integration in your app, you should skip this step and use the same SpikeSDK connection object.

To set up the Spike SDK create SpikeConnectionV3 instance with your Spike application id, application user id and signature unique to each of your apllication users (more on generating signatures here):


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 user gives your app permissions. First, you have to check if Samsung Health is available on users phone using checkSamsungHealthDataAvailability method:

val availability = spikeConnection.checkSamsungHealthDataAvailability(activity = activity)

where

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 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 other error.
     */
    ERROR_OTHER("ERROR_OTHER"),

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

If Samsung Health is installed, you can ask user for permissions using requestPermissions method:

// Samsung Health integration has to be enabled in SpikeSDK 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)
)

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 SpikeSDK itself will still operate even without full permissions; however, it may result in no data being returned in certain scenarios.

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.

Step 3: Get data

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

  • Statistics are calculated values derived from records.
  • Activities are data about user’s activities or workouts.
  • Sleep is data about user’s sleep.
  • Records 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 getStatistics method:

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:

Records

Get all records we have from Samsung Health using getRecords method:

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 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:

spikeConnection.enableSamsungHealthDataIntegration(activity = activity)

After enabling Samsung Health Data integration, you can use background delivery normally. See Background Delivery section for more details.

See also