如何阅读谷歌适合消耗的卡路里和剩余卡路里

时间:2015-09-25 10:32:51

标签: android google-fit

如何阅读谷歌适合消耗的卡路里和剩余卡路里。直到现在我只能得到步数我还需要卡路里数据。我还将重量和高度插入到下面的命中api。

 public  void saveUserHeight(int heightCentimiters) {
    // to post data
    float height = ((float) heightCentimiters) / 100.0f;
    Calendar cal = Calendar.getInstance();
    Date now = new Date();
    cal.setTime(now);
    long endTime = cal.getTimeInMillis();
    cal.add(Calendar.DAY_OF_YEAR, -1);
    long startTime = cal.getTimeInMillis();

    DataSet heightDataSet = createDataForRequest(
            DataType.TYPE_HEIGHT,    // for height, it would be DataType.TYPE_HEIGHT
            DataSource.TYPE_RAW,
            height,                  // weight in kgs
            startTime,              // start time
            endTime,                // end time
            TimeUnit.MILLISECONDS                // Time Unit, for example, TimeUnit.MILLISECONDS
    );

    com.google.android.gms.common.api.Status heightInsertStatus =
            Fitness.HistoryApi.insertData(mGoogleApiFitnessClient, heightDataSet)
                    .await(1, TimeUnit.MINUTES);
    if(heightInsertStatus.isSuccess()){
        Log.e("Height","Inserted");
    }
    else{
        Log.e("Height","inserted failed");
    }
}

public  void saveUserWeight(float weight) {
    // to post data
    Calendar cal = Calendar.getInstance();
    Date now = new Date();
    cal.setTime(now);
    long endTime = cal.getTimeInMillis();
    cal.add(Calendar.DAY_OF_YEAR, -1);
    long startTime = cal.getTimeInMillis();

    DataSet weightDataSet = createDataForRequest(
            DataType.TYPE_WEIGHT,    // for height, it would be DataType.TYPE_HEIGHT
            DataSource.TYPE_RAW,
            weight,                  // weight in kgs
            startTime,              // start time
            endTime,                // end time
            TimeUnit.MILLISECONDS                // Time Unit, for example, TimeUnit.MILLISECONDS
    );

    com.google.android.gms.common.api.Status weightInsertStatus =
            Fitness.HistoryApi.insertData(mGoogleApiFitnessClient, weightDataSet)
                    .await(1, TimeUnit.MINUTES);
    if(weightInsertStatus.isSuccess()){
        Log.e("Weight","Inserted");
    }
    else{
        Log.e("Weight","inserted failed");
    }
}

为了获得今天的步数,我使用了这种方法。但是对于卡路里信息我找不到方法..所以如果有人知道请在这里分享..

 private void getStepsToday() {
    Calendar cal = Calendar.getInstance();
    Date now = 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;

    for (DataPoint dp : stepData.getDataPoints()) {
        for(Field field : dp.getDataType().getFields()) {
            int steps = dp.getValue(field).asInt();

            totalSteps += steps;

        }
    }

    publishTodaysStepData(totalSteps);
}

1 个答案:

答案 0 :(得分:1)

对于卡路里,您需要更改数据类型 即:

<script type="text/javascript">

var clientId  = 'a:myOrgId:'+Math.random().toString(16).substr(2, 8);
var client = new Messaging.Client("myOqgId.messaging.internetofthings.ibmcloud.com", 1883, clientId);

 //Gets  called if the websocket/mqtt connection gets disconnected for any reason
 client.onConnectionLost = function (responseObject) {
     //Depending on your scenario you could implement a reconnect logic here
     alert("connection lost: " + responseObject.errorMessage);
 };

 //Gets called whenever you receive a message for your subscriptions
 client.onMessageArrived = function (message) {
     //Do something with the push message you received
     $('#messages').append('<span>Topic: ' + message.destinationName + '  | ' + message.payloadString + '</span><br/>');
 };

 //Connect Options
 var options = {
     userName: API-Key here,
     password: Auth token here,
     timeout: 3,
     //Gets Called if the connection has sucessfully been established
     onSuccess: function () {
         alert("Connected");    	 
     },
     //Gets Called if the connection could not be established
     onFailure: function (message) {
         alert("Connection failed: " + message.errorMessage);
     }
 };

 //Creates a new Messaging.Message Object and sends it to the HiveMQ MQTT Broker
 var publish = function (payload, topic, qos) {
     //Send your message (also possible to serialize it as JSON or protobuf or just use a string, no limitations)
     var message = new Messaging.Message(payload);
     message.destinationName = topic;
     message.qos = qos;
     client.send(message);
 }
client.connect(options);
</script>

或:

DataType.TYPE_CALORIES_EXPENDED