Can't connect to the google api, the result on the monitor is:
E/GoogleFit: RESULT_CANCELED (this message only appears after select the google account)
Obviously, i have the internet permission on my app.
on the google developer console, the name of the package is the same of my project.
Here is the code:
public class MainActivity extends AppCompatActivity implements OnDataPointListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
private static final int REQUEST_OAUTH = 1;
private static final String AUTH_PENDING = "auth_state_pending";
private boolean authInProgress = false;
private GoogleApiClient mApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState != null){
authInProgress = savedInstanceState.getBoolean(AUTH_PENDING);
}
mApiClient = new GoogleApiClient.Builder(this)
.addApi(Fitness.SENSORS_API)
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
.addConnectionCallbacks(this).addOnConnectionFailedListener(this)
.build(); {
}
}
@Override
protected void onStart() {
super.onStart();
mApiClient.connect();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
DataSourcesRequest dataSourcesRequest = new DataSourcesRequest.Builder()
.setDataTypes(DataType.TYPE_STEP_COUNT_CUMULATIVE)
.setDataSourceTypes(DataSource.TYPE_RAW)
.build();
ResultCallback<DataSourcesResult> dataSourcesResultResultCallback = new ResultCallback<DataSourcesResult>() {
@Override
public void onResult(@NonNull DataSourcesResult dataSourcesResult) {
for(DataSource dataSource : dataSourcesResult.getDataSources()){
if(DataType.TYPE_STEP_COUNT_CUMULATIVE.equals(dataSource.getDataType())){
registerFitnessDataListener(dataSource, DataType.TYPE_STEP_COUNT_CUMULATIVE);
}
}
}
};
Fitness.SensorsApi.findDataSources(mApiClient, dataSourcesRequest).setResultCallback(dataSourcesResultResultCallback);
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
if(!authInProgress){
try{
authInProgress = true;
connectionResult.startResolutionForResult(MainActivity.this, REQUEST_OAUTH);
}catch (IntentSender.SendIntentException e){
}
}else{
Log.e("GoogleFit", "AuthInProgress");
}
}
@Override
public void onDataPoint(DataPoint dataPoint) {
for(final Field field: dataPoint.getDataType().getFields()){
final Value value = dataPoint.getValue(field);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),"Field"+field.getName()+"Value:"+ value,Toast.LENGTH_SHORT).show();
}
});
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_OAUTH){
authInProgress = false;
if(resultCode == RESULT_OK){
if(!mApiClient.isConnecting() && !mApiClient.isConnected()){
mApiClient.connect();
}
}else if(resultCode == RESULT_CANCELED){
Log.e("GoogleFit", "RESULT_CANCELED");
}
}else {
Log.e("GoogleFit", "requestCode NOT request_oauth");
}
super.onActivityResult(requestCode, resultCode, data);
}
private void registerFitnessDataListener(DataSource dataSource, DataType dataType){
SensorRequest request = new SensorRequest.Builder()
.setDataSource(dataSource)
.setDataType(dataType)
.setSamplingRate(3, TimeUnit.SECONDS)
.build();
Fitness.SensorsApi.add(mApiClient, request, this)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
if(status.isSuccess()){
Log.e("GoogleFit", "SensorApi succesfully added");
}
}
});
}
@Override
protected void onStop() {
super.onStop();
Fitness.SensorsApi.remove(mApiClient, this)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
if(status.isSuccess()){
mApiClient.disconnect();
}
}
});
}
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
outState.putBoolean(AUTH_PENDING, authInProgress);
}
}
Make sure that you are using the correct OAuth 2.0 Client ID code. This may lead to an error if you're using it wrong. Also make sure that you have updated the authentication flow based from the latest documentation for Google Sign-In.
You may refer with these SO threads: Google Oauth 2.0 RESULT_CANCELED while using Google Fit api and GoogleApiClient not connecting while using google fit.