Insert TYPE_NUTRITION in Google Fit -
i user input (calorie) , want insert in google fit insertion not work.
private dataset insertnutritiondata(){ calendar cal = calendar.getinstance(); date = new date(); cal.settime(now); long endtime = cal.gettimeinmillis(); datasource nutritionsource = new datasource.builder() .setapppackagename(getapplicationcontext().getpackagename()) .setdatatype(datatype.type_nutrition) .settype(datasource.type_raw) .build(); dataset dataset = dataset.create(nutritionsource); datapoint datapoint = datapoint.create(nutritionsource); datapoint.settimestamp(endtime, timeunit.milliseconds); datapoint.getvalue(field.field_nutrients).setkeyvalue(field.nutrient_calories,calorie); dataset.add(datapoint); return dataset; }
the insertion done in asynctask :
private class insertandverifynutritiontask extends asynctask<void, void, void> { protected void doinbackground(void... params) { dataset dataset = insertnutritiondata(); log.i(tag, "inserting dataset in history api"); com.google.android.gms.common.api.status insertstatus = fitness.historyapi.insertdata(mclient, dataset) .await(1, timeunit.minutes); if (!insertstatus.issuccess()) { log.i(tag, "there problem inserting dataset."); return null; } log.i(tag, "data insert successful!"); return null; } }
unfortunately, insertion not done , don't know why. there no sample explain how can use type_nutrients...
thanks lot !
[update]
i found error :
couldn't connect google api client: connectionresult{statuscode=api_unavailable, resolution=null}
however, build client :
mclient = new googleapiclient.builder(this) .addapi(fitness.recording_api) .addapi(fitness.sensors_api) .addapi(fitness.history_api) .addscope(new scope(scopes.fitness_activity_read_write)) .addscope(new scope(scopes.fitness_nutrition_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, // you'll able determine reason , react here. mtryingtoconnect = false; 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) { //toast.maketext(getactivity(),"connection failed 1",toast.length_short).show(); mtryingtoconnect = false; notifyuifailedconnection(result); } } ) .build(); }
moreover, don't understand why cannot connect fit whereas worked perfectly...
updated manifest :
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.webear.mysilhouette"> <uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="android.permission.read_external_storage" /> <uses-permission android:name="android.permission.internet"/> <uses-permission android:name="android.permission.access_network_state"/> <application android:allowbackup="true" android:label="mysilhouette" android:icon="@drawable/ic_launcher" > <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <service android:enabled="true" android:name="com.example.webear.mysilhouette.googleapiintentservice"/> (...) </application> </manifest>
kamel
the statuscode
says api_unavailable
. means:
one of api components attempted connect not available. api not work on device, , updating google play services not solve problem. using api on device should avoided.
maybe try device? or play services misconfigured.
Comments
Post a Comment