Sessions and datapoints are being returned null from SessionsClient of google fit

101 views Asked by At

I am trying to read sleep data from Google fit but always receiving empty/null sessions and data points. I took reference from multiple resources but nothing helped me. Below is my code snippet to read sleep data from google fit.

Please let me know what's going wrong in my code and what has to be done.

 val calendar = Calendar.getInstance(Locale.getDefault())
    val now = Date()

    calendar.time = now
    calendar.set(Calendar.HOUR_OF_DAY, 23)
    calendar.set(Calendar.MINUTE, 59)
    calendar.set(Calendar.SECOND, 59)
    val endTime = calendar.timeInMillis

    calendar.add(Calendar.MONTH, -6)
    calendar.set(Calendar.HOUR_OF_DAY, 0)
    calendar.set(Calendar.MINUTE, 0)
    calendar.set(Calendar.SECOND, 0)
    val startTime = calendar.timeInMillis

    val dataSource = DataSource.Builder()
        .setAppPackageName("com.google.android.gms")
        .setDataType(DataType.TYPE_SLEEP_SEGMENT)
        .setType(DataSource.TYPE_RAW)
        .setStreamName("sleep_session")
        .build()

    val readRequest = 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()
        .includeActivitySessions()
        .enableServerQueries()
        // Sleep segment data is required for details of the fine-granularity sleep, if it is present.
        .read(DataType.TYPE_SLEEP_SEGMENT)
        .read(dataSource)
        .setSessionName("Sample Session sleep")
        .setTimeInterval(startTime, endTime, TimeUnit.SECONDS)
        .build()
    Fitness.getSessionsClient(
        activity,
        GoogleSignIn.getAccountForExtension(activity, fitnessOptions)
    ).readSession(readRequest)
        .addOnSuccessListener { dataReadResponse ->
            processData(dataReadResponse)
        }
        .addOnFailureListener { error -> }


private fun processData(
    dataReadResponse: SessionReadResponse?,
) {
    for (session in dataReadResponse!!.sessions) {
        val sessionStart = session.getStartTime(TimeUnit.MILLISECONDS)
        val sessionEnd = session.getEndTime(TimeUnit.MILLISECONDS)

        // If the sleep session has finer granularity sub-components, extract them:
        val dataSets = dataReadResponse.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)
            }
        }
    }
}

After running the code, SessionReadResult status code is success, but there are empty sessions and datapoints.

SessionReadResult{status=Status{statusCode=SUCCESS, resolution=null}, sessions=[], sessionDataSets=[]}

0

There are 0 answers