睡眠由时段表示
类型为 sleep
。
时段可以选择包含睡眠阶段,睡眠阶段具有更精细的细节
关于睡眠数据例如,如果是浅睡眠、深睡眠或 REM
休眠:
睡眠阶段值
睡眠阶段类型 | 值 |
---|---|
清醒(睡眠周期期间) | 1 |
睡眠 | 2 |
床外 | 3 |
浅层睡眠 | 4 |
深层睡眠 | 5 |
快速眼动睡眠 | 6 |
写入睡眠数据指南介绍了 Google 健身会显示精细的和非精细的睡眠数据。
Android
以下示例使用 SessionClient 从 Google 健身检索数据。
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 检索睡眠时段的过程分为两个阶段:
检索会话列表 将
activityType
参数设为72
(SLEEP
)。 注意:您可以使用startTime
和endTime
,也可以使用 pageToken 检索自上次请求之后的新会话。HTTP 方法
GET
Request URL
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 示例 可能是:
{ "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" }
如要获得每个时段的睡眠阶段详细信息(如果存在),请使用 针对已过滤列表中的每个会话发出以下请求:
HTTP 方法
POST
Request URL
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": [] } ] } ] } ] } ] }