When the user enters step count that's less than the auto detected steps app is showing sum of both and called both entries as non user entered, when fetching steps using Android SDK
e.g auto detected steps are 85 I manually entered 50 steps now its showing 135 in Google Fit App but using below code when fetching steps its giving only one entry which contains 135 steps that is not user entered.
The Code
suspend fun readAutomaticallyDetectedSteps(): Pair<Double, Double> {
// Set the start and end date for the query.
val startTime: Long = getUtcStartOfDay(System.currentTimeMillis(), TimeZone.getDefault())
val endTime = startTime + TimeUnit.DAYS.toMillis(1)
val c1 = Calendar.getInstance()
c1.timeInMillis = startTime
val c2 = Calendar.getInstance()
Log.d(TAG, "fetchTodaysStats: getDailyCount ${c1.time.toString()} to ${c2.time.toString()}")
// Creating the query.
val readRequest = DataReadRequest.Builder()
.aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build()
// Reading the data.
var sum = 0.0
var user_sum = 0.0
// Invoke the History API to fetch the data with the query
suspendCancellableCoroutine<Pair<Double, Double>> { cont ->
Fitness.getHistoryClient(context, GoogleSignIn.getLastSignedInAccount(context)!!)
.readData(readRequest)
.addOnSuccessListener { dataReadResponse ->
if (dataReadResponse.buckets.isNotEmpty()) {
for (bucket in dataReadResponse.buckets) {
bucket.dataSets.forEach {
Log.d(TAG, "Activity Name: " + bucket.activity)
dumpDataSet(it)
var lst = it.dataPoints
.filter {
it.originalDataSource.streamName != "user_input" }
lst.forEach {dp->
for (field in dp.dataType.fields) {
sum+= dp.getValue(field!!).asInt().toDouble()
}
}
var lst2 = it.dataPoints
.filter {
it.originalDataSource.streamName == "user_input" }
lst2.forEach {dp->
for (field in dp.dataType.fields) {
user_sum+= dp.getValue(field!!).asInt().toDouble()
}
}
}
}
}
if (dataReadResponse.dataSets.isNotEmpty()) {
dataReadResponse.dataSets.forEach {
Log.d(TAG, "No Activity Name")
dumpDataSet(it)
var lst = it.dataPoints
.filter {
it.originalDataSource.streamName != "user_input" }
lst.forEach {dp->
for (field in dp.dataType.fields) {
sum+= dp.getValue(field!!).asInt().toDouble()
}
}
var lst2 = it.dataPoints
.filter {
it.originalDataSource.streamName == "user_input" }
lst2.forEach {dp->
for (field in dp.dataType.fields) {
user_sum+= dp.getValue(field!!).asInt().toDouble()
}
}
}
}
Log.e(TAG, "user_entered=$user_sum")
Log.e(TAG, "auto detected=$sum")
cont.resume(Pair(sum, user_sum), null)
}
.addOnFailureListener {
// Handle error
}
}
return Pair(sum, user_sum)
}
The Result (Logs): The result you can see is have only one entry coming from samsung device but it should be two entries
D fetchTodaysStats: getDailyCount Mon Jul 03 00:00:01 GMT+05:00 2023 to Mon Jul 03 18:03:24 GMT+05:00 2023
Activity Name: unknown
Data returned for Data type: com.google.step_count.delta
# Type, Time, Source, Original Source
origin: step_counter Non-wakeup
entry#1
com.google.step_count.delta, 28139775, 1, DataSource{raw:Device{samsung:SM-A235F:be9b9a53:1:2}:step_counter Non-wakeup:DataType{com.google.step_count.cumulative[steps(i)]}}, raw:com.google.step_count.cumulative:samsung:SM-A235F:be9b9a53:step_counter Non-wakeup, step_counter Non-wakeup
user_entered=0.0
auto detected=135.0
what should be the result: The result should be two entries one non user entered that contains 85 steps and other one 50 steps that is user entered.