寫入睡眠資料

您的應用程式可以讀取及寫入精細的睡眠資料。 包括淺層睡眠、深層睡眠、REM 睡眠和醒來的睡眠階段 SleepStages敬上 列舉的型別。如要寫入睡眠資料,請建立類型時段的時段 FitnessActivities.SLEEP。 如果睡眠資料出現在使用者的睡眠中,就必須封裝在工作階段中 日誌 Google Fit 應用程式

視需要在工作階段中插入 SleepStages 類型的區隔:

無精細程度範例

若要寫入沒有階段精細程度的睡眠,請遵循以下範例。 建立包含開始、結束時間和activity睡眠的工作階段。

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)
    }

進一步瞭解 工作階段

REST

HTTP 方法

PUT

要求網址

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

  1. 首先,請建立 dataSource

    HTTP 方法

    POST
    

    要求網址

    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"
      }
    }
    
  2. 然後填入 dataSet

    HTTP 方法

    PATCH
    

    要求網址

    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

要求網址

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
}