Insert data

To insert data into Samsung Health, use the following API and create a HealthDataPoint object for a specific data type you want to add:

  • HealthDataBuilder.builder()

Then, you can set the data’s properties. Properties of HealthDataPoint may vary depending on data type. Be careful to set mandatory properties.

Set the source device

If the measured health data is from a specific device like a weight scale or blood glucose meter, set the data’s source device. Otherwise, the source device will be the current local device installing Samsung Health.

Find the device information in the registered devices. If no proper device, register a new device information.

Read devices registered in Samsung Health

To get a list of the devices registered in Samsung Health, you should use DeviceManager. The example below shows how to get information about a local device (an Android device in which Samsung Health is installed) and a list of watches that have stored their data in Samsung Health:

suspend fun getLocalAndWatchDevices() {
    val healthDataStore = HealthDataService.getStore(applicationContext)
    val deviceManager = healthDataStore.getDeviceManager()

    val localDevice = deviceManager.getLocalDevice()
    Log.i(TAG, """LOCAL DEVICE: 
        id: ${localDevice.id}, type: ${localDevice.deviceType}, 
        manufacturer: ${localDevice.manufacturer}, model: ${localDevice.model}, 
        name: ${localDevice.name}""".trimIndent()
    )

    val devices = deviceManager.getDevices(DeviceGroup.WATCH)
    devices.forEach { device ->
        Log.i(TAG, """DEVICE: 
            id: ${device.id}, type: ${device.deviceType}, 
            manufacturer:${device.manufacturer}, model: ${device.model}, 
            name: ${device.name}""".trimIndent()
        )
    }
}

Register your device into Samsung HealthRegister your device into Samsung Health

As mentioned above, to set the source of inserted data, it is necessary to have your device registered in Samsung Health. To do this, you need to create a DeviceRegistrationRequest. See the following example:

fun getSeed(): String {
    return "01:A2:B3:11:C4:D5" // example MAC address used for device seed
}

fun createDevice(): Device {
    return Device
        .accessoryBuilder()
        .addType(AccessoryType.BLOOD_GLUCOSE_METER)
        // add more types to this device, if required
        .build()
}

suspend fun deviceRegistration() {
    val deviceRegistrationRequest =
        DeviceRegistrationRequest.registerDevice(createDevice(), getSeed())

    val healthDataStore = HealthDataService.getStore(applicationContext)
    val deviceManager = healthDataStore.getDeviceManager()
    deviceManager.registerDevice(deviceRegistrationRequest)
}

Create a HealthDataPoint object

Having our device registered, we can obtain its id at any time using provided seed. This can be done in the following way:

suspend fun getRegisteredDeviceId(): String {
    val healthDataStore = HealthDataService.getStore(applicationContext)
    // get device based on MAC address used in registration
    val device = healthDataStore.getDeviceManager().getDeviceBySeed(getSeed())
    // return id of registered device
    return device?.id ?: ""
}

We can now use the obtained id to assign it while creating our new HealthDataPoint.

See the following example for adding new continuous glucose monitoring (CGM) data into Samsung Health. Be careful to set timestamps of each BloodGlucose sample correctly – series data must be enclosed between specified startTime and endTime of created HealthDataPoint. Otherwise, you will receive an error and you will not be able to insert the data. For example, you could consider time period of 15 minutes for the HealthDataPoint and add the first BloodGlucose entry after first 5 minutes and the second BloodGlucose entry after 10 minutes since given startTime.

Such approach is presented in the code snippet below:

suspend fun createBloodGlucoseDataPoint(
    startTime: Instant, 
    endTime: Instant
): HealthDataPoint {
    val FIVE_MINUTES_AS_SECONDS = 5L * 60
    val TEN_MINUTES_AS_SECONDS = 2 * FIVE_MINUTES_AS_SECONDS
    val values = listOf(3.90F, 3.95F)
    val bloodGlucose = BloodGlucose.of(
        values[0],   
        startTime.plusSeconds(FIVE_MINUTES_AS_SECONDS))
    val bloodGlucoseTwo = BloodGlucose.of(
        values[1], 
        startTime.plusSeconds(TEN_MINUTES_AS_SECONDS))
    val seriesData = listOf(bloodGlucose, bloodGlucoseTwo)

    return HealthDataPoint.builder()
        .setStartTime(startTime)
        .setEndTime(endTime)
        .setDeviceId(getRegisteredDeviceId())
        .setClientDataId("My ID")
        // set mandatory fields
        .addFieldData(
            DataType.BloodGlucoseType.GLUCOSE_LEVEL,
            values.average().toFloat())
        .addFieldData(
            DataType.BloodGlucoseType.MEASUREMENT_TYPE,
            DataType.BloodGlucoseType.MeasurementType.WHOLE_BLOOD)
        .addFieldData(
            DataType.BloodGlucoseType.MEAL_STATUS,
            DataType.BloodGlucoseType.MealStatus.GENERAL)
        .addFieldData(
            DataType.BloodGlucoseType.SERIES_DATA,
            seriesData)
        // set other fields
        .build()
}

You can now write created data into Samsung Health using:

  • HealthDataStore.insertData()

The example code is presented below:

suspend fun insertBloodGlucoseData(data: HealthDataPoint) {
    val healthDataStore = HealthDataService.getStore(applicationContext)    
    val insertRequest = DataTypes.BLOOD_GLUCOSE.insertDataRequestBuilder
        .addData(data)
        // add more if needed
        .build()
    
    try {
        healthDataStore.insertData(insertRequest)
    } catch (e: Exception) {
       // handle possible exceptions
       e.printStackTrace()
   }
}