2015-01-02 71 views
3

我無法獲取使用Google Fit Api的距離。我使用了類似的計步器,它的工作。它只是說聽衆註冊。Fit Api中的距離

大部分代碼來自Github示例。

什麼可能是錯的?

public class MainActivity extends Activity { 

public static final String TAG = "BasicSensorsApi"; 
private static final int REQUEST_OAUTH = 1; 
private OnDataPointListener mListener; 
private static final String AUTH_PENDING = "auth_state_pending"; 
private boolean authInProgress = false; 

TextView dispSteps; 
long numSteps; 

private GoogleApiClient mClient = null; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_activity); 

    if (savedInstanceState != null) { 
     authInProgress = savedInstanceState.getBoolean(AUTH_PENDING); 
    } 

    dispSteps=(TextView)findViewById(R.id.dispStepsTV); 
    numSteps=0; 

    buildFitnessClient(); 

} 

private void updateDispSteps(final int update) { 

    numSteps+=update; 

    runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      dispSteps.setText(String.valueOf(numSteps)); 
     } 
    }); 
} 

private void buildFitnessClient() { 
    // Create the Google API Client 
    mClient = new GoogleApiClient.Builder(this) 
      .addApi(Fitness.API) 
      .addScope(new Scope(Scopes.FITNESS_LOCATION_READ_WRITE)) 
      .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE)) 
      .addConnectionCallbacks(
        new GoogleApiClient.ConnectionCallbacks() { 

         @Override 
         public void onConnected(Bundle bundle) { 
          Log.i(TAG, "Connected!!!"); 

          Fitness.RecordingApi.subscribe(mClient, DataType.TYPE_DISTANCE_DELTA) 
            .setResultCallback(new ResultCallback<Status>() { 
             @Override 
             public void onResult(Status status) { 
              if (status.isSuccess()) { 
               if (status.getStatusCode() 
                 == FitnessStatusCodes.SUCCESS_ALREADY_SUBSCRIBED) { 
                Log.i(TAG, "Existing subscription for activity detected."); 

               } else { 
                Log.i(TAG, "Successfully subscribed!"); 
               } 
              } else { 
               Log.i(TAG, "There was a problem subscribing."); 

              } 
             } 
            }); 

          mListener = new OnDataPointListener() { 
           @Override 
           public void onDataPoint(DataPoint dataPoint) { 
            for (Field field : dataPoint.getDataType().getFields()) { 
             Value value = dataPoint.getValue(field); 
             updateDispSteps(value.asInt()); 
             Log.i("BIG BLUE TEXT", value.toString()); 
            } 
           } 
          }; 

          SensorRequest req = new SensorRequest.Builder() 
            .setDataType(DataType.TYPE_DISTANCE_DELTA) 
            .setSamplingRate(10, TimeUnit.SECONDS) 
            .build(); 

          Fitness.SensorsApi.add(mClient, req, mListener) 
            .setResultCallback(new ResultCallback<Status>() { 
             @Override 
             public void onResult(Status status) { 
              if (status.isSuccess()) { 
               Log.i(TAG, "Listener registered!"); 
              } else { 
               Log.i(TAG, "Listener not registered."); 
              } 
             } 
            }); 

         } 

         @Override 
         public void onConnectionSuspended(int i) { 
          if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) { 
           Log.i(TAG, "Connection lost. Cause: Network Lost."); 
          } else if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) { 
           Log.i(TAG, "Connection lost. Reason: Service Disconnected"); 
          } 
         } 
        } 
      ) 
      .addOnConnectionFailedListener(
        new GoogleApiClient.OnConnectionFailedListener() { 
         // Called whenever the API client fails to connect. 
         @Override 
         public void onConnectionFailed(ConnectionResult result) { 
          Log.i(TAG, "Connection failed. Cause: " + result.toString()); 
          if (!result.hasResolution()) { 
           // Show the localized error dialog 
           GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), 
             MainActivity.this, 0).show(); 
           return; 
          } 
          if (!authInProgress) { 
           try { 
            Log.i(TAG, "Attempting to resolve failed connection"); 
            authInProgress = true; 
            result.startResolutionForResult(MainActivity.this, 
              REQUEST_OAUTH); 
           } catch (IntentSender.SendIntentException e) { 
            Log.e(TAG, 
              "Exception while starting resolution activity", e); 
           } 
          } 
         } 
        } 
      ) 
      .build(); 
}  

PS:這是我的第一個計算器問題:P

+0

,如果你能得到步數則u墊將其轉換成長度。大約1300步驟等於1公里 –

回答

1
I think sensor api is used to calculate distance and other values, recoding is for storing them.I used the sensor api and it works for me. 

private void buildFitnessClient() { 
    if (mClient == null && checkPermissions()) { 
     mClient = new GoogleApiClient.Builder(this) 
        .addApi(Fitness.SENSORS_API) 
        .addScope(new Scope(Scopes.FITNESS_LOCATION_READ)) 
        .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))....}} 


private void findDistDataSrc() { 
Fitness.SensorsApi.findDataSources(mClient, new DataSourcesRequest.Builder() 
// At least one datatype must be specified. 
.setDataTypes(DataType.TYPE_STEP_COUNT_CUMULATIVE, 
    DataType.TYPE_DISTANCE_DELTA) 
    // Can specify whether data type is raw or derived. 
    .setDataSourceTypes(DataSource.TYPE_RAW, DataSource.TYPE_DERIVED) 
       .build()) 
    .setResultCallback(new ResultCallback<DataSourcesResult>() { 
    @Override 
    public void onResult(DataSourcesResult dataSourcesResult) { 
    //Log.i(TAG, "Result: " + dataSourcesResult.getStatus().toString()); 
    for (DataSource dataSource : dataSourcesResult.getDataSources()) { 
    //Log.i(TAG, "Data source found: " + dataSource.toString()); 
    //Log.i(TAG, "Data Source type: " + dataSource.getDataType().getName()); 
final DataType dataType = dataSource.getDataType(); 
    if ((dataType.equals(DataType.TYPE_STEP_COUNT_CUMULATIVE) || dataType.equals(DataType.TYPE_DISTANCE_DELTA)) && mListener == null) { 
    Log.i(TAG, "Data source for " + dataType.toString() + " found! Registering."); 
..........}