Google API client getting disconnected
-
I am trying to establish a connection between google api client from my android source. i have the followed all the necessary procedures like getting google play services library , mentioning it in application tag (manifest file), created a keystore using and got the SHA1 using keytool . Created a Project in Google console, enabled Fitness APi and mentioned the SHA1 and package name Client OAUTH 2 id. Still I am getting status error code 4 . Kindly help me.
Please find attached my Android Manifest file and java file.
Manifest.
<?xml version="1.0"?>
<manifest package="package-name" xmlns:android="http://schemas.android.com/apk/res/android" android:versionName="1.0" android:versionCode="1" android:installLocation="auto">
<application android:name="org.qtproject.qt5.android.bindings.QtApplication" android:label="@string/app_name">
<activity android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|locale|fontScale|keyboard|keyboardHidden|navigation" android:name="package-name" android:label="@string/app_name" android:screenOrientation="unspecified">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<meta-data android:name="android.app.lib_name" android:value="-- %%INSERT_APP_LIB_NAME%% --"/>
<meta-data android:name="android.app.qt_sources_resource_id" android:resource="@array/qt_sources"/>
<meta-data android:name="android.app.repository" android:value="default"/>
<meta-data android:name="android.app.qt_libs_resource_id" android:resource="@array/qt_libs"/>
<meta-data android:name="android.app.bundled_libs_resource_id" android:resource="@array/bundled_libs"/><!-- Deploy Qt libs as part of package --> <meta-data android:name="android.app.bundle_local_qt_libs" android:value="-- %%BUNDLE_LOCAL_QT_LIBS%% --"/> <meta-data android:name="android.app.bundled_in_lib_resource_id" android:resource="@array/bundled_in_lib"/> <meta-data android:name="android.app.bundled_in_assets_resource_id" android:resource="@array/bundled_in_assets"/> <!-- Run with local libs --> <meta-data android:name="android.app.use_local_qt_libs" android:value="-- %%USE_LOCAL_QT_LIBS%% --"/> <meta-data android:name="android.app.libs_prefix" android:value="/data/local/tmp/qt/"/> <meta-data android:name="android.app.load_local_libs" android:value="-- %%INSERT_LOCAL_LIBS%% --"/> <meta-data android:name="android.app.load_local_jars" android:value="-- %%INSERT_LOCAL_JARS%% --"/> <meta-data android:name="android.app.static_init_classes" android:value="-- %%INSERT_INIT_CLASSES%% --"/> <!-- Messages maps --> <meta-data android:value="@string/ministro_not_found_msg" android:name="android.app.ministro_not_found_msg"/> <meta-data android:value="@string/ministro_needed_msg" android:name="android.app.ministro_needed_msg"/> <meta-data android:value="@string/fatal_error_msg" android:name="android.app.fatal_error_msg"/> <!-- Messages maps --> <!-- Splash screen --> <meta-data android:name="android.app.splash_screen" android:resource="@layout/splash"/> <!-- Splash screen --> </activity>
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
</application><supports-screens android:largeScreens="true" android:normalScreens="true" android:anyDensity="true" android:smallScreens="true"/> <!-- %%INSERT_PERMISSIONS --> <!--uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/--> <!-- %%INSERT_FEATURES -->
<!--uses-permission android:name="android.permission.ACCESS_CHECKIN_PROPERTIES"/-->
<!--uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.USE_CREDENTIALS"/>
<uses-sdk android:minSdkVersion="22"/></manifest>
Java File
package packagename;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.Scopes;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.Scope;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.fitness.Fitness;
import com.google.android.gms.fitness.data.Bucket;
import com.google.android.gms.fitness.data.DataPoint;
import com.google.android.gms.fitness.data.DataSet;
import com.google.android.gms.fitness.data.DataSource;
import com.google.android.gms.fitness.data.DataType;
import com.google.android.gms.fitness.data.Field;
import com.google.android.gms.fitness.request.DataDeleteRequest;
import com.google.android.gms.fitness.request.DataReadRequest;
import com.google.android.gms.fitness.request.DataUpdateRequest;
import com.google.android.gms.fitness.result.DailyTotalResult;
import com.google.android.gms.fitness.result.DataReadResult;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.qtproject.qt5.android.bindings.QtApplication;
import org.qtproject.qt5.android.bindings.QtActivity;public class TestAndroidClient extends QtActivity implements ConnectionCallbacks,
OnConnectionFailedListener {private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 1000; // Google client to interact with Google API private GoogleApiClient mGoogleApiClient; private static String str; private String s1,s2,s3,s4; ArrayList<String> ar = new ArrayList<String>(); public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Building the GoogleApi client buildGoogleApiClient(); } protected synchronized void buildGoogleApiClient() { mGoogleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(Fitness.HISTORY_API).build(); new ViewWeekStepCountTask().execute(); } @Override protected void onStart() { super.onStart(); if (mGoogleApiClient != null) { System.out.println("google api connected"); mGoogleApiClient.connect(); } } @Override protected void onResume() { super.onResume(); } @Override public void onConnectionFailed(ConnectionResult result) { System.out.println( "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode()); } @Override public void onConnected(Bundle arg0) { // Once connected with google api, get the location System.out.println("connected"); } @Override public void onConnectionSuspended(int arg0) { mGoogleApiClient.connect(); } /* Google Fit Logic */ private class ViewWeekStepCountTask extends AsyncTask<String, Void, DataReadResult> { DataReadResult dataReadResult; public DataReadResult doInBackground(String... params) { Calendar cal = Calendar.getInstance(); Date now = new Date(); cal.setTime(now); long endTime = cal.getTimeInMillis(); cal.add(Calendar.WEEK_OF_YEAR, -1); long startTime = cal.getTimeInMillis(); java.text.DateFormat dateFormat = DateFormat.getDateInstance(); // Log.e("History", "Range Start: " + dateFormat.format(startTime)); // Log.e("History", "Range End: " + dateFormat.format(endTime)); //Check how many steps were walked and recorded in the last 7 days DataReadRequest readRequest = new DataReadRequest.Builder() .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA) .bucketByTime(1, TimeUnit.DAYS) .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS) .build(); dataReadResult = Fitness.HistoryApi.readData(mGoogleApiClient, readRequest).await(1, TimeUnit.MINUTES); return dataReadResult; } protected void onPostExecute(DataReadResult dataReadResult) { super.onPostExecute(dataReadResult); if (dataReadResult.getBuckets().size() > 0) { // Log.e("History", "Number of buckets: " + dataReadResult.getBuckets().size()); for (Bucket bucket : dataReadResult.getBuckets()) { List<DataSet> dataSets = bucket.getDataSets(); for (DataSet dataSet : dataSets) { showDataSet(dataSet); } } } //Used for non-aggregated data else if (dataReadResult.getDataSets().size() > 0) { // Log.e("History", "Number of returned DataSets: " + dataReadResult.getDataSets().size()); for (DataSet dataSet : dataReadResult.getDataSets()) { showDataSet(dataSet); } } } private void showDataSet(DataSet dataSet) { Calendar cal = Calendar.getInstance(); Date now = new Date(); cal.setTime(now); long endTime = cal.getTimeInMillis(); cal.add(Calendar.WEEK_OF_YEAR, -1); long startTime = cal.getTimeInMillis(); java.text.DateFormat dateFormat = DateFormat.getDateInstance(); // tv1.setText( "Range Start: " + dateFormat.format(startTime)); // tv2.setText( "Range End: " + dateFormat.format(endTime)); // Log.e("History", "Data returned for Data type: " + dataSet.getDataType().getName()); DateFormat dateFormat1 = DateFormat.getDateInstance(); DateFormat timeFormat = DateFormat.getTimeInstance(); for (DataPoint dp : dataSet.getDataPoints()) { s1 = "\tType: " + dp.getDataType().getName().toString(); s2 ="\tStart: " + dateFormat1.format(dp.getStartTime(TimeUnit.MILLISECONDS)) + " " + timeFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)).toString(); s3 = "\tEnd: " + dateFormat1.format(dp.getEndTime(TimeUnit.MILLISECONDS)) + " " + timeFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)).toString(); for(Field field : dp.getDataType().getFields()) { s4 ="\tField: " + field.getName() + " Value: " + dp.getValue(field).toString(); str = "hi"; // hello(5); //System.out.println(str); //ar.add(s1); ar.add(s2); ar.add(s3); ar.add(s4);
// for(int i = 0; i < ar.size(); i++) {
// System.out.println(ar.get(i));
// }
// tv1.setText("");
// for (int j = 0; j < ar.size(); j++){
// tv1.append( ar.get(j) + "\n");
// }} } } } private static String printHelloWorld(int a){ return str; }
}
Application Output
0x5585b6b310,id:1561,api:2,p:3844,c:376) new GraphicBuffer needed
W/HTCLOG ( 3844): use specified tag [libEGL], func [3].
W/HTCLOG ( 3844): mask=0x18
E/GED ( 3844): Failed to get GED Log Buf, err(0)
W/HTCLOG ( 3844): use specified tag [GraphicBufferMapper], func [3].
W/HTCLOG ( 3844): mask=0x18
W/HTCLOG ( 3844): use specified tag [GraphicBuffer], func [3].
W/HTCLOG ( 3844): mask=0x18
W/HTCLOG ( 3844): use specified tag [GraphicBufferExtra], func [0].
W/HTCLOG ( 3844): mask=0x18
I/System.out( 3844): Connection failed: ConnectionResult.getErrorCode() = 4
**