讀取睡眠資料

睡眠以時段表示 類型為 sleep。 時段可選擇包含睡眠階段,這些階段較為精細 關於睡眠資料例如淺睡、熟睡或 REM 睡眠 睡眠:

睡眠階段值
睡眠階段類型
清醒 (睡眠週期內) 1
睡眠 2
戶外床 3
淺層睡眠 4
深層睡眠 5
快速動眼期 6

寫入睡眠資料」指南會說明 精細和非精細的睡眠資料會顯示在 Fit 中。

Android

下列範例使用 SessionClient 從 Fit 擷取資料。

val SLEEP_STAGE_NAMES = arrayOf(
    "Unused",
    "Awake (during sleep)",
    "Sleep",
    "Out-of-bed",
    "Light sleep",
    "Deep sleep",
    "REM sleep"
)

val request = SessionReadRequest.Builder()
    .readSessionsFromAllApps()
    // By default, only activity sessions are included, so it is necessary to explicitly
    // request sleep sessions. This will cause activity sessions to be *excluded*.
    .includeSleepSessions()
    // Sleep segment data is required for details of the fine-granularity sleep, if it is present.
    .read(DataType.TYPE_SLEEP_SEGMENT)
    .setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS)
    .build()

sessionsClient.readSession(request)
    .addOnSuccessListener { response ->
        for (session in response.sessions) {
            val sessionStart = session.getStartTime(TimeUnit.MILLISECONDS)
            val sessionEnd = session.getEndTime(TimeUnit.MILLISECONDS)
            Log.i(TAG, "Sleep between $sessionStart and $sessionEnd")

            // If the sleep session has finer granularity sub-components, extract them:
            val dataSets = response.getDataSet(session)
            for (dataSet in dataSets) {
                for (point in dataSet.dataPoints) {
                    val sleepStageVal = point.getValue(Field.FIELD_SLEEP_SEGMENT_TYPE).asInt()
                    val sleepStage = SLEEP_STAGE_NAMES[sleepStageVal]
                    val segmentStart = point.getStartTime(TimeUnit.MILLISECONDS)
                    val segmentEnd = point.getEndTime(TimeUnit.MILLISECONDS)
                    Log.i(TAG, "\t* Type $sleepStage between $segmentStart and $segmentEnd")
                }
            }
        }
    }

REST

使用 REST API 擷取睡眠時段分為兩個階段:

  1. 擷取工作階段清單activityType 參數設為 72 (SLEEP)。 注意:您可以使用 startTimeendTime 或使用 pageToken ,擷取自上次要求以來的新工作階段。

    HTTP 方法

    GET
    

    要求網址

    https://www.googleapis.com/fitness/v1/users/me/sessions?startTime=2019-12-05T00:00.000Z&endTime=2019-12-17T23:59:59.999Z&activityType=72
    

    回應

    工作階段範例 可能是:

    {
     "session": [
        {
         "id": "Sleep1575505620000",
         "name": "Sleep",
         "description": "",
         "startTimeMillis": "1575505620000",
         "endTimeMillis": "1575526800000",
         "modifiedTimeMillis": "1575590432413",
         "application": {
          "packageName": "com.example.sleep_tracker"
         },
         "activityType": 72  // Sleep
        },
        {
         "id": "Run2939075083",
         "name": "Mud",
         "description": "",
         "startTimeMillis": "1576594403000",
         "endTimeMillis": "1576598754000",
         "modifiedTimeMillis": "1576616010143",
         "application": {
          "packageName": "com.example.run_tracker"
         },
         "activityType": 8  // Running
        }
      ],
     "deletedSession": [],
     "nextPageToken": "1576598754001"
    }
    
  2. 如要取得每個課程的睡眠階段詳細資料 (如有),請使用 。

    HTTP 方法

    POST
    

    要求網址

    https://www.googleapis.com/fitness/v1/users/userId/dataset:aggregate
    

    要求主體

    {
      "aggregateBy": [
        {
          "dataTypeName": "com.google.sleep.segment"
        }
      ],
      "endTimeMillis": 1575609060000,
      "startTimeMillis": 1575591360000
    }
    

    回應

    如果要求成功,您會收到 200 OK HTTP 回應狀態 再也不是件繁重乏味的工作回應主體包含活動的 JSON 表示法 構成睡眠時段的區段每個 intVal 都代表 睡眠活動類型

    {
     "bucket": [
      {
       "startTimeMillis": "1575591360000",
       "endTimeMillis": "1575609060000",
       "dataset": [
        {
         "point": [
          {
           "startTimeNanos": "1575591360000000000",
           "endTimeNanos": "1575595020000000000",
           "dataTypeName": "com.google.sleep.segment",
           "originDataSourceId": "...",
           "value": [
            {
             "intVal": 4, // Light sleep
             "mapVal": []
            }
           ]
          },
          {
           "startTimeNanos": "1575595020000000000",
           "endTimeNanos": "1575596220000000000",
           "dataTypeName": "com.google.sleep.segment",
           "originDataSourceId": "...",
           "value": [
            {
             "intVal": 1, // Sleep
             "mapVal": []
            }
           ]
          },
    
          // .... more datapoints
    
          {
           "startTimeNanos": "1575605940000000000",
           "endTimeNanos": "1575609060000000000",
           "dataTypeName": "com.google.sleep.segment",
           "originDataSourceId": "...",
           "value": [
            {
             "intVal": 4, // Light sleep
             "mapVal": []
            }
           ]
          }
         ]
        }
       ]
      }
     ]
    }