2015-06-29 43 views
2

我正在构建一款应用,需要使用Google健身的Android API获取用户的当前速度。 API使用findDataSources方法找到DataType.TYPE_SPEED没有任何问题,但速度值永远不会返回。我的代码如下:Android - Google健身 - 获取用户速度

private void buildFitnessClient() { 
    mClient = new GoogleApiClient.Builder(this) 
      .addApi(Fitness.SENSORS_API) 
      .addScope(new Scope(Scopes.FITNESS_LOCATION_READ)) 
      .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() { 
       @Override 
       public void onConnected(Bundle bundle) { 
        showToast("Connected"); 
        //Calls to the Fitness API 


        Fitness.SensorsApi.findDataSources(mClient, new DataSourcesRequest.Builder() 
          // At least one datatype must be specified. 
          .setDataTypes(DataType.TYPE_SPEED) 
          .build()) 
          .setResultCallback(new ResultCallback<DataSourcesResult>() { 
           @Override 
           public void onResult(DataSourcesResult dataSourcesResult) { 
            String text = ""; 
            for (DataSource dataSource : dataSourcesResult.getDataSources()) { 
             text += "Data Source type: " + dataSource.getDataType().getName() + "\n"; 

             if(dataSource.getDataType().equals(DataType.TYPE_SPEED) && mListener == null) { 
              showToast("mListener"); 
              registerFitnessDataListener(dataSource, DataType.TYPE_SPEED); 
             } 
            } 

            tvaveragespeed.setText(text); 
           } 
          }); 
       } 

       @Override 
       public void onConnectionSuspended(int i) { 
        if(i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) 
         showToast("Connection lost. Cause: Network lost"); 
        else if(i == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) 
         showToast("Connection lost. Cause: Service disconnected"); 
       } 
      }) 
      .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() { 
       @Override 
       public void onConnectionFailed(ConnectionResult connectionResult) { 
        Log.d(TAG, "Connection failed. Reason: " + connectionResult); 

        if(!connectionResult.hasResolution()) { 
         //Show localized error dialog 
         GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), 
           MainActivity.this, 0).show(); 
         return; 
        } 

        if(!authInProgress) { 
         try { 
          Log.d(TAG, "Attempting to resolve failed connection"); 
          authInProgress = true; 
          connectionResult.startResolutionForResult(MainActivity.this, 
            REQUEST_OAUTH); 
         }catch(IntentSender.SendIntentException e) { 
          showToast("Exception while starting resolution Activity"); 
         } 
        } 
       } 
      }).build(); 
} 

private void registerFitnessDataListener(DataSource dataSource, DataType dataType) { 
    mListener = new OnDataPointListener() { 
     @Override 
     public void onDataPoint(DataPoint dataPoint) { 
      for(Field field : dataPoint.getDataType().getFields()) { 
       Value val = dataPoint.getValue(field); 
       showToast("val"); 
       tvaveragespeed.setText(val.toString()); 
      } 
     } 
    }; 

    Fitness.SensorsApi.add(
      mClient, 
      new SensorRequest.Builder() 
        .setDataSource(dataSource) 
        .setDataType(dataType) 
        .setSamplingRate(10, TimeUnit.SECONDS) 
        .build(), 
      mListener) 
      .setResultCallback(new ResultCallback<Status>() { 
            @Override 
            public void onResult(Status status) { 
             if(status.isSuccess()) { 
              showToast("Speed listener registered"); 
             }else{ 
              showToast("Problem while registering listener"); 
             } 
            } 
           } 
      ); 
} 

我在做什么错? 谢谢

回答

0

我不会有很大帮助这里,但经过.setDataTypes setadd

.setDataSourceTypes(DataSource.TYPE_DERIVED) 

,并确保在创建API客户端时,你有你的权限右

.addApi(Fitness.SENSORS_API) 
.addScope(new Scope(Scopes.FITNESS_LOCATION_READ)) 

目的。 在任何一种情况下,SPEED都不是常见的数据源,而是使用DISTANCE并相应地按时间划分。