采样数据 Sample
更新时间:
了解如何操作样本数据 Sample 通过本节,您将学会
查询以及监听 Sample
下面我们通过获取心率来阐述如何使用Sample 的接口
查询当前的心率
假设现在我们要获取用户当前这个时刻的心率和步数数据,可以通过下面的代码实现
导入模块
import health from '@service.health' 或 const health = require('@service.health')
复制代码
接口定义
health.getRecentSamples(OBJECT)
获取最近一次采样数据
import health from '@service.health'
health.getRecentSamples({
dataTypes: [health.DATA_TYPES.HEART_RATE, health.DATA_TYPES.STEP_COUNT],
success: (res) => {
console.log(`current heart rate(${res[0].dataType}) is`, res[0].data.value, 'bpm')
},
})
复制代码
health.subscribeSample(OBJECT)
监听心率的变化
假设现在我们要实时展示用户心率数据的变化,可以通过下面的代码实现
import health from '@service.health'
health.subscribeSample({
dataType: health.DATA_TYPES.HEART_RATE,
callback: (res) => {
console.log(`current heart rate(${res.value}) is`)
},
})
复制代码
health.unsubscribeSample(OBJECT)
取消监听心率的变化
import health from '@service.health'
health.unsubscribeSample({
dataType: health.DATA_TYPES.HEART_RATE,
})
复制代码