Search code examples
androidgoogle-fitgoogle-fit-sdk

How to get all google fit activities performed for a day


I know how to get various data from google Fit like steps or calories when I specifically subscribe to them. However how can I retrieve all activities that user performed without knowing which ones they did exactly?

Also how can I get values for activities such as Stairs climbing? It isn't available in DataType class, the samples on google developer website only show steps and calories. Thanks


Solution

  • I found that I can only find all exercises using the Sessions API.

    Crete a request first.

    The:

    .read(DataType.TYPE_WORKOUT_EXERCISE)
    

    is important here to get all workouts.

        private SessionReadRequest readFitnessSession() {
        Calendar cal = Calendar.getInstance();
        Date now = new Date();
        cal.setTime(now);
        long endTime = cal.getTimeInMillis();
        cal.add(Calendar.WEEK_OF_YEAR, -1);
        long startTime = cal.getTimeInMillis();
    
        // Build a session read request
        SessionReadRequest readRequest = new SessionReadRequest.Builder()
                .setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS)
                .read(DataType.TYPE_WORKOUT_EXERCISE)
                .readSessionsFromAllApps()
                .build();
        // [END build_read_session_request]
    
        return readRequest;
    }
    

    And read the sessions data

        private void readSessionsApiAllSessions() {
    
        SessionReadRequest readRequest = readFitnessSession();
    
        Fitness.getSessionsClient(getActivity(), GoogleSignIn.getLastSignedInAccount(getActivity()))
                .readSession(readRequest)
                .addOnSuccessListener(new OnSuccessListener<SessionReadResponse>() {
                    @Override
                    public void onSuccess(SessionReadResponse sessionReadResponse) {
                        // Get a list of the sessions that match the criteria to check the result.
                        List<Session> sessions = sessionReadResponse.getSessions();
                        Log.i(TAG, "Session read was successful. Number of returned sessions is: "
                                + sessions.size());
    
                        for (Session session : sessions) {
                            // Process the session
                            dumpSession(session);
    
                            // Process the data sets for this session
                            List<DataSet> dataSets = sessionReadResponse.getDataSet(session);
                            for (DataSet dataSet : dataSets) {
                                dumpDataSet(dataSet);
                            }
                        }
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.i(TAG, "Failed to read session");
                    }
                });
    
    }