未验证 提交 59fb0c95 编写于 作者: O openharmony_ci 提交者: Gitee

!12378 Modify sensor data development

Merge pull request !12378 from liyaoyao/master
......@@ -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);
}
```
## 相关实例
......
......@@ -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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册