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:
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()
}
}
Manage Your Cookies
We use cookies to improve your experience on our website and to show you relevant
advertising. Manage you settings for our cookies below.
Essential Cookies
These cookies are essential as they enable you to move around the website. This
category cannot be disabled.
Company
Domain
Samsung Electronics
.samsungdeveloperconference.com
Analytical/Performance Cookies
These cookies collect information about how you use our website. for example which
pages you visit most often. All information these cookies collect is used to improve
how the website works.
Company
Domain
LinkedIn
.linkedin.com
Meta (formerly Facebook)
.samsungdeveloperconference.com
Google Inc.
.samsungdeveloperconference.com
Functionality Cookies
These cookies allow our website to remember choices you make (such as your user name, language or the region your are in) and
tailor the website to provide enhanced features and content for you.
Company
Domain
LinkedIn
.ads.linkedin.com, .linkedin.com
Advertising Cookies
These cookies gather information about your browser habits. They remember that
you've visited our website and share this information with other organizations such
as advertisers.
Company
Domain
LinkedIn
.linkedin.com
Meta (formerly Facebook)
.samsungdeveloperconference.com
Google Inc.
.samsungdeveloperconference.com
Preferences Submitted
You have successfully updated your cookie preferences.