2016-06-21 2 views
1

私はステップカウントに使用するAndroidアプリでサービスを提供しています。私は毎日と毎週のステップを取得するためのフィットネスと歴史のAPIです。突然のステップ数が常に0になるまで、すべてが正しく機能していました!アプリは順番に、私はすべてがOK働いていた言及したが、突然stepData.getDataPointsように私はこの機能ステップのためのAndroidのGoogleフィットネスAPI

private void getStepsToday() { 
    Calendar cal = Calendar.getInstance(); 
    Date now = new Date(); 

    cal.set(Calendar.HOUR_OF_DAY, 23); 
    cal.set(Calendar.MINUTE, 59); 
    cal.set(Calendar.SECOND, 59); 
    long endTime = cal.getTimeInMillis(); 

    cal.setTime(now); 
    cal.set(Calendar.HOUR_OF_DAY, 0); 
    cal.set(Calendar.MINUTE, 0); 
    cal.set(Calendar.SECOND, 0); 
    long startTime = cal.getTimeInMillis(); 

    DebugLogger.debug("Value of startTime = "+startTime+" - endTime = "+endTime); 
    DebugLogger.debug("Test for getting steps "+getStepsCount(startTime,endTime)); 

    final DataReadRequest readRequest = new DataReadRequest.Builder() 
      .read(DataType.TYPE_STEP_COUNT_DELTA) 
      .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS) 
      .build(); 

    DataReadResult dataReadResult = Fitness.HistoryApi.readData(mGoogleApiFitnessClient, readRequest).await(1, TimeUnit.MINUTES); 
    DataSet stepData = dataReadResult.getDataSet(DataType.TYPE_STEP_COUNT_DELTA); 


    int totalSteps = 0; 
    DebugLogger.debug("GetStepsToday - Size in for loop "+stepData.getDataPoints().size()); 
    for (DataPoint dp : stepData.getDataPoints()) { 
     for(Field field : dp.getDataType().getFields()) { 
      int steps = dp.getValue(field).asInt(); 
      DebugLogger.debug("GoogleFit Service - debugging - steps count "+steps); 
      totalSteps += steps; 
     } 
    } 
    publishTodaysStepData(totalSteps, getGoalStep(goalEnabledDate)); 
} 

を使用し、毎日の手順を取得するためにしながら、上記のコード

private void buildFitnessClient() { 
    mGoogleApiFitnessClient = new GoogleApiClient.Builder(this) 
      .addApi(Fitness.SENSORS_API) 
      .addApi(Fitness.HISTORY_API) 
      .addApi(Fitness.RECORDING_API) 
      .addScope(new Scope(Scopes.FITNESS_BODY_READ_WRITE)) 
      .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE)) 
      .addScope(new Scope(Scopes.FITNESS_LOCATION_READ_WRITE)) 
      .addConnectionCallbacks(
        new GoogleApiClient.ConnectionCallbacks() { 
         @Override 
         public void onConnected(Bundle bundle) { 
          Log.i(TAG, "Google Fit connected."); 
          mTryingToConnect = false; 
          Log.d(TAG, "Notifying the UI that we're connected."); 
          notifyUiFitConnected(); 

         } 

         @Override 
         public void onConnectionSuspended(int i) { 
          mTryingToConnect = false; 
          if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) { 
           Log.i(TAG, "Google Fit Connection lost. Cause: Network Lost."); 
          } else if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) { 
           Log.i(TAG, "Google Fit Connection lost. Reason: Service Disconnected"); 
          } 
         } 
        } 
      ) 
      .addOnConnectionFailedListener(
        new GoogleApiClient.OnConnectionFailedListener() { 
         // Called whenever the API client fails to connect. 
         @Override 
         public void onConnectionFailed(ConnectionResult result) { 
          mTryingToConnect = false; 
          notifyUiFailedConnection(result); 
         } 
        } 
      ) 
      .build(); 
} 

を使用してフィットネスAPIに接続しているようです()sizeは常に0です。私はデバッグとリリースのキーストアのために新しいOathキーを作成しました。私はアプリのパッケージ名を変更し、Android APIコンソールでプロジェクトを再作成しました。パーミッションなどを変更する必要がありますか?私は私のアプリのどこにでも私のキーを宣言する必要がありますか?

+0

これはhttp://www.gadgetsaint.com/android/google-fit-steps-calories-android/#.WESRX6J97BJに役立つ可能性があります – ASP

答えて

0

端末でGoogle Playサービスのバージョンを確認し、開発ホストにGoogle Playサービス用の最新のクライアントライブラリがあることを確認してください。

apply plugin: 'com.android.application' 
... 

dependencies { 
compile 'com.google.android.gms:play-services-fitness:9.0.1' 
} 
  • は、Android SDK Managerを開きます。
  • エクストラの下にGoogle PlayサービスとGoogleリポジトリがあります。
  • これらのパッケージのステータスが「インストール済」と異なる場合は、両方を選択して[パッケージのインストール]をクリックします。

APIsAuthが設定されている場合にも、Googleのデベロッパーコンソールで確認してください。

関連する問題