Search code examples
androidgoogle-fitgoogle-fit-sdk

Android Google Fit get basic info (Gender, Height, Weight)


I have been working on Google fit integration for Android since a couple of days and got stuck at a point. I need to get height, weight and gender data from google fit. I could successfully get the height and weight of the user from google fit by making a query using the following data types

 DataReadRequest readRequest = new DataReadRequest.Builder()
                               .read(DataType.TYPE_WEIGHT)
                               .read(DataType.TYPE_HEIGHT)
                               .setLimit(1)
                               .setTimeRange(1, currentTime, TimeUnit.MILLISECONDS)
                               .build();

But failed to get the details of the user's gender. I'm wasting a huge time in figuring this out. I would like to at least know that getting gender from Google fit is not possible.

Thanks in advance.


Solution

  • I'm afraid google don't provide pre defined DataType for gender as of now. It might possible they provide support in upcoming updates. There are four types of fields for all Google Fit data types. You can check the data types list here without investing lot of time in research.

    I would like to at least know that getting gender from Google fit is not possible.

    This is achievable through Custom data types.

    1. Build a request to for your custom data type
    2. With Google API client object invoke the Config API
    3. Check the result asynchronously

    Below a sample code for your reference :

        GoogleApiClient client = new GoogleApiClient.Builder(context)
         .addApi(Fitness.CONFIG_API)
         ... .build();
         client.connect();
    
         PendingResult<DataTypeResult> pendingResult = Fitness.ConfigApi.readDataType(
             client, "com.example.my_custom_data_type");
    
         DataTypeResult dataTypeResult = pendingResult.await();
         DataType dataType = dataTypeResult.getDataType();