Update data

You can also update your app’s data in Samsung Health with:

  • HealthDataStore.updateData()

After reading data, select the data’s ID to update. You can use:

  • uid: The data’s ID managed in Samsung Health. It is automatically assigned by Samsung Health when a new data record is created. You can obtain it by making standard read request and accessing returned HealthDataPoint’s uid field.
  • clientDataId: It indicates the ID managed in your app. It works only when the clientDataId has been set at the time of data insertion.

The following example shows how to update BloodGlucoseType data using the clientDataId.

suspend fun updateBloodGlucoseData(
    startTime: Instant, 
    endTime: Instant
) {
    val healthDataStore = HealthDataService.getStore(applicationContext)

    val FIVE_MINUTES_AS_SECONDS = 5L * 60
    val TEN_MINUTES_AS_SECONDS = 2 * FIVE_MINUTES_AS_SECONDS
    val newValues = listOf(5.4F, 5.5F)
    val bloodGlucose = BloodGlucose.of(
        newValues[0], 
        startTime.plusSeconds(FIVE_MINUTES_AS_SECONDS))
    val bloodGlucoseTwo = BloodGlucose.of(
        newValues[1],  
        startTime.plusSeconds(TEN_MINUTES_AS_SECONDS))
    val seriesData = listOf(bloodGlucose, bloodGlucoseTwo)

    val updatedData = HealthDataPoint.builder()
        .setStartTime(startTime)
        .setEndTime(endTime)
        .addFieldData(
            DataType.BloodGlucoseType.GLUCOSE_LEVEL,
            newValues.average().toFloat())
        .addFieldData(
            DataType.BloodGlucoseType.SERIES_DATA,
            seriesData)
        .build()

    val updateRequest = DataTypes.BLOOD_GLUCOSE.updateDataRequestBuilder
        // updating with the app's own Data ID
        .addDataWithClientDataId(clientDataId = "My ID", data = updatedData)
        // you may update more data entries in the request
        .build()

    try {
        healthDataStore.updateData(updateRequest)
    } catch (e: Exception) {
       // handle possible exceptions
       e.printStackTrace()
   }
}