Search code examples
androidgoogle-fit

Extract field values from DataType


I'm adding a PendingIntent listener to a sensor that read samples of the current activity using the code:

Fitness.SensorsApi.add(
            googleApiClient,
            new SensorRequest.Builder()
                    .setDataType(DataType.TYPE_ACTIVITY_SAMPLE)
                    .setSamplingRate(1, TimeUnit.MINUTES)
                    .build(), pendingIntent);

On my intentService, i can extract the DataPoint from the intent using [DataPoint.extract(intent)](https://developers.google.com/android/reference/com/google/android/gms/fitness/SensorsApi.html#add(com.google.android.gms.common.api.GoogleApiClient, com.google.android.gms.fitness.request.SensorRequest, android.app.PendingIntent)). From the dataPoint i get the DataType using getDataType(), but then i stuck in reading the the values(in this case are activity and confidence from com.google.activity.sample) from the DataType.

Thanks!


Solution

  • I manage this by calling getValue() of DataPoint passing the Field (in this case Field.FIELD_ACTIVITY and Field.FIELD_CONFIDENCE). Then i need to convert the Value.

    dataPoint.getValue(Field.FIELD_CONFIDENCE).asFloat() dataResult.getValue(Field.FIELD_ACTIVITY).asActivity()