Tulis Data Tidur

Aplikasi Anda dapat membaca dan menulis data tidur terperinci. Ini termasuk fase tidur ringan, tidur nyenyak, REM, dan tidur terbangun dari SleepStages yang terenumerasi. Untuk menulis data tidur, Anda harus membuat jenis sesi FitnessActivities.SLEEP Data tidur harus dienkapsulasi dalam sebuah sesi jika ingin muncul dalam Jurnal dalam Aplikasi Google Fit.

Jika perlu, masukkan segmen jenis SleepStages dalam sesi:

Contoh tanpa perincian

Untuk menulis tidur malam tanpa perincian tahapan, ikuti contoh di bawah ini. Buat sesi dengan waktu mulai dan berakhir, serta TIDUR 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)
    }

Baca detail selengkapnya tentang sesi di Android.

REST

Metode HTTP

PUT

URL Permintaan

https://www.googleapis.com/fitness/v1/users/me/sessions/identifier

Isi permintaan

{
    "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
}

Contoh perincian fase tidur

Untuk menulis tidur dengan tahapan perincian, tulis kedua tidur tingkat teratas sesi dan segmen untuk berbagai tahapan tidur.

Tulis segmen tidur

Dalam contoh ini kita akan menulis beberapa segmen untuk mewakili beberapa jenis tidur tahapan selama satu malam tidur.

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. Pertama-tama, buat dataSource:

    Metode HTTP

    POST
    

    URL Permintaan

    https://www.googleapis.com/fitness/v1/users/me/dataSources
    

    Isi permintaan

    {
      "dataStreamName": streamName,
      "type": "raw",
      "application": {
        "detailsUrl": "http://example.com",
        "name": "Foo Example App",
        "version": "1"
      },
      "dataType": {
        "name": "com.google.sleep.segment"
      }
    }
    
  2. Kemudian isi dataSet:

    Metode HTTP

    PATCH
    

    URL Permintaan

    https://www.googleapis.com/fitness/v1/users/userId/dataSources/dataSourceId/datasets/datasetId
    

    Isi permintaan

    {
        "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
                    }
                ]
            }
         ]
     }
    

Tulis sesi tidur

Terakhir, kelompokkan segmen di atas dengan memasukkan sesi tidur. Tetapkan waktu mulai waktu untuk sesi ini hingga waktu mulai segmen pertama, dan waktu berakhir ke waktu berakhir dari segmen terakhir.

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

Metode HTTP

PUT

URL Permintaan

https://www.googleapis.com/fitness/v1/users/me/sessions/identifier

Isi permintaan

{
    "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
}