讀取睡眠資料

透過集合功能整理內容 你可以依據偏好儲存及分類內容。

睡眠以sleep類型的工作階段表示。工作階段可選擇是否包含睡眠階段,其中包含更詳細的睡眠資料詳細資料。例如,淺睡、熟睡或 REM 睡眠:

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

寫入睡眠資料指南說明 Fit 和非顆粒睡眠資料在 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 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": []
            }
           ]
          }
         ]
        }
       ]
      }
     ]
    }