Android - Google Fit - Get user's speed -
i'm building app needs user's current speed using google fit's android api. api finds datatype.type_speed without problem, using finddatasources method, speed value never gets returned. code follows:
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 fitness api fitness.sensorsapi.finddatasources(mclient, new datasourcesrequest.builder() // @ least 1 datatype must specified. .setdatatypes(datatype.type_speed) .build()) .setresultcallback(new resultcallback<datasourcesresult>() { @override public void onresult(datasourcesresult datasourcesresult) { string text = ""; (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 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"); } } } ); }
what doing wrong? thank you
i not of here, setadd after .setdatatypes
.setdatasourcetypes(datasource.type_derived)
and make sure got permissions right
.addapi(fitness.sensors_api) .addscope(new scope(scopes.fitness_location_read))
when creating api client object. in either case speed not common data source, instead use distance , divide time accordingly.
Comments
Post a Comment