android / health-samples

Apache License 2.0
265 stars 148 forks source link

After using Wear Health Services , the accelerator sensor data cannot be sampled with the specified sample rate #9

Closed haozes closed 2 years ago

haozes commented 3 years ago

I use WHS and Accelerator at the same time.

After using the HealthServices and exerciseClient to start startExercise,

the the accelerator always sampled by 100HZ, not the sample rate which i specified.

this.motionManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager
this.acc_sensor = this.motionManager!!.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)

this.SAMPLE_RATE = 20.0
this.motionManager?.registerListener(
            this,
            this.acc_sensor!!,
            (1000 * 1000 / this.SAMPLE_RATE).toInt()  //not work, the data samplerate will be 100hz
        )

Is this a bug?

BTW: Test on galaxy watch 4

haozes commented 3 years ago

As I test, if i set ExerciseType.JUMP_ROPE , the Accelerometer sample rate will be 100HZ,

ExerciseType.RUNNING will be 20HZ.

It seems that WHS has some side effects on Accelerometer.😢

If I use WHS, I may not be able to control the sampling rate

ithinkihaveacat commented 3 years ago

@haozes Unfortunately it's possible for WHS to affect the behavior of the Android SensorManager. (The sampling rate is "only a hint to the system".) Can you provide your WHS configuration? What's your overall goal/use case?

Also, if it's not possible for the system to generate samples at the rate you specify, what would be the next best behavior be? (From your second comment it seems that having different rates for different exercise types is especially unexpected—would you prefer them to be consistent, at least?)

haozes commented 3 years ago

@ithinkihaveacat thank you for reply.

I use watch accelerator data to compute jump rope repetition. It depends on my own algorithm. So I must specify a sample rate.

This is what i built on watchOS: https://apps.apple.com/app/yaoyao-jump-rope/id1179393901

When i have not use WHS, the Watch workout auto detection always interrupt my app after jump 10min.

auto detection

As I test, after using WHS, Watch auto workout detection disappears. That what i want WHS to do, only this thing.

But now, that is all undefined. so disappointed, Maybe from the beginning, It didn't fully consider others to making a workout app on wearOS.

If it's not possible for the system to generate samples at the rate I specify,
let me know what's sample rate now in different types of exercise.

Otherwise it's impossible to build a workout app depending on anyone's own algorithm.

haozes commented 3 years ago

PS: My WHS config

private  val workoutType:ExerciseType = ExerciseType.JUMP_ROPE
----    
suspend fun startExercise() {
        Log.d(TAG, "Starting exercise")
        // Types for which we want to receive metrics. Only ask for ones that are supported.
        val capabilities = getExerciseCapabilities() ?: return
        val dataTypes = setOf(
            DataType.HEART_RATE_BPM,
        ).intersect(capabilities.supportedDataTypes)

        val aggDataTypes = setOf(
            DataType.TOTAL_CALORIES
        ).intersect(capabilities.supportedDataTypes)

        val config = ExerciseConfig.builder()
            .setExerciseType(workoutType)
            .setShouldEnableAutoPauseAndResume(false)
            .setAggregateDataTypes(aggDataTypes)
            .setDataTypes(dataTypes)
            // Required for GPS for LOCATION data type, optional for some other types.
            .setShouldEnableGps(false)
            .build()
        exerciseClient.startExercise(config).await()
        exerciseClient.setUpdateListener(this).await()
    }
ithinkihaveacat commented 2 years ago

Is this a good summary of your issue?

  1. Your algorithm requires receiving accelerometer data at a constant rate.
  2. If you use only Android SensorManager APIs, the data is delivered at the expected rate.
  3. However, after around 10 minutes, the automatic activity recognition system activates, and interrupts your app.
  4. Starting a WHS exercise disables the automatic activity recognition (this is the intended behaviour BTW), however it also affects the rate at which accelerometer data is delivered.

What you would like:

  1. A way of disabling the automatic activity recognition mechanism without affecting the sensor rate.

I think it's reasonable to want to disable the automatic activity recognition mechanism (I will look into this, although as I recall this was not as straightforward as it seems…). However I still think you should not depend on receiving accelerometer data at a constant rate since the sampling period is only a hint (see ref above).

Can you adapt your algorithm to deal with a variable rate?

haozes commented 2 years ago

Thank you for your advice. I will change my algorithm to adapt a variable rate.