3

Pardon noobiness, I am completely new to Android, and Google APIs. I have the following code that connects to GoogleFit. I also have an API key and Oauth.

Where/how do i use API key and Oauth? Lots of guides on how to obtain them but zero info on where to put them/how to use them in the app.

And how do I actually use the steps returned. I set up a global:

private int steps;

and then try to set it via:

steps = (int)total;

but it does nothing.

Here is the rest of the function. How do I actually get the step count out of it.

 private void accessGoogleFit() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        long endtime = cal.getTimeInMillis();
        cal.add(Calendar.YEAR, -1);
        long starttime = cal.getTimeInMillis();

        DataReadRequest readRequest = new DataReadRequest.Builder()
                .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
                .bucketByTime(1, TimeUnit.DAYS)
                .setTimeRange(starttime, endtime, TimeUnit.MILLISECONDS)
                .build();

        Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this))
                .readDailyTotal(DataType.TYPE_STEP_COUNT_DELTA)
                .addOnSuccessListener(new OnSuccessListener<DataSet>() {
                    @Override
                    public void onSuccess(DataSet dataSet) {
                        Log.d("Status", "Success");
                        long total = dataSet.isEmpty()
                                ? 0
                                : dataSet.getDataPoints().get(0).getValue(Field.FIELD_STEPS).asInt();
                        Log.d("Steps ", String.valueOf(total));
                        steps = (int)total; //Trying to get steps here
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.d("Status", "Failure", e);
                    }
                })
                .addOnCompleteListener(new OnCompleteListener<DataSet>() {
                    @Override
                    public void onComplete(@NonNull Task<DataSet> task) {
                        Log.d("Status", "Complete");

                    }
                });

    }

Ive been up and down the official documentation and StackOverflow. But it seems like google made big changes to the API last year and so most things are outdated, including google's own tutorials (posted in 2015). And a few places that have updated documentation provide snippets of code, and I have no idea how to use them or where to put them.

Duxa
  • 966
  • 2
  • 14
  • 27
  • did you check if the fit api have some data? – Vadim Eksler Mar 06 '19 at 09:33
  • I dont know how to do that. How do I check that? – Duxa Mar 06 '19 at 09:34
  • if you try it on real device install the google fit app and open it. if it show some data – Vadim Eksler Mar 06 '19 at 09:37
  • I was able to check your request in my app now, it works and show me on this `dataReadResponse.getBuckets().get(0));` : my log `Bucket{startTime=1551260296979, endTime=1551865096979, activity=0, dataSets=[DataSet{d:step_count.delta:gms:aggregated [RawDataPoint{[21648]@[1551260985001540984, 1551864350023920959](0,1)}]}], bucketType=time, serverHasMoreData=false}` – Vadim Eksler Mar 06 '19 at 09:40
  • my callback - `.addOnSuccessListener(new OnSuccessListener()` – Vadim Eksler Mar 06 '19 at 09:42
  • Tried running on an android phone that has Google Fit installed and since there is no logcat Im not sure if theres data or not... I do have a textView being set to the number if >0 and textView is blank... so im guessing it cant read the steps?? That request you posted above... what does it mean and how can I use that information? I have a feeling I am not retrieving step # correctly... like I need another function call or something? – Duxa Mar 06 '19 at 09:44
  • open your google fit app. it will show if you have some data in your api – Vadim Eksler Mar 06 '19 at 09:45
  • oh, yes it has steps and calories (I carried it around all day today on purpose to get some data).. – Duxa Mar 06 '19 at 09:46
  • take a look my answer – Vadim Eksler Mar 06 '19 at 09:51

1 Answers1

2

This is how i make my request Google Fit API : request with FitnessOptions like

    FitnessOptions fitnessOptions = FitnessOptions.builder()
            .addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
            .addDataType(DataType.TYPE_DISTANCE_DELTA, FitnessOptions.ACCESS_READ)
            .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
            .build();

you will need to request GoogleSignIn.requestPermissions

And my request function

Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        long endTime = cal.getTimeInMillis();
        cal.add(Calendar.WEEK_OF_YEAR, -1);
        long startTime = cal.getTimeInMillis();

        DataReadRequest readRequest = new DataReadRequest.Builder()
                .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
//                .read(DataType.TYPE_STEP_COUNT_DELTA)
                .bucketByTime(8, TimeUnit.DAYS)
                .enableServerQueries()
                .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
                .build();

        Fitness.getHistoryClient(
                this,
                GoogleSignIn.getLastSignedInAccount(this))
                .readData(readRequest)
                .addOnSuccessListener(new OnSuccessListener<DataReadResponse>() {
                    @Override
                    public void onSuccess(DataReadResponse dataReadResponse) {
                        Log.d("TAG_F", "onSuccess: 1 " + dataReadResponse.toString());
                        Log.d("TAG_F", "onSuccess: 1 " + dataReadResponse.getStatus());
                        Log.d("TAG_F", "onSuccess: 1 " + dataReadResponse.getDataSet(DataType.TYPE_STEP_COUNT_DELTA));
                        Log.d("TAG_F", "onSuccess: 1 " + dataReadResponse.getBuckets().get(0));
                        Log.d("TAG_F", "onSuccess: 1 " + dataReadResponse.getBuckets().get(0).getDataSets().size());

                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.d("TAG_F", "onFailure: 1 " + e.getMessage());
                    }
                })
                .addOnCompleteListener(new OnCompleteListener<DataReadResponse>() {
                    @Override
                    public void onComplete(@NonNull Task<DataReadResponse> task) {
                        Log.d("TAG_F", "onComplete: 1 ");
                    }
                });
Vadim Eksler
  • 865
  • 9
  • 24
  • I think that works... but I am still unsure how to set int stepNumber...... can I do something like stepNumber = dataReadResponse.getDataSet(DataType.TYPE_STEP_COUNT_DELTA)); Or do I need to extract it from fitnessOptions somehow? – Duxa Mar 06 '19 at 09:56
  • to clarify, how do I actually turn that response into a simple Integer or Long that I can use in my app? – Duxa Mar 06 '19 at 10:19
  • easy)) you will back to `.readDailyTotal(DataType.TYPE_STEP_COUNT_DELTA)` and `.addOnSuccessListener(new OnSuccessListener()`. On your result DataSet you can get: `dataSet.getDataPoints().get(0).getValue(Field.FIELD_STEPS).asInt());` – Vadim Eksler Mar 06 '19 at 11:07
  • When I try to set int steps = dataSet.getDataPoints().get(0).getValue(Field.FIELD_STEPS).asInt()); it compiles and runs... but crashes on the part where I try to set the TextView to .setText(steps); I think garbage is being stored in steps? Do I need to tell it to use API key somewhere? or Oauth? Because ive yet to put those anywhere since I have no idea where they go. – Duxa Mar 06 '19 at 23:10
  • ? I also use android studio on win 10, and have logs on all my real devices. – Vadim Eksler Mar 07 '19 at 06:30
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/189574/discussion-between-duxa-and-vadim-eksler). – Duxa Mar 07 '19 at 06:31
  • I am getting dataSet.getDataPoints() with zero size can you please help me to figure out why i am getting zero everytime.? – Maradiya Krupa Aug 08 '20 at 13:36