简体   繁体   中英

Getting sleep data from the Google fit API

I've been trying for the last few days to get Sleep information from the google fit API.

I've managed to get data from the history API and the session API for the time period, but the activity list doesn't seem to include any sleep data.

I've been capturing the data in "Sleep as Android" and I've also tried "Instant". But don't see anything in the data sets coming back.

I seem to remember with the older fit app I'd see the sleep activity shown, but there's no sign of the data in the journal section of the new app.

Should I be able to get the data from the history or session data?

At the moment I'm using

DataReadRequest readRequest = new DataReadRequest.Builder()
   .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
   .read(DataType.TYPE_ACTIVITY_SEGMENT)
   .build();

Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(context))
   .readData(readRequest)
   .addOnSuccessListener(new OnSuccessListener<DataReadResponse>(){
       @Override
       public void onSuccess(DataReadResponse dataReadResponse) {
           List<DataSet> dataSets = dataReadResponse.getDataSets();
           for (DataSet dataSet : dataSets) {
                for (DataPoint dp : dataSet.getDataPoints()) {
                   for (Field field : dp.getDataType().getFields()){
                      LogUtil.d("google fit Start: " + dateFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)) + " " + timeFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)) + " End: " + dateFormat.format(dp.getEndTime(TimeUnit.MILLISECONDS)) + " " + timeFormat.format(dp.getEndTime(TimeUnit.MILLISECONDS)) + " type: " + showDataType(dp.getValue(field)));
                  }
              }
          }
      }
  }

I'm not sure what I'm missing to get at the correct data. I see walking, still, running etc, but just no sleep!

You can try to send a session request that will read the dataType.TYPE_ACTIVITY_SEGMENT or used the Users.sessions methods to correctly record fitness data the sleep activities .

List of activities:

  • Sleeping 72
  • Light sleep 109
  • Deep sleep 110
  • REM sleep 111

Here is the sample code from Martin Stava that will show you on how to use Sleep as Anroid data from Google Fit. Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM