I'm trying to build a step counter application, as a test i downloaded the android fit code from github and ran the basicsensorsAPI:
In order to get stepcount instead of location I changed the data type to TYPE_STEP_COUNT_CUMULATIVE and TYPE_DERIVED, (the orignials are TYPE_LOCATION_SAMPLE and TYPE_RAW). But as soon as I do this the OAUTH stops working, and i'm not sure why this is creating an issue.
Here is the changed code:
private void findFitnessDataSources() {
// [START find_data_sources]
// Note: Fitness.SensorsApi.findDataSources() requires the ACCESS_FINE_LOCATION permission.
Fitness.SensorsApi.findDataSources(mClient, new DataSourcesRequest.Builder()
// At least one datatype must be specified.
.setDataTypes(DataType.TYPE_STEP_COUNT_CUMULATIVE)
// Can specify whether data type is raw or derived.
.setDataSourceTypes(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());
//Let's register a listener to receive Activity data!
if (dataSource.getDataType().equals(DataType.TYPE_STEP_COUNT_CUMULATIVE)
&& mListener == null) {
Log.i(TAG, "Data source for LOCATION_SAMPLE found! Registering.");
registerFitnessDataListener(dataSource,
DataType.TYPE_STEP_COUNT_CUMULATIVE);
}
}
}
});
// [END find_data_sources]
}
Here's the original code:
private void findFitnessDataSources() {
// [START find_data_sources]
// Note: Fitness.SensorsApi.findDataSources() requires the ACCESS_FINE_LOCATION permission.
Fitness.SensorsApi.findDataSources(mClient, new DataSourcesRequest.Builder()
// At least one datatype must be specified.
.setDataTypes(DataType.TYPE_LOCATION_SAMPLE)
// Can specify whether data type is raw or derived.
.setDataSourceTypes(DataSource.TYPE_RAW)
.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());
//Let's register a listener to receive Activity data!
if (dataSource.getDataType().equals(DataType.TYPE_LOCATION_SAMPLE)
&& mListener == null) {
Log.i(TAG, "Data source for LOCATION_SAMPLE found! Registering.");
registerFitnessDataListener(dataSource,
DataType.TYPE_LOCATION_SAMPLE);
}
}
}
});
// [END find_data_sources]
}
I get this output: Application needs oAuth consent from the User.
Its about scope. visit this google developer page
Data type for which you are adding data should be in scope. means if you want to add
You need to add relevant scope. which is
and If you want to add
than you need to add scope
https://www.googleapis.com/auth/fitness.activity.write
or "FITNESS_ACTIVITY_READ_WRITE"
According to this Getting Started on Android:
Also,
Connect to the Google Fit Platform, and then check and request permissions if required by the Fit APIs that you use in your app:
Create the Google API client and provide the required callback methods:
I had also faced this problem.
You need to ask for a permission if you face this problem: just add this code inside your
onResult()
methodand in
onActivityResult
, call again your methodYou can also check here for more info here. Please visit this url. It can explain you all the status code and error regarding google fit