diff --git a/zh-cn/application-dev/device/sensor-guidelines.md b/zh-cn/application-dev/device/sensor-guidelines.md index bdf1901ad3ad6a04422395f83bd4f4106a6be9ce..eda194278eb71e09c9e8eaf3e90722afeb1f3701 100644 --- a/zh-cn/application-dev/device/sensor-guidelines.md +++ b/zh-cn/application-dev/device/sensor-guidelines.md @@ -31,17 +31,15 @@ 具体配置方式请参考[权限申请声明](../security/accesstoken-guidelines.md)。 -2. 持续监听传感器数据变化。 +2. 以下场景以加速度传感器ACCELEROMETER为例。展示持续监听传感器接口的调用结果。 ```js import sensor from "@ohos.sensor"; sensor.on(sensor.SensorId.ACCELEROMETER, function(data){ - console.info("Data obtained successfully. x: " + data.x + "y: " + data.y + "z: " + data.z);// 获取数据成功 + console.info("Data obtained successfully. x: " + data.x + "y: " + data.y + "z: " + data.z); // 获取数据成功 }); ``` - 以SensorId为ACCELEROMETER为例展示运行结果,持续监听传感器接口的结果如下图所示: - ![171e6f30-a8d9-414c-bafa-b430340305fb](figures/171e6f30-a8d9-414c-bafa-b430340305fb.png) 3. 注销传感器数据监听。 @@ -51,8 +49,6 @@ sensor.off(sensor.SensorId.ACCELEROMETER); ``` - 以SensorId为ACCELEROMETER为例展示运行结果,注销传感器成功结果如下图所示: - ![65d69983-29f6-4381-80a3-f9ef2ec19e53](figures/65d69983-29f6-4381-80a3-f9ef2ec19e53.png) 4. 获取一次传感器数据变化。 @@ -60,12 +56,10 @@ ```js import sensor from "@ohos.sensor"; sensor.once(sensor.SensorId.ACCELEROMETER, function(data) { - console.info("Data obtained successfully. x: " + data.x + "y: " + data.y + "z: " + data.z);// 获取数据成功 + console.info("Data obtained successfully. x: " + data.x + "y: " + data.y + "z: " + data.z); // 获取数据成功 }); ``` - 以SensorId为ACCELEROMETER为例展示运行结果,获取数据成功日志如下图所示: - ![db5d017d-6c1c-4a71-a2dd-f74b7f23239e](figures/db5d017d-6c1c-4a71-a2dd-f74b7f23239e.png) 若接口调用不成功,建议使用try/catch语句捕获代码中可能出现的错误信息。例如: @@ -74,10 +68,10 @@ import sensor from "@ohos.sensor"; try { sensor.once(sensor.SensorId.ACCELEROMETER, function(data) { - console.info("Data obtained successfully. x: " + data.x + "y: " + data.y + "z: " + data.z);// 获取数据成功 + console.info("Data obtained successfully. x: " + data.x + "y: " + data.y + "z: " + data.z); // 获取数据成功 }); } catch (error) { - console.error("Get sensor data fail"); + console.error("Get sensor data error. data:" + error.data, " msg:", error.message); } ``` ## 相关实例 diff --git a/zh-cn/application-dev/device/sensor-overview.md b/zh-cn/application-dev/device/sensor-overview.md index 6247d9b85739ee2f73785d35865e7ccb7247adbf..080201ffa57bdc89cd2ca4463b14906d6d939089 100644 --- a/zh-cn/application-dev/device/sensor-overview.md +++ b/zh-cn/application-dev/device/sensor-overview.md @@ -56,5 +56,4 @@ OpenHarmony传感器包含如下四个模块:Sensor API、Sensor Framework、S | 计步器 | ohos.permission.ACTIVITY_MOTION | user_grant | 允许订阅运动状态 | | 心率计 | ohos.permission.READ_HEALTH_DATA | user_grant | 允许读取健康数据 | -2. 传感器数据订阅和取消订阅接口成对调用,当不再需要订阅传感器数据时,开发者需要调用取消订阅接口停止数据上报。 - \ No newline at end of file +2. 传感器数据订阅和取消订阅接口成对调用,当不再需要订阅传感器数据时,开发者需要调用取消订阅接口停止数据上报。 \ No newline at end of file