您的应用可以读取和写入精细的睡眠数据。
其中包括浅度睡眠、深度睡眠、REM 和清醒睡眠阶段(从
SleepStages
枚举类型。如需写入睡眠数据,您必须创建一个类型的时段
FitnessActivities.SLEEP
。
如果睡眠数据要显示在用户的
中的日志
Google 健身应用。
(可选)在会话中插入 SleepStages
类型的细分:
SleepStages.SLEEP_LIGHT
SleepStages.SLEEP_DEEP
SleepStages.SLEEP_REM
SleepStages.AWAKE
SleepStages.OUT_OF_BED
非粒度示例
要编写没有阶段粒度的睡眠之夜,请遵循以下示例。
创建一个设有开始和结束时间以及 activity
SLEEP 的会话。
Android
val fitnessOptions = FitnessOptions.builder() .accessSleepSessions(FitnessOptions.ACCESS_WRITE) .build() // Create the sleep session val session= Session.Builder() .setName(sessionName) .setIdentifier(identifier) .setDescription(description) .setStartTime(startTime, TimeUnit.MILLISECONDS) .setEndTime(endTime, TimeUnit.MILLISECONDS) .setActivity(FitnessActivities.SLEEP) .build() // Build the request to insert the session. val request = SessionInsertRequest.Builder() .setSession(session) .build() // Insert the session into Fit platform Log.i(TAG, "Inserting the session with the SessionsClient") Fitness.getSessionsClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions)) .insertSession(request) .addOnSuccessListener { Log.i(TAG,"Session insert was successful!") } .addOnFailureListener { e -> Log.w(TAG, "There was a problem inserting the session", e) }
详细了解 Android 中的会话。
REST
HTTP 方法
PUT
Request URL
https://www.googleapis.com/fitness/v1/users/me/sessions/identifier
请求正文
{
"id": identifier,
"name": sessionName,
"description": description,
"startTimeMillis": startTime,
"endTimeMillis": endTime,
"version": 1,
"lastModifiedToken": "exampleToken",
"application": {
"detailsUrl": "http://example.com",
"name": "Foo Example App",
"version": "1.0"
},
"activityType": 72 // Sleep
}
睡眠阶段粒度示例
如需以阶段粒度写入睡眠,请同时写入顶级睡眠 不同睡眠阶段的时段和细分。
写入睡眠时段
在本示例中,我们将编写几个片段来表示若干睡眠 睡眠阶段的变化。
Android
val dataSource = DataSource.Builder() .setType(DataSource.TYPE_RAW) .setDataType(DataType.TYPE_SLEEP_SEGMENT) .setAppPackageName(context) // Optional but recommended for identifying the stream if you have multiple streams with the same dataType. .setStreamName(streamName) .build() val dataPoints = listOf( DataPoint.builder(dataSource) .setTimeInterval(startTime1, endTime1, TimeUnit.MILLISECONDS) .setField(Field.FIELD_SLEEP_SEGMENT_TYPE, SleepStages.SLEEP_LIGHT) .build(), DataPoint.builder(dataSource) .setTimeInterval(startTime2, endTime2, TimeUnit.MILLISECONDS) .setField(Field.FIELD_SLEEP_SEGMENT_TYPE, SleepStages.SLEEP_DEEP) .build(), DataPoint.builder(dataSource) .setTimeInterval(startTime3, endTime3, TimeUnit.MILLISECONDS) .setField(Field.FIELD_SLEEP_SEGMENT_TYPE, SleepStages.SLEEP_LIGHT) .build(), DataPoint.builder(dataSource) .setTimeInterval(startTime4, endTime4, TimeUnit.MILLISECONDS) .setField(Field.FIELD_SLEEP_SEGMENT_TYPE, SleepStages.SLEEP_REM) .build(), DataPoint.builder(dataSource) .setTimeInterval(startTime5, endTime5, TimeUnit.MILLISECONDS) .setField(Field.FIELD_SLEEP_SEGMENT_TYPE, SleepStages.AWAKE) .build(), DataPoint.builder(dataSource) .setTimeInterval(startTime6, endTime6, TimeUnit.MILLISECONDS) .setField(Field.FIELD_SLEEP_SEGMENT_TYPE, SleepStages.SLEEP_LIGHT) .build() ) val dataSet = DataSet.builder(dataSource) .addAll(dataPoints) .build()
REST
首先,创建
dataSource
:HTTP 方法
POST
Request URL
https://www.googleapis.com/fitness/v1/users/me/dataSources
请求正文
{ "dataStreamName": streamName, "type": "raw", "application": { "detailsUrl": "http://example.com", "name": "Foo Example App", "version": "1" }, "dataType": { "name": "com.google.sleep.segment" } }
然后填充
dataSet
:HTTP 方法
PATCH
Request URL
https://www.googleapis.com/fitness/v1/users/userId/dataSources/dataSourceId/datasets/datasetId
请求正文
{ "dataSourceId": dataSourceId, "point": [ { "dataTypeName": "com.google.sleep.segment", "startTimeNanos": startTime1, "endTimeNanos": endTime1, "value": [ { intVal: 4 // Light sleep } ] }, { "dataTypeName": "com.google.sleep.segment", "startTimeNanos": startTime2, "endTimeNanos": endTime2, "value": [ { intVal: 5 // Deep sleep } ] }, { "dataTypeName": "com.google.sleep.segment", "startTimeNanos": startTime3, "endTimeNanos": endTime3, "value": [ { intVal: 4 // Light sleep } ] }, { "dataTypeName": "com.google.sleep.segment", "startTimeNanos": startTime4, "endTimeNanos": endTime4, "value": [ { intVal: 6 // REM sleep } ] }, { "dataTypeName": "com.google.sleep.segment", "startTimeNanos": startTime5, "endTimeNanos": endTime5, "value": [ { intVal: 1 // Awake } ] }, { "dataTypeName": "com.google.sleep.segment", "startTimeNanos": startTime6, "endTimeNanos": endTime6, "value": [ { intVal: 4 // Light sleep } ] } ] }
写入睡眠时段数据
最后,通过插入睡眠时段对以上片段进行分组。设置起点 该会话的时长与第一个片段的开始时间和结束时间之间 最后一个片段的结束时间。
Android
val fitnessOptions = FitnessOptions.builder() .accessSleepSessions(FitnessOptions.ACCESS_WRITE) .addDataType(DataType.TYPE_SLEEP_SEGMENT, FitnessOptions.ACCESS_WRITE) .build() val session = Session.Builder() .setName(sessionName) .setIdentifier(identifier) .setDescription(description) .setStartTime(startTime1, TimeUnit.MILLISECONDS) // From first segment .setEndTime(endTime6, TimeUnit.MILLISECONDS) // From last segment .setActivity(FitnessActivities.SLEEP) .build() // Build the request to insert the session. val request = SessionInsertRequest.Builder() .setSession(session) .addDataSet(dataset) .build() // Insert the session into Fit platform Log.i(TAG, "Inserting the session in the Sessions API") Fitness.getSessionsClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions)) .insertSession(request) .addOnSuccessListener { Log.i(TAG,"Session insert was successful!") } .addOnFailureListener { e -> Log.i(TAG, "There was a problem inserting the session", e) }
REST
HTTP 方法
PUT
Request URL
https://www.googleapis.com/fitness/v1/users/me/sessions/identifier
请求正文
{
"id": identifier,
"name": sessionName,
"description": description,
"startTimeMillis": startTime1,
"endTimeMillis": endTime6,
"version": 1,
"lastModifiedToken": "exampleToken",
"application": {
"detailsUrl": "http://example.com",
"name": "Foo Example App",
"version": "1.0"
},
"activityType": 72 // Sleep
}