androidandroid-emulatorwear-oswearables

How do I mock locations using Health Services on Wear OS?


I have followed the guide at Active data and exercises to set up an exercise client. This works fine on my physical Samsung Galaxy Watch 5 and I get my real-time location.

On emulators inside of Android Studio, however, the location I receive is a constant longitude 51.530211685534 longitude -0.12388666083292, which is in London.

The way I initialize an exercise client is:

healthClient.exerciseClient.setUpdateCallback(excerciseUpdateCallback);

val exerciseConfig = ExerciseConfig(ExerciseType.BIKING, setOf(DataType.LOCATION), false, true);

healthClient.exerciseClient.startExerciseAsync(exerciseConfig);

The way I receive location updates is:

override fun onExerciseUpdateReceived(update: ExerciseUpdate) {
    println("Recorder: onExerciseUpdate");

    val latestMetrics = update.latestMetrics;
    val locationUpdates = latestMetrics.getData(DataType.LOCATION);
    
    if(locationUpdates.isNotEmpty()) {
        val location = locationUpdates.last();

        println("Last longitude " + location.value.latitude + " longitude " + location.value.longitude);
    }
}

The result I always get is:

Recorder: onExerciseUpdate
Last longitude 51.530211685534 longitude -0.12388666083292

When I start a route, this pops into the console:

set_timerslack_ns write failed: Operation not permitted

I do not have anything else but the exercise update callback running during this. Only the UI, but nothing is being done nor running in the UI.

I have tried setting a static location point, which has not yielded a change in the coordinates.

I have also tried setting a route, which has also not yielded a change in the coordinates.

This only happens in the emulator, as my physical device is running just fine.

The emulator I am running is the latest one that Android Studio provides me:

Wear OS Large Round API 30

Android 11.0 Wear OS 3 x86

Emulator demonstration Click for a video demonstration


Solution

  • I had not seen the Use synthetic data with Health Services page.

    To enable synthetic sensors:

    adb shell am broadcast -a "whs.USE_SYNTHETIC_PROVIDERS" com.google.android.wearable.healthservices
    

    To enable simulated biking location updates:

    adb shell am broadcast -a "whs.synthetic.user.START_EXERCISE" --ef exercise_options_average_speed 1.2 --ez exercise_options_use_location true com.google.android.wearable.healthservices
    

    Related: How do you connect your terminal with the Android emulator?