Reputation: 456
I tried this using java
String AccessToken = "TOKEN";
String ApiUrl = "https://www.googleapis.com/fitness/v1/users/me/sessions?startTime=2017-02-28T22:00:00.00Z&endTime=2017-03-01T10:59:59.99Z";
HttpClient httpclient = HttpClientBuilder.create().build();
try {
HttpGet httpPost = new HttpGet(ApiUrl);
httpPost.addHeader("Authorization", "Bearer " + AccessToken);
HttpResponse response = httpclient.execute(httpPost);
`enter code here` System.out.println("\nSending 'GET' request to URL : " + ApiUrl);
HttpEntity entity = response.getEntity();
BufferedReader rd = new BufferedReader(
new InputStreamReader(entity.getContent()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println("line" + line);
}
} catch (Exception e) {
System.out.println("Exception at getDataFromUrl ,error is " + e.getMessage());
}
And I am getting response like
{
"session": [
{
"id": "Deep sleep141488319560000",
"name": "Deep sleep14",
"startTimeMillis": "1488319560000",
"endTimeMillis": "1488320700000",
"modifiedTimeMillis": "1488374094270",
"application": {
"packageName": "com.xiaomi.hm.health"
},
"activityType": 72
},
{
"id": "Deep sleep161488321420000",
"name": "Deep sleep16",
"startTimeMillis": "1488321420000",
"endTimeMillis": "1488322500000",
"modifiedTimeMillis": "1488374094280",
"application": {
"packageName": "com.xiaomi.hm.health"
},
"activityType": 72
},
{
"id": "Deep sleep201488328680000",
"name": "Deep sleep20",
"startTimeMillis": "1488328680000",
"endTimeMillis": "1488330360000",
"modifiedTimeMillis": "1488374094303",
"application": {
"packageName": "com.xiaomi.hm.health"
},
"activityType": 72
},
{
"id": "Light sleep131488318900000",
"name": "Light sleep13",
"startTimeMillis": "1488318900000",
"endTimeMillis": "1488319560000",
"modifiedTimeMillis": "1488374094265",
"application": {
"packageName": "com.xiaomi.hm.health"
},
"activityType": 72
}]}
But i want get total sleep e.g 7 hours 10 mins
Is it correct API I am using or am i missing something.
Any help appreciated. I am new to Google Fit.
Note:This is java not Android.
Upvotes: 2
Views: 1756
Reputation: 11
You must calculate the sleep duration on your own by computing the difference between "endTimeMillis" and "startTimeMillis" and summerize these computed values in your requested range. That’s easy, but also tricky as the stored data may differ from the source (the app that sends the data to google fit).
I also run in this issue and did not find a satisfying solution yet that is working correct. Since sleep data it is not a "real" data type (see https://developers.google.com/fit/rest/v1/data-types#public_data_types) you cannot be sure that an app sends duplicate values within the same requested range. In my case the app "xiaomi mi-fit" sends duplicate values with the same "startTimeMillis" and "endTimeMillis" so you must check this in your code. Otherwise you will be getting a sleep duration that is not accurate.
As long as Google did not support sleep data as an official data type you cannot be sure that the data is correct. Moreover: When different apps send their sleep data to the same google fit account the data will not be aggregated, you will have to sort this on your own by filtering the package name (app) that sends the data.
Also, Google removed the visibility of sleep data in their own native app (version 2.x) and in their WebView (https://fit.google.com) too!
Upvotes: 1