华为手表开发:WATCH 3 Pro(20)传感器订阅 佩戴状态

  • 环境与设备
  • 鸿蒙开发
  • 文件夹:
  • 文件
  • 新增展示的文本标记
  • index.hml
  • index.css
  • index.js

希望能写一些简单的教程和案例分享给需要的人

鸿蒙可穿戴开发

环境与设备

系统:window
设备:HUAWEI WATCH 3 Pro New
开发工具:DevEco Studio 3.0.0.800

鸿蒙开发

文件夹:

entry:项目文件夹
js:前端文件夹
pages:页面文件夹
index:首页文件夹

文件

index.css:首页样式
index.hml:首页
index.js:首页脚本
config.json:配置文件

新增展示的文本标记

代码如下:

index.hml

<div class="container">
    <text class="title">
        设备佩戴 :{{ OnBodyState }}
    </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: 20px;
        color: #FFFFFF;
    }
}

index.js

导入功能包:system.sensor
import sensor from ‘@system.sensor’;

import sensor from '@system.sensor';

export default {
    data: {
        OnBodyState: "",
    },
    onInit() {
        let currJS = this;
        sensor.subscribeOnBodyState({
            success: function (data) {
                console.info('dao::订阅设备佩戴状态:' + JSON.stringify(data));
                if (data.value) {
                    currJS.OnBodyState = "是";
                } else {
                    currJS.OnBodyState = "否";
                }
            },
            fail: function (data, code) {
                console.info('dao::订阅设备佩戴状态报错 failed. Code: ' + code + '; Data: ' + data);
            },
        });
    }
}

需要 demo(代码) 的留下邮箱,或者留言提需要什么样的 demo