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())
Each time definition of the sleep stage in the sleep segment is needed, but I don't know what standard to set. I need help.
https://developers.google.com/fit/scenarios/write-sleep-data?
Related
The issue started happening after I disabled google fit. Link
Checking permissions with
GoogleSignIn.hasPermissions(googleAccount, fitnessOptions) returns true.
Also the method for requesting permissions asks to select an account, but after selecting an account I get the same exception trying to read data.
GoogleSignIn.requestPermissions(
activity,
GOOGLE_FIT_PERMISSIONS_REQUEST_CODE,
googleAccount,
fitnessOptions
)
Here is the code that I'm using to try to read data from GoogleFit.
var end = DateHelper.getEndOfDay(endDate)
if (DateHelper.isToday(endDate)) {
/* make sure current day query time is until current time, not end of the day */
end = Calendar.getInstance().time
}
val start = DateHelper.getStartOfDay(startDate)
val datasource = DataSource.Builder()
.setAppPackageName("com.google.android.gms")
.setDataType(DataType.TYPE_STEP_COUNT_DELTA)
.setType(DataSource.TYPE_DERIVED)
.setStreamName("estimated_steps")
.build()
val request = DataReadRequest.Builder()
.aggregate(datasource)
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(start.time, end.time, TimeUnit.SECONDS)
.build()
val fitnessOptions: GoogleSignInOptionsExtension = FitnessOptions.builder()
.addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
.addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
.build()
val googleSignInAccount: GoogleSignInAccount =
GoogleSignIn.getAccountForExtension(this.reactContext, fitnessOptions)
Fitness.getHistoryClient(this.reactContext, googleSignInAccount)
.readData(request)
.addOnSuccessListener { response ->
val totalSteps = response.buckets
.flatMap { it.dataSets }
.flatMap { it.dataPoints }
.sumBy { it.getValue(Field.FIELD_STEPS).asInt() }
Log.i(TAG, "Total steps: $totalSteps")
promise.resolve(totalSteps)
}
.addOnFailureListener { e: Exception ->
Log.i(TAG, "onFailure()");
Log.i(TAG, "Error $e");
promise.reject(e);
}
Is there any way to force account authorization to sign in the user? Or what is the reason I get this error, because the method to check if account has permission returns true.
I'm trying a simple thing, but unfortunately, I'm unable to achieve it. I want to receive activity transition recognition callbacks from Android SDK 26 - 29:
I have registered the following permissions in the manifest:
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/>
In the main activity, I check the permission:
private val activityRecognitionPermission = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) permission.ACTIVITY_RECOGNITION else "com.google.android.gms.permission.ACTIVITY_RECOGNITION"
if( PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(this, activityRecognitionPermission) )
permissionsToRequest += activityRecognitionPermission
This works fine. The permission is granted.
Afterwards I register the listener:
if (PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(this, activityRecognitionPermission)) {
val transitions = ArrayList<ActivityTransition>()
transitions.add(ActivityTransition.Builder()
.setActivityType(DetectedActivity.IN_VEHICLE)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
.build())
transitions.add(ActivityTransition.Builder()
.setActivityType(DetectedActivity.IN_VEHICLE)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
.build())
transitions.add(ActivityTransition.Builder()
.setActivityType(DetectedActivity.WALKING)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
.build())
transitions.add(ActivityTransition.Builder()
.setActivityType(DetectedActivity.WALKING)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
.build())
transitions.add(ActivityTransition.Builder()
.setActivityType(DetectedActivity.ON_BICYCLE)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
.build())
transitions.add(ActivityTransition.Builder()
.setActivityType(DetectedActivity.ON_BICYCLE)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
.build())
transitions.add(ActivityTransition.Builder()
.setActivityType(DetectedActivity.ON_FOOT)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
.build())
transitions.add(ActivityTransition.Builder()
.setActivityType(DetectedActivity.ON_FOOT)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
.build())
transitions.add(ActivityTransition.Builder()
.setActivityType(DetectedActivity.RUNNING)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
.build())
transitions.add(ActivityTransition.Builder()
.setActivityType(DetectedActivity.RUNNING)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
.build())
transitions.add(ActivityTransition.Builder()
.setActivityType(DetectedActivity.STILL)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
.build())
transitions.add(ActivityTransition.Builder()
.setActivityType(DetectedActivity.STILL)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
.build())
val request = ActivityTransitionRequest( transitions )
val client = ActivityRecognition.getClient(this)
val intent = Intent(this, ActivityRecognitionReceiver::class.java)
activityRecognitionPendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0)
val task = client.requestActivityTransitionUpdates(request, activityRecognitionPendingIntent!!)
task.addOnSuccessListener ( object: OnSuccessListener<Void> {
override fun onSuccess(p0: Void?) {
return
}
})
task.addOnFailureListener ( object: OnFailureListener {
override fun onFailure(p0: Exception) {
return
}
})
}
I debuged and the onSuccess method is called after that.
This is the code of my receiver:
class ActivityRecognitionReceiver: BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if( ActivityTransitionResult.hasResult(intent)) {
val resultList = ActivityTransitionResult.extractResult(intent)
if( resultList != null ) {
for (event in resultList.transitionEvents) {
// TODO
}
}
}
}
}
My problem is, that the onReceive method is never called back.
Does anyone have an idea, what the problem could be?
I'm developing a step counter with Google Fit.
I tried to implement as it is defined in Google Fit docs. But the problem is that when I query step count it always returns empty dataset but when I query with readDailyTotal function it returns a dataset.
I am not able to find what is the cause.
I get the required permission for required step count permission
val fitnessOptions = FitnessOptions.builder()
.addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
.addDataType(DataType.TYPE_STEP_COUNT_CUMULATIVE, FitnessOptions.ACCESS_READ)
.addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
.build()
if (! GoogleSignIn.hasPermissions(GoogleSignIn.getLastSignedInAccount(activity),
fitnessOptions)) {
GoogleSignIn.requestPermissions(
activity, // your activity
REQUEST_CODE_GOOGLE_FIT_PERMISSIONS,
GoogleSignIn.getLastSignedInAccount(activity),
fitnessOptions
)
} else {
onSuccess.invoke()
}
I subscribe to the application for recording step counts.
Fitness.getRecordingClient(context, client!!)
.subscribe(DataType.TYPE_STEP_COUNT_DELTA)
.addOnSuccessListener {
onSuccess.invoke()
}
.addOnFailureListener { e ->
onFail.invoke(e)
}
I query 1 week period with history API but it always returns an empty dataset.
// Setting a start and end date using a range of 1 week before this moment.
val cal = Calendar.getInstance()
val now = Date()
cal.time = now
val endTime = cal.timeInMillis
cal.add(Calendar.WEEK_OF_YEAR, -1)
val startTime = cal.timeInMillis
val dateFormat = DateFormat.getDateInstance()
Log.i(TAG, "Range Start: " + dateFormat.format(startTime))
Log.i(TAG, "Range End: " + dateFormat.format(endTime))
val readRequest = DataReadRequest.Builder()
// The data request can specify multiple data types to return, effectively
// combining multiple data queries into one call.
// In this example, it's very unlikely that the request is for several hundred
// datapoints each consisting of a few steps and a timestamp. The more likely
// scenario is wanting to see how many steps were walked per day, for 7 days.
.aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
// Analogous to a "Group By" in SQL, defines how data should be aggregated.
// bucketByTime allows for a time span, whereas bucketBySession would allow
// bucketing by "sessions", which would need to be defined in code.
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.enableServerQueries()
.build()
Fitness.getHistoryClient(context, client)
.readData(readRequest)
.addOnSuccessListener { dataReadResponse ->
dumpDataSets(dataReadResponse.dataSets)
onSuccess.invoke(dataReadResponse)
}
.addOnFailureListener { e ->
onFail.invoke(e)
}
.addOnCompleteListener { task ->
dumpDataSets(task.result!!.dataSets)
onComplete.invoke(task)
}
Datasets can be found in buckets:
dataReadResponse.buckets.forEach { bucket ->
bucket.dataSet.forEach { dataSet ->
dataSet.dataPoints.forEach { dataPoint ->
// USE DataPoint
}
}
}
I'm having trouble finding the major and minor number of a bluetooth beacon using RxBle. Any help would be appreciated. I've tried this
rxBleClient = ProxiApplication.getRxBleClient(this)
scanDisposable = rxBleClient.scanBleDevices(
ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
.build(),
ScanFilter.Builder()
.build()
).observeOn(AndroidSchedulers.mainThread())
.doFinally(this::dispose)
.subscribe({
Log.v("MainActivity", "scan results: ${it}"]
}, {
Log.v("MainActivity", "error: $it")
})
This code get calo in day:
val cal = Calendar.getInstance()
cal.time = Date()
val endTime = cal.timeInMillis
cal.add(Calendar.DAY_OF_YEAR, -1)
val startTime = cal.timeInMillis
val readRequest = DataReadRequest.Builder()
.aggregate(DataType.TYPE_CALORIES_EXPENDED, DataType.AGGREGATE_CALORIES_EXPENDED)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.bucketByTime(1, TimeUnit.DAYS)
.build()
Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this))
.readData(readRequest)
.addOnSuccessListener { dataReadResponse ->
// For the sake of the sample, we'll print the data so we can see what we just
// added. In general, logging fitness information should be avoided for privacy
// reasons.
printData(dataReadResponse)
}
.addOnFailureListener { e -> Log.e(TAG, "onFailure()", e) }
.addOnCompleteListener(OnCompleteListener {
Log.d(TAG, "onComplete()")
})
Value return:Field Calories Value= 11332.985.
But in Google Fit App display value = 6.164 Cal.
Why value of CALORIES_EXPENDED of API not equal value of Google Fit App?