public static interface HealthDataResolver.InsertRequest
InsertRequest
You can make a request instance with InsertRequest.Builder.build().
You can save health data to Samsung Health with HealthDataResolver.insert() after setting detailed property values of the created HealthData instance.
HealthData
The health data framework registers the current device where your application is installed. If the source device is the current device, its ID can be checked as shown below.
// The state of connection private final HealthDataStore mStore; public static final String APP_TAG = "MyApp"; private void insertGlucose(long start, long offset, float value) { // Set the source device as the current device HealthDevice myDevice = new HealthDeviceManager(mStore).getLocalDevice(); HealthData data = new HealthData(); data.setSourceDevice(myDevice.getUuid());
And the data instance's properties including mandatory ones needs to be set.
data
// Set properties including mandatory ones data.putLong(HealthConstants.BloodGlucose.START_TIME, start); data.putLong(HealthConstants.BloodGlucose.TIME_OFFSET, offset); data.putInt(HealthConstants.BloodGlucose.MEASUREMENT_TYPE, HealthConstants.BloodGlucose.MEASUREMENT_TYPE_WHOLE_BLOOD);
The measured data value needs to be set after checking defined data unit. Each property in API Reference lets you know it. In this case, see HealthConstants.BloodGlucose.GLUCOSE
HealthConstants.BloodGlucose.GLUCOSE
data.putFloat(HealthConstants.BloodGlucose.GLUCOSE, value);
An InsertRequest instance is created by InsertRequest.Builder.build() with setting the data type. It's ready to add data to Samsung Health if the data is added to the InsertRequest.
InsertRequest.Builder.build()
HealthDataResolver.InsertRequest insRequest = new HealthDataResolver.InsertRequest.Builder() .setDataType(HealthConstants.BloodGlucose.HEALTH_DATA_TYPE) .build(); // Add health data insRequest.addHealthData(data);
HealthDataResolver resolver = new HealthDataResolver(mStore, null); try { resolver.insert(insRequest).setResultListener(mIstResult); } catch (Exception e) { Log.d(APP_TAG, "resolver.insert() fails."); } }
HealthDataResolver.insert(com.samsung.android.sdk.healthdata.HealthDataResolver.InsertRequest) sends a request to Samsung Health. A data validation exception is received in HealthResultHolder.BaseResult. It gives a request's result state and a number of inserted data.
HealthDataResolver.insert(com.samsung.android.sdk.healthdata.HealthDataResolver.InsertRequest)
HealthResultHolder.BaseResult
private final HealthResultHolder.ResultListener<HealthResultHolder.BaseResult> mIstResult = new HealthResultHolder.ResultListener<HealthResultHolder.BaseResult>(){ @Override public void onResult(HealthResultHolder.BaseResult result) { if(result.getStatus() != STATUS_SUCCESSFUL) { // Check the error } else { // Check the count of inserted data Log.d(APP_TAG, "Count of inserted data: " + result.getCount()); } } };
static class
HealthDataResolver.InsertRequest.Builder
HealthDataResolver.InsertRequest
void
addHealthData(HealthData object)
addHealthData(List<HealthData> objectList)
void addHealthData(HealthData object)
object
IllegalArgumentException
null
void addHealthData(List<HealthData> objectList)
objectList