简体   繁体   中英

I have some problem about step Counter Google fit API

I am currently trying to work with Google Fit API. I have been mainly by following Google's documentation.

Below is the code that I have which seems to have a problem

The problem I have is that it shows me all step count. I want to return my app today step count. this code return me 550530 steps. I want to show today's step count.

class MainActivity : AppCompatActivity(), OnDataPointListener,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {
    private var authInProgress = false
    private var mApiClient: GoogleApiClient? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        if (savedInstanceState != null) {
            authInProgress = savedInstanceState.getBoolean(AUTH_PENDING)
        }
        mApiClient = GoogleApiClient.Builder(this)
            .addApi(Fitness.SENSORS_API)
            .addScope(Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build()

    }

    override fun onStart() {
        super.onStart()
        mApiClient!!.connect()
    }

    override fun onStop() {
        super.onStop()
        Fitness.SensorsApi.remove(mApiClient, this)
            .setResultCallback { status ->
                if (status.isSuccess) {
                    mApiClient!!.disconnect()
                }
            }
    }

    private fun registerFitnessDataListener(dataSource: DataSource, dataType: DataType) {
        val request = SensorRequest.Builder()
            .setDataSource(dataSource)
            .setDataType(dataType)
            .setSamplingRate(3, TimeUnit.SECONDS)
            .build()

        Fitness.SensorsApi.add(mApiClient, request, this).setResultCallback { status ->
            if (status.isSuccess) {
                Log.e("GoogleFit", "SensorApi successfully added")
            } else {
                Log.e(
                    "GoogleFit",
                    "adding status: " + status.statusMessage
                )
            }
        }
    }

    override fun onConnected(bundle: Bundle?) {
        val dataSourceRequest = DataSourcesRequest.Builder()
            .setDataTypes(DataType.TYPE_STEP_COUNT_CUMULATIVE)
            .setDataSourceTypes(DataSource.TYPE_RAW)
            .build()
        val dataSourcesResultCallback =
            ResultCallback<DataSourcesResult> { dataSourcesResult ->
                for (dataSource in dataSourcesResult.dataSources) {
                    if (DataType.TYPE_STEP_COUNT_CUMULATIVE == dataSource.dataType) {
                        registerFitnessDataListener(
                            dataSource,
                            DataType.TYPE_STEP_COUNT_CUMULATIVE
                        )
                    }
                }
            }
        Fitness.SensorsApi.findDataSources(
            mApiClient,
            dataSourceRequest
        )
            .setResultCallback(dataSourcesResultCallback)
    }

    override fun onConnectionFailed(connectionResult: ConnectionResult) {
        if (!authInProgress) {
            try {
                authInProgress = true
                connectionResult.startResolutionForResult(
                    this,
                    REQUEST_OAUTH
                )
            } catch (e: IntentSender.SendIntentException) {
                Log.e("GoogleFit", "sendingIntentException " + e.message)
            }
        } else {
            Log.e("GoogleFit", "authInProgress")
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        //++++++++
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_OAUTH) {
            authInProgress = false
            if (resultCode == Activity.RESULT_OK) {
                if (!mApiClient!!.isConnecting && !mApiClient!!.isConnected) {
                    mApiClient!!.connect()
                }
            } else if (resultCode == Activity.RESULT_CANCELED) {
                Log.e("GoogleFit", "RESULT_CANCELED")
            }
        } else {
            Log.e("GoogleFit", "requestCode NOT request_oauth")
        }
    }

    override fun onConnectionSuspended(i: Int) {}

    override fun onDataPoint(dataPoint: DataPoint) {
        for (field in dataPoint.dataType.fields) {
            val value = dataPoint.getValue(field)

            runOnUiThread {
                Toast.makeText(
                    applicationContext,
                    "Field: " + field.name + " Value: " + value,
                    Toast.LENGTH_SHORT
                ).show()
            }
        }
    }

    companion object {
        private const val REQUEST_OAUTH = 1
        private const val AUTH_PENDING = "auth_state_pending"
    }
}

The code is returning 550530 steps because you are using DataType.TYPE_STEP_COUNT_CUMULATIVE , CUMULATIVE will return the step count data as a sum since the start of the count or you can say from the beginning.

You can use DataType.TYPE_STEP_COUNT_DELTA it will give you each data point not the sum of all.

You can check more in the documentation from DataType

Happy Coding!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM