结合使用合成数据与健康服务

使用合成数据提供程序可以模拟来自健康服务的传感器数据并对应用进行测试,就像真正运动过一样。您可以从预配置的运动个人资料中选择一个来使用,也可以使用标志配置一个自定义个人资料。

您可以通过以下两种方式生成模拟数据:

  • 您可以使用“合成数据提供程序”,该提供程序提供丰富的数据生成选项。此方法使用 adb 命令,本页对此进行了介绍。

    如需在模拟器上使用合成数据提供程序,必须启用开发者选项

  • 您还可以使用 Android Studio 中的模拟器扩展控件。您可以使用按钮栏中的菜单访问模拟器的“Extended controls”面板。在扩展控件中,您可以加载 Keyhole 标记语言 (KML) 或 GPS 交换格式 (GPX) 文件来模拟位置。您还可以指定心率传感器的行为。

启用合成数据提供程序

若要启用合成数据提供程序,请完成以下步骤。

  1. 启用开发者选项
  2. 发出以下 adb 命令启用合成数据提供程序:

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

启用合成数据提供程序后,发出本页介绍的命令来控制“合成用户”的行为。

停用合成数据提供程序

如需切换回使用实际传感器,请运行以下命令:

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

合成运动

健康服务支持以下运动类型:

  • 走路whs.synthetic.user.START_WALKING
  • 跑步whs.synthetic.user.START_RUNNING
  • 远足whs.synthetic.user.START_HIKING
  • 游泳whs.synthetic.user.START_SWIMMING
  • 跑台跑步whs.synthetic.user.START_RUNNING_TREADMILL

这些运动会生成以下数据类型的实际合成数据:

  • 心率
  • 每分钟步数
  • GPS 位置(使用一条默认路线)
  • 活动时长
  • 海拔和楼层

此外,还可以生成以下状态:

  • 睡眠状态 - 睡眠或清醒
  • 跌倒检测

开始

如需开始模拟运动,请向 com.google.android.wearable.healthservices 发出相应的广播:

# start the "walking" synthetic exercise
$ adb shell am broadcast \
-a "whs.synthetic.user.START_WALKING" \
com.google.android.wearable.healthservices

每项活动在支持的各项指标上的预设值如下:

活动 心率 平均速度 海拔变化 使用位置信息
步行 120 bpm 1.4 米/秒 20.0 米/分钟
跑步 170 bpm 2.3 米/秒 20.0 米/分钟
远足 150 bpm 1.3 米/秒 20.0 米/分钟
游泳 150 bpm 1.6 米/秒 0.0 米/分钟
跑台跑步 160 bpm 2.3 米/秒 20.0 米/分钟

停止

如需停止综合活动,请使用以下命令:

adb shell am broadcast \
-a "whs.synthetic.user.STOP_EXERCISE" \
com.google.android.wearable.healthservices

自定义

如需更精确地控制将要生成哪些指标,请使用操作字符串 whs.synthetic.user.START_EXERCISE 来启动自定义运动活动:提供以下标志的任意组合:

  • --ei exercise_options_duration_secs <int>:运动时长(秒)。默认值:0
  • --ei exercise_options_heart_rate <int>:心率(每分钟心跳次数)。平均值:70
  • --ef exercise_options_average_speed <float>:平均速度(米/秒)。还会影响每分钟步数或迈步频率。默认值:0
  • --ez exercise_options_use_location <boolean>:是否在运动期间发出位置数据(使用默认路线)。默认值:false
  • --ef exercise_options_max_elevation_rate <float>:可能的最大海拔变化速度(米/分钟)。默认值:0

例如,可以按照以下方式设置运动选项:

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

此外,无论实际硬件或模拟硬件是否支持某种特定数据类型,您都可以更改可用的数据类型。例如,您可以启用或停用绝对海拔,如以下代码段所示:

# enable synthetic mode and enable absolute elevation
$ adb shell am broadcast \
-a "whs.CONFIGURE_SYNTHETIC_DEVICE" \
--ez absolute_elevation true \
com.google.android.wearable.healthservices

# enable synthetic mode and disable absolute elevation
$ adb shell am broadcast \
-a "whs.CONFIGURE_SYNTHETIC_DEVICE" \
--ez absolute_elevation false \
com.google.android.wearable.healthservices

其他状态和事件

睡眠状态

您还可以为合成用户触发睡眠状态。支持两种状态:睡眠和清醒。

如需进入睡眠状态,请运行以下命令:

adb shell am broadcast \
-a "whs.synthetic.user.START_SLEEPING" \
com.google.android.wearable.healthservices

如需进入清醒状态,请运行以下命令:

adb shell am broadcast \
-a "whs.synthetic.user.STOP_SLEEPING" \
com.google.android.wearable.healthservices

跌倒检测

如需模拟跌倒情况,请运行以下命令:

adb shell am broadcast \
-a "whs.synthetic.user.FALL_OVER" \
com.google.android.wearable.healthservices

健康服务最多可能需要 1 分钟才能提供跌倒事件数据。