传感器
{ "name": "blueos.hardware.sensor.sensor" }import sensor from '@blueos.hardware.sensor.sensor' 或 const sensor = require('@blueos.hardware.sensor.sensor')sensor.subscribeAccelerometer(OBJECT)
Section titled “sensor.subscribeAccelerometer(OBJECT)”订阅加速度传感器数据,如果多次调用,仅最后一次调用生效
说明:
- 加速度是重力加速度和设备自身运动加速度的矢量叠加
- 当设备静止或做匀速直线运动时,返回的加速度值表示重力加速度。
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| interval | String | 否 | 监听加速度数据回调函数的执行频率,默认 normal |
| callback | Function | 是 | 加速度感应数据变化后会回调此函数。 |
| fail | Function | 否 | 失败回调 |
callback 返回值:
Section titled “callback 返回值:”| 参数名 | 类型 | 说明 |
|---|---|---|
| x | Number | x 轴加速度 |
| y | Number | y 轴加速度 |
| z | Number | z 轴加速度 |
interval 的合法值:
| 值 | 说明 |
|---|---|
| game | 适用于更新游戏的回调频率,在 20ms/次 左右 |
| ui | 适用于更新 UI 的回调频率,在 60ms/次 左右 |
| normal | 普通的回调频率,在 200ms/次 左右 |
fail 返回错误代码
Section titled “fail 返回错误代码”| 错误码 | 说明 |
|---|---|
| 1000 | 当前设备不支持重力感应传感器 |
sensor.subscribeAccelerometer({ callback: function (ret) { console.log(`handling callback, x = ${ret.x}, y = ${ret.y}, z = ${ret.z}`) }, fail: function (data, code) { console.log(`handling fail, code = ${code}`) },})sensor.unsubscribeAccelerometer()
Section titled “sensor.unsubscribeAccelerometer()”取消监听重力感应数据
无
sensor.unsubscribeAccelerometer()sensor.subscribeCompass(OBJECT)
Section titled “sensor.subscribeCompass(OBJECT)”监听罗盘数据。如果多次调用,仅最后一次调用生效
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callback | Function | 是 | 罗盘数据变化后会回调此函数。 |
| fail | Function | 否 | 失败回调 |
callback 返回值:
Section titled “callback 返回值:”| 参数名 | 类型 | 说明 |
|---|---|---|
| direction | Number | 表示设备的 y 轴和地球磁场北极之间的角度,当面朝北,角度为 0;朝南角度为 π;朝东角度 π/2;朝西角度-π/2 |
| accuracy | Number | 精度 |
fail 返回错误代码
Section titled “fail 返回错误代码”| 错误码 | 说明 |
|---|---|
| 1000 | 当前设备不支持罗盘传感器 |
| 值 | 说明 |
|---|---|
| 3 | 高精度 |
| 2 | 中等精度 |
| 1 | 低精度 |
| -1 | 不可信,传感器失去连接 |
| 0 | 不可信,原因未知 |
sensor.subscribeCompass({ callback: function (ret) { console.log(`handling callback, direction = ${ret.direction}`) }, fail: function (data, code) { console.log(`handling fail, code = ${code}`) },})sensor.unsubscribeCompass()
Section titled “sensor.unsubscribeCompass()”取消监听罗盘数据
无
sensor.unsubscribeCompass()sensor.subscribeStepCounter(OBJECT)
Section titled “sensor.subscribeStepCounter(OBJECT)”监听计步传感器数据。如果多次调用,仅最后一次调用生效。
开发者需要在 manifest.json 里面配置权限:
Section titled “开发者需要在 manifest.json 里面配置权限:”{ "permissions": [{ "name": "watch.permission.STEP_COUNTER" }]}| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callback | Function | 是 | 计步传感器数据变化后会回调此函数。 |
| fail | Function | 否 | 失败回调 |
callback 返回值:
Section titled “callback 返回值:”| 参数名 | 类型 | 说明 |
|---|---|---|
| steps | Number | 计步传感器当前累计记录的步数。每次手表重启,这个值就会从 0 开始重新计算。 |
fail 返回错误代码
Section titled “fail 返回错误代码”| 错误码 | 说明 |
|---|---|
| 1000 | 当前设备不支持计步传感器 |
sensor.subscribeStepCounter({ callback: function (ret) { console.log(`handling callback, steps = ${ret.steps}`) }, fail: function (data, code) { console.log(`handling fail, code = ${code}`) },})sensor.unsubscribeStepCounter()
Section titled “sensor.unsubscribeStepCounter()”取消监听计步传感器数据。
无
sensor.unsubscribeStepCounter()sensor.subscribeOnBodyState(OBJECT)
Section titled “sensor.subscribeOnBodyState(OBJECT)”监听设备佩戴状态传感器数据。如果多次调用,仅最后一次调用生效。
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callback | Function | 是 | 穿戴状态改变后的回调函数。 |
| fail | Function | 否 | 接口调用失败的回调函数。 |
callback 返回值:
Section titled “callback 返回值:”| 参数名 | 类型 | 说明 |
|---|---|---|
| value | boolean | 是否已佩戴。 |
fail 返回错误代码
Section titled “fail 返回错误代码”| 错误码 | 说明 |
|---|---|
| 1000 | 当前设备不支持佩戴状态传感器 |
sensor.subscribeOnBodyState({ callback: function (ret) { console.log('get on-body state value:' + ret.value) }, fail: function (data, code) { console.log('Subscription failed. Code: ' + code + '; Data: ' + data) },})sensor.unsubscribeOnBodyState()
Section titled “sensor.unsubscribeOnBodyState()”取消监听设备佩戴状态。
无
sensor.unsubscribeOnBodyState()sensor.getOnBodyState(OBJECT)
Section titled “sensor.getOnBodyState(OBJECT)”获取设备佩戴状态。
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| success | Function | 否 | 接口调用成功的回调函数。 |
| fail | Function | 否 | 接口调用失败的回调函数。 |
| complete | Function | 否 | 接口调用结束的回调函数。 |
callback 返回值:
Section titled “callback 返回值:”| 参数名 | 类型 | 说明 |
|---|---|---|
| value | boolean | 是否已佩戴。 |
fail 返回错误代码
Section titled “fail 返回错误代码”| 错误码 | 说明 |
|---|---|
| 1000 | 当前设备不支持佩戴状态传感器 |
sensor.getOnBodyState({ success: function (ret) { console.log('on body state: ' + ret.value) }, fail: function (data, code) { console.log('Subscription failed. Code: ' + code + '; Data: ' + data) },})sensor.subscribeGyroscope(OBJECT)
Section titled “sensor.subscribeGyroscope(OBJECT)”监听陀螺仪传感器数据。如果多次调用,仅最后一次调用生效。
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callback | Function | 是 | 陀螺仪传感器数据改变后的回调函数。 |
| fail | Function | 否 | 接口调用失败的回调函数。 |
callback 返回值:
Section titled “callback 返回值:”| 参数名 | 类型 | 说明 |
|---|---|---|
| x | Number | x 轴坐标 |
| y | Number | y 轴坐标 |
| z | Number | z 轴坐标 |
fail 返回错误代码
Section titled “fail 返回错误代码”| 错误码 | 说明 |
|---|---|
| 1000 | 当前设备不支持陀螺仪传感器 |
sensor.subscribeGyroscope({ callback: function (ret) { console.log(`handling callback, x = ${ret.x}, y = ${ret.y}, z = ${ret.z}`) }, fail: function (data, code) { console.log(`handling fail, code = ${code}`) },})sensor.unsubscribeGyroscope()
Section titled “sensor.unsubscribeGyroscope()”取消监听陀螺仪数据。
无
sensor.unsubscribeGyroscope()sensor.subscribeBarometer(OBJECT)
Section titled “sensor.subscribeBarometer(OBJECT)”监听气压传感器数据。如果多次调用,仅最后一次调用生效。
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callback | Function | 是 | 气压传感器数据改变后的回调函数。 |
| fail | Function | 否 | 接口调用失败的回调函数。 |
callback 返回值:
Section titled “callback 返回值:”| 参数名 | 类型 | 说明 |
|---|---|---|
| pressure | Number | 气压值,单位:帕斯卡。 |
fail 返回错误代码
Section titled “fail 返回错误代码”| 错误码 | 说明 |
|---|---|
| 1000 | 当前设备不支持气压传感器 |
sensor.subscribeBarometer({ callback: function (ret) { console.log('get data value:' + ret.pressure) }, fail: function (data, code) { console.log('Subscription failed. Code: ' + code + '; Data: ' + data) },})sensor.unsubscribeBarometer()
Section titled “sensor.unsubscribeBarometer()”取消监听气压传感器。
无
sensor.unsubscribeBarometer()sensor.subscribeWristLift(OBJECT)
Section titled “sensor.subscribeWristLift(OBJECT)”监听抬腕。如果多次调用,仅最后一次调用生效。
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callback | Function | 是 | 抬腕后的回调函数。 |
| fail | Function | 否 | 接口调用失败的回调函数。 |
callback 返回值:
Section titled “callback 返回值:”无
fail 返回错误代码
Section titled “fail 返回错误代码”| 错误码 | 说明 |
|---|---|
| 1000 | 当前设备不支持 |
sensor.subscribeWristLift({ callback: function () { console.log('wrist lift') }, fail: function (data, code) { console.log('Subscription failed. Code: ' + code + '; Data: ' + data) },})sensor.unsubscribeWristLift()
Section titled “sensor.unsubscribeWristLift()”取消监听监听抬腕。
无
sensor.unsubscribeWristLift()sensor.subscribeContinuousWristTurn(OBJECT)
Section titled “sensor.subscribeContinuousWristTurn(OBJECT)”监听连续转腕。如果多次调用,仅最后一次调用生效。
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callback | Function | 是 | 连续转腕变后的回调函数。 |
| fail | Function | 否 | 接口调用失败的回调函数。 |
callback 返回值:
Section titled “callback 返回值:”无
fail 返回错误代码
Section titled “fail 返回错误代码”| 错误码 | 说明 |
|---|---|
| 1000 | 当前设备不支持 |
sensor.subscribeContinuousWristTurn({ callback: function () { console.log('continuous wrist turn') }, fail: function (data, code) { console.log('Subscription failed. Code: ' + code + '; Data: ' + data) },})sensor.unsubscribeContinuousWristTurn()
Section titled “sensor.unsubscribeContinuousWristTurn()”取消监听连续转腕。
无
sensor.unsubscribeContinuousWristTurn()