I am trying to create a custom DataType similar to Google docs.
https://developers.google.com/fit/datatypes/custom
I can successfully create the DataType and a Field, yet not set it's value. Here is the code.
public void writeCustomDatatype() throws ExecutionException, InterruptedException {
// Write data to Fit
Task<DataType> response = Fitness.getConfigClient(this, getGoogleAccount())
.createCustomDataType(new DataTypeCreateRequest.Builder()
.setName("com.google.android.gms.fit.samples.stepcounter.CustomDataGender1")
.addField("custom", Field.FORMAT_INT32)
.build());
// Get data to Fit
response = Fitness.getConfigClient(this, getGoogleAccount())
.readDataType("com.google.android.gms.fit.samples.stepcounter.CustomDataGender1")
.addOnSuccessListener(new OnSuccessListener<DataType>() {
@Override
public void onSuccess(DataType dataType) {
dataTypeGender = dataType;
updateGender();
Log.w(TAG, "Success in getting the gender" + dataType);
}
}).addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "There was a problem getting the gender.", e);
}
});
}
// Updating Gender
private Task<Void> updateGender() {
// Create a new dataset and update request.
DataSet dataSet = updateGenderData();
long startTime = 0;
long endTime = 0;
// Get the start and end times from the dataset.
for (DataPoint dataPoint : dataSet.getDataPoints()) {
startTime = dataPoint.getStartTime(TimeUnit.MILLISECONDS);
endTime = dataPoint.getEndTime(TimeUnit.MILLISECONDS);
}
// [START update_data_request]
Log.i(TAG, "Updating the Gender! - dataset in the History API.");
DataUpdateRequest request =
new DataUpdateRequest.Builder()
.setDataSet(dataSet)
.setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS)
.build();
// Invoke the History API to update data.
return Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this))
.updateData(request)
.addOnCompleteListener(
new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
// At this point the data has been updated and can be read.
Log.i(TAG, "Data update was successful for Gender.");
} else {
Log.e(TAG, "There was a problem updating the dataset.", task.getException());
}
}
});
}
// Update the Gender Data
private DataSet updateGenderData(){
Log.i(TAG, "Creating a new data update request.");
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
cal.add(Calendar.MINUTE, 0);
long endTime = cal.getTimeInMillis();
cal.add(Calendar.MINUTE, 0);
long startTime = cal.getTimeInMillis();
// Create a data source
DataSource dataSource =
new DataSource.Builder()
.setAppPackageName(this)
.setDataType(dataTypeGender)
.setStreamName(TAG + " - Gender")
.setType(DataSource.TYPE_RAW)
.build();
DataSet dataSet = DataSet.create(dataSource);
DataPoint dataPoint = dataSet.createDataPoint().setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS);
Value value = dataPoint.getValue(Field.custom(gender));
dataPoint.getValue(Field.FORMAT_STRING).setString(gender);
String test = value.toString();
dataSet.add(dataPoint);
// [END build_update_data_request]
return dataSet;
}
These two lines both do not work. The first line the Field.custom does not exist so it's red in the IDE even if I created the Field and the corresponding DataType. In the second line dataPoint.getValue(), the getValue() Method wants a Field and no other Field really works right now.
Value value = dataPoint.getValue(Field.custom(gender));
dataPoint.getValue(Field.FORMAT_STRING).setString(gender);
I need a Field and I can't use a Field called custom even when I created a custom dataType Field called custom. This is from my debugger.
DataType{com.google.android.gms.fit.samples.stepcounter.CustomDataGender1[custom(i)]}.
I can't set the value of the custom data types when I use custom data types it works for pre-defined datatypes. Like TYPE.FIELD_WEIGHT or TYPE.FIELD_HEIGHT. Here is an example:
DataPoint dataPoint =
dataSet.createDataPoint().setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS);
dataPoint.getValue(Field.FIELD_HEIGHT).setFloat(heightDecimalMoved);
For example this does not work as a solution for me. I've tried multiple variants of setting up the custom Field like this they all do not work currently. Setting DataPoint for for Custom DataType in Google Fit
Here is also how the custom datatype looks like that I generated with the code above. I used an integer as format that is why it say's (i).
DataType{com.google.android.gms.fit.samples.stepcounter.CustomDataGender1[custom(i)]}
The DataType for a pre-defined DataType by Google would look like this, this time with a String format.
DataType{com.google.weight[weight(f)]}
Any ideas what to do or how to proceed? I can create a custom Field with primitive datatypes for a custom DataType. But, I can't set the value similar to the pre-defined DataTypes from GoogleFit and the example I provided above, with the pre-defined DataTypes Weight and Height.
https://developers.google.com/android/reference/com/google/android/gms/fitness/data/DataType