华为手表开发:WATCH 3 Pro(15)传感器订阅加速度计
华为手表开发:WATCH 3 Pro(15)传感器订阅加速度计
- 初
- 环境与设备
-
-
- 加速度传感器介绍与说明
- 鸿蒙开发
-
- 文件夹:
- 文件
-
- 重点
-
-
- 新增展示的文本标记
-
- index.hml
- index.css
- index.js
-
初
希望能写一些简单的教程和案例分享给需要的人
鸿蒙可穿戴开发
环境与设备
系统:window
设备:HUAWEI WATCH 3 Pro New
开发工具:DevEco Studio 3.0.0.800
加速度传感器介绍与说明
加速度计(Accelerometer)是一种用于测量物体加速度的传感器。它通常通过使用质量与力之间的关系,来检测物体的加速度变化。加速度计可以用于许多应用,如智能手机、运动追踪设备、虚拟现实头盔、汽车安全气囊、航空航天器和工业自动化等。在智能手机中,加速度计通常用于检测设备的方向、位置和运动,从而实现屏幕旋转、步数计数、手势识别等功能。加速度计通常使用微电机制作,通过测量微小的电容或电阻变化来检测加速度。
鸿蒙开发
文件夹:
entry:项目文件夹
js:前端文件夹
pages:页面文件夹
index:首页文件夹
文件
index.css:首页样式
index.hml:首页
index.js:首页脚本
config.json:配置文件
重点
获取加速度计传感器,需要权限,这个必须注意:都在 config.json 增加
权限的代码:
"reqPermissions": [{"reason": "加速度计传感器","name": "ohos.permission.ACCELEROMETER"},],
如果没加就会获取不到 ,所以一定一定要加上。
新增展示的文本标记
代码如下:
index.hml
<div class="container"><text class="title">加速度计 X :{{ accelerometer_x }}</text><text class="title">加速度计 Y :{{ accelerometer_y }}</text><text class="title">加速度计 Z :{{ accelerometer_z }}</text>
</div>
index.css
.container {flex-direction: column;justify-content: center;align-items: center;width: 100%;height: 100%;
}.title {font-size: 40px;color: #000000;opacity: 0.9;
}@media screen and (device-type: wearable) {.title {font-size: 15px;color: #FFFFFF;}
}
index.js
导入功能包:system.sensor
import sensor from ‘@system.sensor’;
import sensor from '@system.sensor';export default {data: {accelerometer_x: "",accelerometer_y: "",accelerometer_z: ""},onInit() {let currJS = this;sensor.subscribeAccelerometer({success: function (data) {console.info('dao::接收到加速度计传感器数据:' + JSON.stringify(data));currJS.accelerometer_x = data.x;currJS.accelerometer_y = data.y;currJS.accelerometer_z = data.z;},fail: function (data, code) {console.info('dao::加速度计报错 failed. Code: ' + code + '; Data: ' + data);},});}
}
需要 demo(代码) 的留下邮箱,或者留言提需要什么样的 demo