service - GoogleFit wasn't able to connect, so the request failed-Android -
i have following code 2 main things.
- it connects google play services.
- inside onconnected() method service started, calling startservice(...) method.
when run program following log message.
connected!!! fit wasn't able connect, request failed. googlefitservice destroyed.
here code:
public class mainactivity extends actionbaractivity { public final static string tag = "googlefitservice"; private static final int request_oauth = 1; private static final string auth_pending = "auth_state_pending"; private boolean authinprogress = false; private googleapiclient mclient = null; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); buildfitnessclient(); } private void buildfitnessclient() { // create google api client mclient = new googleapiclient.builder(this) .addapi(fitness.api) .addconnectioncallbacks( new googleapiclient.connectioncallbacks() { @override public void onconnected(bundle bundle) { log.i(tag, "connected!!!"); // can make calls fitness apis. // put application specific code here. intent service = new intent(mainactivity.this, googlefitservice.class); service.putextra(googlefitservice.service_request_type, googlefitservice.type_request_connection); startservice(service); } @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 api client fails connect. @override public void onconnectionfailed(connectionresult result) { log.i(tag, "connection failed. cause: " + result.tostring()); if (!result.hasresolution()) { // show localized error dialog googleplayservicesutil.geterrordialog(result.geterrorcode(), mainactivity.this, 0).show(); return; } if (!authinprogress) { try { log.i(tag, "attempting 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(); } @override protected void onstart() { super.onstart(); // connect fitness api log.i(tag, "connecting..."); mclient.connect(); intent service = new intent(this, googlefitservice.class); service.putextra(googlefitservice.service_request_type, googlefitservice.type_request_connection); startservice(service); } @override protected void onstop() { super.onstop(); if (mclient.isconnected()) { mclient.disconnect(); } } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == request_oauth) { authinprogress = false; if (resultcode == result_ok) { // make sure app not connected or attempting connect if (!mclient.isconnecting() && !mclient.isconnected()) { mclient.connect(); } } } } @override protected void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); outstate.putboolean(auth_pending, authinprogress); } }
and second class intent service. maybe mistake here.
public class googlefitservice extends intentservice { public static final string tag = "googlefitservice"; private googleapiclient mgoogleapifitnessclient; private boolean mtryingtoconnect = false; public static final string service_request_type = "requesttype"; public static final int type_get_step_today_data = 1; public static final int type_request_connection = 2; public static final string history_intent = "fithistory"; public static final string history_extra_steps_today = "stepstoday"; public static final string fit_notify_intent = "fitstatusupdateintent"; public static final string fit_extra_connection_message = "fitfirstconnection"; public static final string fit_extra_notify_failed_status_code = "fitextrafailedstatuscode"; public static final string fit_extra_notify_failed_intent = "fitextrafailedintent"; @override public void ondestroy() { log.d(tag, "googlefitservice destroyed"); if (mgoogleapifitnessclient.isconnected()) { log.d(tag, "disconecting google fit."); mgoogleapifitnessclient.disconnect(); } super.ondestroy(); } @override public void oncreate() { super.oncreate(); buildfitnessclient(); log.d(tag, "googlefitservice created"); } public googlefitservice() { super("googlefitservice"); } @override protected void onhandleintent(intent intent) { //get request type int type = intent.getintextra(service_request_type, 1); //block until google fit connects. give after 10 seconds. if (!mgoogleapifitnessclient.isconnected()) { mtryingtoconnect = true; mgoogleapifitnessclient.connect(); //wait until service either connects or fails connect while (mtryingtoconnect) { try { thread.sleep(100, 0); } catch (interruptedexception e) { e.printstacktrace(); } } } if (mgoogleapifitnessclient.isconnected()) { if (type == type_get_step_today_data) { log.d(tag, "requesting steps google fit"); getstepstoday(); log.d(tag, "fit update complete. allowing android destroy service."); } else if (type == type_request_connection) { } } else { //not connected log.w(tag, "fit wasn't able connect, request failed."); } } private void getstepstoday() { calendar cal = calendar.getinstance(); date = new date(); cal.settime(now); long endtime = cal.gettimeinmillis(); cal.set(calendar.hour_of_day, 0); cal.set(calendar.minute, 0); cal.set(calendar.second, 0); long starttime = cal.gettimeinmillis(); final datareadrequest readrequest = new datareadrequest.builder() .read(datatype.type_step_count_delta) .settimerange(starttime, endtime, timeunit.milliseconds) .build(); datareadresult datareadresult = fitness.historyapi.readdata(mgoogleapifitnessclient, readrequest).await(1, timeunit.minutes); dataset stepdata = datareadresult.getdataset(datatype.type_step_count_delta); int totalsteps = 0; (datapoint dp : stepdata.getdatapoints()) { (field field : dp.getdatatype().getfields()) { int steps = dp.getvalue(field).asint(); totalsteps += steps; } } publishtodaysstepdata(totalsteps); } private void publishtodaysstepdata(int totalsteps) { intent intent = new intent(history_intent); // can include data. intent.putextra(history_extra_steps_today, totalsteps); localbroadcastmanager.getinstance(this).sendbroadcast(intent); } private void buildfitnessclient() { // create google api client mgoogleapifitnessclient = new googleapiclient.builder(this) .addapi(fitness.api) .addscope(new scope(scopes.fitness_body_read_write)) .addscope(new scope(scopes.fitness_activity_read_write)) .addscope(new scope(scopes.fitness_location_read_write)) .addconnectioncallbacks( new googleapiclient.connectioncallbacks() { @override public void onconnected(bundle bundle) { log.i(tag, "google fit connected."); mtryingtoconnect = false; log.d(tag, "notifying ui we're connected."); notifyuifitconnected(); } @override public void onconnectionsuspended(int i) { // if connection sensor gets lost @ point, mtryingtoconnect = false; if (i == googleapiclient.connectioncallbacks.cause_network_lost) { log.i(tag, "google fit connection lost. cause:network lost."); } else if (i == googleapiclient.connectioncallbacks.cause_service_disconnected) { log.i(tag, "google fit connection lost. reason:service disconnected "); } } } ) .addonconnectionfailedlistener( new googleapiclient.onconnectionfailedlistener() { // called whenever api client fails connect. @override public void onconnectionfailed(connectionresult result) { mtryingtoconnect = false; notifyuifailedconnection(result); } } ) .build(); } private void notifyuifitconnected() { intent intent = new intent(fit_notify_intent); intent.putextra(fit_extra_connection_message, fit_extra_connection_message); localbroadcastmanager.getinstance(this).sendbroadcast(intent); } private void notifyuifailedconnection(connectionresult result) { intent intent = new intent(fit_notify_intent); intent.putextra(fit_extra_notify_failed_status_code, result.geterrorcode()); intent.putextra(fit_extra_notify_failed_intent, result.getresolution()); localbroadcastmanager.getinstance(this).sendbroadcast(intent); } }
any ideas might went wrong? thanks, theo.
you need access detail fit when connected .and try
public void buildfitnessclient() { fitnessclient = new googleapiclient.builder(context) .addapi(fitness.history_api) .addapi(fitness.sessions_api) .addapi(fitness.recording_api) .addscope(new scope(scopes.fitness_body_read_write)) .addscope(new scope(scopes.fitness_activity_read_write)) .addscope(new scope(scopes.fitness_location_read_write)) .addscope(new scope(scopes.fitness_nutrition_read_write)) .addconnectioncallbacks( new googleapiclient.connectioncallbacks() { @override public void onconnected(bundle bundle) { referencewrapper.getinstance(context).setapiclient(fitnessclient); ((onclientconnectlistener) context).onclientconnected(); } @override public void onconnectionsuspended(int i) { } }) .addonconnectionfailedlistener( new googleapiclient.onconnectionfailedlistener() { @override public void onconnectionfailed( connectionresult result) { if (!result.hasresolution()) { googleplayservicesutil.geterrordialog( result.geterrorcode(), context, 0) .show(); return; } if (!authinprogress) { try { authinprogress = true; result.startresolutionforresult( context, keyconstant.request_oauth); } catch (intentsender.sendintentexception e) { } } } }).build(); }
and @ first need register application on google api console . generate outh2 client id there , enable fitness api . search fitness api in api dashboard , , enable it.. , override onactivityresult in activity .
@override public void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode == keyconstant.request_oauth) { fitnesshelper.setauthinprogress(false); if (resultcode == activity.result_ok) { if (!fitnesshelper.isconnecting() && !fitnesshelper.isconnected()) { fitnesshelper.connect(); } } } }
Comments
Post a Comment