Samsung Health app is a most popular health app and has more than 50 million of monthly active users. It works not only on Samsung smartphones but also non-Samsung phones if the device supports Android Marshmallow or above. It consists a big ecosystem including various health apps and connected accessory devices.
Samsung Health SDK for Android enables your app to manage the user’s health data with the unified interface by connecting to Samsung Health app. You can have more enhanced chances with a partnership of Samsung Health.
Even though Samsung Health is installed in Android smartphones with Marshmallow or above including Samsung and non-Samsung smartphones, some phones cannot support Samsung Health. Its check code needs to be added to prevent a relevant situation.
The SDK’s Health Data provides a solution to check it through HealthConnectionErrorResult.hasResolution(). If the app meets the following cases, it means that the device is not available for Samsung Health.
HealthConnectionErrorResult.hasResolution()
HealthDataStore.connectService()
See an example for failure exception handling.
All data type interfaces extend the HealthConstants.Common interface basically. Some data types extend HealthConstants.DiscreteMeasurement and others extend HealthConstants.SessionMeasurement depending on whether the end measured time exists.
HealthConstants.Common
HealthConstants.DiscreteMeasurement
HealthConstants.SessionMeasurement
For example, HealthConstants.Sleep extends:
HealthConstants.Sleep
You can set HealthConstants.Sleep's following properties including its own ones.
HealthConstants.Common.DEVICE_UUID
HealthConstants.SessionMeasurement.START_TIME
HealthConstants.SessionMeasurement.END_TIME
See the "Properties" description of each data type interface in API Reference for more information.
Sleep data can be set as the example shown below.
public class HealthDataExample { // The state of connection private HealthDataStore mStore; private void insertSleepData(long start, long end, long offset) { HealthDevice myDevice = new HealthDeviceManager(mStore).getLocalDevice(); HealthData data = new HealthData(); data.setSourceDevice(myDevice.getUuid()); // Fills all mandatory properties out data.putLong(HealthConstants.Sleep.START_TIME, start); data.putFloat(HealthConstants.Sleep.END_TIME, end); data.putLong(HealthConstants.Sleep.TIME_OFFSET, offset); HealthDataResolver resolver = new HealthDataResolver(mStore, null); HealthDataResolver.InsertRequest insRequest = new HealthDataResolver.InsertRequest.Builder() .setDataType(HealthConstants.Sleep.HEALTH_DATA_TYPE) .build(); // Do something } }
Handling all use cases for the phone and linked multi-accessory devices is not easy with it because the phone and accessory device like Galaxy Watch can provide step count data at the same time or not depending on how many devices are carried by the user.
Samsung Health SDK provides the following data types for steps:
HealthConstants.StepDailyTrend
HealthConstants.StepCount
HealthConstants.StepDailyTrend is used to generally to read the user’s daily steps.
Especially, its SOURCE_TYPE_ALL provides the day’s total steps simply without excluding duplicated steps when the user carries one more devices at the same time.
SOURCE_TYPE_ALL
HealthConstants.StepCount is useful for:
If new data is inserted to the health data store, CREATED_TIME, UPDATED_TIME of HealthConstants.Common are assigned by the system automatically. So you don’t need to set them separately. Data’s START_TIME or END_TIME indicates its UTC measurement time in milliseconds. Be careful not to set START_TIME or END_TIME as the device’s current local time.
CREATED_TIME, UPDATED_TIME
START_TIME
END_TIME
TIME_OFFSET is indicated by the time zone and daylight saving time in milliseconds and it helps in showing the health data’s measured time properly in the device. The following example shows getting the current device’s TIME_OFFSET.
TIME_OFFSET
import java.util.TimeZone; public class HeathDataExample { long getTimeOffset(long intakeTime) { return TimeZone.getDefault().getOffset(intakeTime); } }