2

I'm trying to write a Watch face with some information. Now I want to read the daily step count...

I tryed this code:

        Calendar start;

        start = Calendar.getInstance();
        start.setTimeInMillis(System.currentTimeMillis());
        start.set(Calendar.MILLISECOND, 0);
        start.set(Calendar.SECOND, 0);
        start.set(Calendar.MINUTE, 0);
        start.set(Calendar.HOUR_OF_DAY, 0);

        try
        {
          GoogleSignInOptionsExtension fitnessOptions =
                  FitnessOptions.builder()
                          .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
                          .build();

          GoogleSignInAccount googleSignInAccount =
                  GoogleSignIn.getAccountForExtension(getApplicationContext(), fitnessOptions);

          Task<DataReadResponse> response = Fitness.getHistoryClient(getApplicationContext(), googleSignInAccount)
                  .readData(new DataReadRequest.Builder()
                          .read(DataType.TYPE_STEP_COUNT_DELTA)
                          .setTimeRange(start.getTimeInMillis(), System.currentTimeMillis(), TimeUnit.MILLISECONDS)
                          .build());

          DataReadResponse readDataResult = Tasks.await(response);
          DataSet dataSet = readDataResult.getDataSet(DataType.TYPE_STEP_COUNT_DELTA);
          total = dataSet.isEmpty()
                  ? 0
                  : dataSet.getDataPoints().get(0).getValue(Field.FIELD_STEPS).asInt();
        }
        catch(ExecutionException e)
        {
          e.printStackTrace();
        }
        catch(InterruptedException e)
        {
          e.printStackTrace();
        }

No error on compile, no information on the emulator, but when I try to run it on my SmartWatch I get this Exception:

05-10 21:47:28.045  4273  4273 D PilotWatch: Heart rate: 0
05-10 21:47:28.135  4273  4902 W System.err: java.util.concurrent.ExecutionException: com.google.android.gms.common.api.ApiException: 17: API: Fitness.CLIENT is not available on this device. Connection failed with: ConnectionResult{statusCode=INVALID_ACCOUNT, resolution=null, message=null}
05-10 21:47:28.135  4273  4902 W System.err:    at com.google.android.gms.tasks.Tasks.zza(com.google.android.gms:play-services-tasks@@17.0.2:69)
05-10 21:47:28.135  4273  4902 W System.err:    at com.google.android.gms.tasks.Tasks.await(com.google.android.gms:play-services-tasks@@17.0.2:23)
05-10 21:47:28.135  4273  4902 W System.err:    at de.lucabert.pilotwatch.PilotWatch$Engine$DailyStepTask.doInBackground(PilotWatch.java:284)
05-10 21:47:28.135  4273  4902 W System.err:    at de.lucabert.pilotwatch.PilotWatch$Engine$DailyStepTask.doInBackground(PilotWatch.java:252)
05-10 21:47:28.135  4273  4902 W System.err:    at android.os.AsyncTask$2.call(AsyncTask.java:295)
05-10 21:47:28.135  4273  4902 W System.err:    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-10 21:47:28.135  4273  4902 W System.err:    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
05-10 21:47:28.135  4273  4902 W System.err:    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
05-10 21:47:28.135  4273  4902 W System.err:    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
05-10 21:47:28.135  4273  4902 W System.err:    at java.lang.Thread.run(Thread.java:818)
05-10 21:47:28.135  4273  4902 W System.err: Caused by: com.google.android.gms.common.api.ApiException: 17: API: Fitness.CLIENT is not available on this device. Connection failed with: ConnectionResult{statusCode=INVALID_ACCOUNT, resolution=null, message=null}
05-10 21:47:28.136  4273  4902 W System.err:    at com.google.android.gms.common.internal.ApiExceptionUtil.fromStatus(com.google.android.gms:play-services-base@@17.2.1:4)
05-10 21:47:28.136  4273  4902 W System.err:    at com.google.android.gms.common.internal.zaj.zaf(com.google.android.gms:play-services-base@@17.2.1:2)
05-10 21:47:28.136  4273  4902 W System.err:    at com.google.android.gms.common.internal.zai.onComplete(com.google.android.gms:play-services-base@@17.2.1:6)
05-10 21:47:28.136  4273  4902 W System.err:    at com.google.android.gms.common.api.internal.BasePendingResult.zaa(com.google.android.gms:play-services-base@@17.2.1:176)
05-10 21:47:28.136  4273  4902 W System.err:    at com.google.android.gms.common.api.internal.BasePendingResult.setResult(com.google.android.gms:play-services-base@@17.2.1:135)
05-10 21:47:28.136  4273  490

Line 284 is DataReadResponse readDataResult = Tasks.await(response);
Why the evil should the Fitness.CLIENT not be available? And how can I make it available?
I really don't know what I can do to get the daily steps... I found many code example for this purpose, but nothing works...

Can someone help me?

Thanks a lot
Luca

  • related: https://stackoverflow.com/questions/48152896/google-fit-api-apiexception-17-fitness-client-is-not-available-on-this-device – njzk2 May 10 '20 at 20:05
  • I don't have an Activity in my App... I'm writing a WatchFace, so I have my Class extending CanvasWatchFaceService... – Luca Bertoncello May 10 '20 at 20:11
  • have you tried with an up date watch OS ? – Harsh May 10 '20 at 20:14
  • @Harsh: no, I only tried with **my** SmartWatch... ;) Android 6. I can't upgrade the Watch... I tried with Fitness.HistoryApi.readDailyTotal(googleApiClient, DataType.TYPE_STEP_COUNT_DELTA) too, but I get a Timeout reading the results with result.await(1, TimeUnit.SECONDS) – Luca Bertoncello May 10 '20 at 20:23
  • you are confident that the account you use is valid for this? (I'm not familiar with the API, but shouldn't there be a permission somewhere that you allow this app to access this data?) – njzk2 May 11 '20 at 20:31

0 Answers0