• Home
Name Date Size #Lines LOC

..--

figures/12-May-2024-

frameworks/native/sensor/12-May-2024-2,3561,869

interfaces/12-May-2024-15,12911,339

rust/utils/socket_ipc_rust_ffi/12-May-2024-1,6801,163

sa_profile/12-May-2024-3229

services/sensor/12-May-2024-5,4984,556

test/unittest/common/12-May-2024-309250

utils/12-May-2024-3,4582,603

CODEOWNERSD12-May-2024795 1716

LICENSED12-May-202410.1 KiB177150

OAT.xmlD12-May-20247.7 KiB8362

README.mdD12-May-202412.1 KiB182146

README_zh.mdD12-May-202410.6 KiB182146

bundle.jsonD12-May-20242.1 KiB7272

hisysevent.yamlD12-May-20242 KiB5131

sensor.gniD12-May-20241 KiB3528

README.md

1# Sensor<a name="EN-US_TOPIC_0000001148682248"></a>
2
3-   [Introduction](#section11660541593)
4-   [Directory Structure](#section44981327519)
5-   [Constraints](#section98068674513)
6-   [Usage](#section1581412211528)
7    -   [Available APIs](#section15684191115524)
8    -   [How to Use](#section1543714111810)
9
10-   [Repositories Involved](#section96071132185310)
11
12## Introduction<a name="section11660541593"></a>
13
14A sensor is a device to detect events or changes in an environment and send messages about the events or changes to another device \(for example, a CPU\). Generally, a sensor is composed of sensitive components and conversion components. Sensors are the cornerstone of the IoT. A unified sensor management framework is required to achieve data sensing at a low latency and low power consumption, thereby keeping up with requirements of "1+8+N" products or business in the Seamless AI Life Strategy. Based on the usage, sensors are divided into the following categories:
15
16-   Motion: acceleration sensors, gyroscope sensors, gravity sensors, linear acceleration sensors, etc.
17-   Orientation: rotation vector sensors, orientation sensors, etc.
18-   Environment: magnetic field sensors, barometric pressure sensors, humidity sensors, etc.
19-   Light: ambient light sensors, proximity sensors, color temperature sensors, etc.
20-   Body: heart rate sensors, heartbeat sensors, etc.
21-   Other: Hall effect sensors, grip detection sensors, etc.
22
23The following figure shows the sensor architecture.
24
25**Figure  1**  Sensor architecture<a name="fig18632347122018"></a>
26
27
28![](figures/en-us_image_0000001199027515.png)
29
30## Directory Structure<a name="section44981327519"></a>
31
32The sample code for importing the sensor module is as follows:
33
34```
35/base/sensors/sensor
36├── frameworks                 # Framework code
37│   └── native                 # Sensor client code
38├── interfaces                 # External APIs
39│   ├── native                 # Native Implementation for sensors
40│   └── plugin                 # JS APIs
41├── sa_profile                 # Configuration file of system ability names and dynamic libraries
42├── services                   # Code of services
43│   └── sensor                 # Sensor service for reporting data about sensors, such as the acceleration and gyroscope sensors
44└── utils                      # Common code, including permissions and communication capabilities
45```
46
47## Constraints<a name="section98068674513"></a>
48
49-   To use sensor functions, ensure that the device where your application runs has the required sensor components.
50
51-   To obtain data of some sensors, you need to request the required permissions.
52
53    **Table  1**  Permissions required by sensors
54
55    <a name="table1379101653916"></a>
56    <table><thead align="left"><tr id="row137911161397"><th class="cellrowborder" valign="top" width="23.46765323467653%" id="mcps1.2.5.1.1"><p id="p15138132134118"><a name="p15138132134118"></a><a name="p15138132134118"></a>Sensor</p>
57    </th>
58    <th class="cellrowborder" valign="top" width="27.61723827617238%" id="mcps1.2.5.1.2"><p id="p2798169398"><a name="p2798169398"></a><a name="p2798169398"></a>Permission Name</p>
59    </th>
60    <th class="cellrowborder" valign="top" width="13.358664133586636%" id="mcps1.2.5.1.3"><p id="p14791716163914"><a name="p14791716163914"></a><a name="p14791716163914"></a>Sensitivity</p>
61    </th>
62    <th class="cellrowborder" valign="top" width="35.55644435556444%" id="mcps1.2.5.1.4"><p id="p379171633919"><a name="p379171633919"></a><a name="p379171633919"></a>Permission Description</p>
63    </th>
64    </tr>
65    </thead>
66    <tbody><tr id="row12791216173912"><td class="cellrowborder" valign="top" width="23.46765323467653%" headers="mcps1.2.5.1.1 "><p id="p41385264116"><a name="p41385264116"></a><a name="p41385264116"></a>Acceleration sensor, uncalibrated acceleration sensor, and linear acceleration sensor</p>
67    </td>
68    <td class="cellrowborder" valign="top" width="27.61723827617238%" headers="mcps1.2.5.1.2 "><p id="p177941653914"><a name="p177941653914"></a><a name="p177941653914"></a>ohos.permission.ACCELEROMETER</p>
69    </td>
70    <td class="cellrowborder" valign="top" width="13.358664133586636%" headers="mcps1.2.5.1.3 "><p id="p207991683914"><a name="p207991683914"></a><a name="p207991683914"></a>system_grant</p>
71    </td>
72    <td class="cellrowborder" valign="top" width="35.55644435556444%" headers="mcps1.2.5.1.4 "><p id="p118061614399"><a name="p118061614399"></a><a name="p118061614399"></a>Allows an application to subscribe to data of these acceleration-related sensors.</p>
73    </td>
74    </tr>
75    <tr id="row480116123911"><td class="cellrowborder" valign="top" width="23.46765323467653%" headers="mcps1.2.5.1.1 "><p id="p1213992174111"><a name="p1213992174111"></a><a name="p1213992174111"></a>Gyroscope sensor and uncalibrated gyroscope sensor</p>
76    </td>
77    <td class="cellrowborder" valign="top" width="27.61723827617238%" headers="mcps1.2.5.1.2 "><p id="p13807165395"><a name="p13807165395"></a><a name="p13807165395"></a>ohos.permission.GYROSCOPE</p>
78    </td>
79    <td class="cellrowborder" valign="top" width="13.358664133586636%" headers="mcps1.2.5.1.3 "><p id="p188031617391"><a name="p188031617391"></a><a name="p188031617391"></a>system_grant</p>
80    </td>
81    <td class="cellrowborder" valign="top" width="35.55644435556444%" headers="mcps1.2.5.1.4 "><p id="p4804168392"><a name="p4804168392"></a><a name="p4804168392"></a>Allows an application to subscribe to data of these gyroscope-related sensors.</p>
82    </td>
83    </tr>
84    <tr id="row68051693916"><td class="cellrowborder" valign="top" width="23.46765323467653%" headers="mcps1.2.5.1.1 "><p id="p191397210413"><a name="p191397210413"></a><a name="p191397210413"></a>Pedometer sensor</p>
85    </td>
86    <td class="cellrowborder" valign="top" width="27.61723827617238%" headers="mcps1.2.5.1.2 "><p id="p9802016183911"><a name="p9802016183911"></a><a name="p9802016183911"></a>ohos.permission.ACTIVITY_MOTION</p>
87    </td>
88    <td class="cellrowborder" valign="top" width="13.358664133586636%" headers="mcps1.2.5.1.3 "><p id="p11801216143916"><a name="p11801216143916"></a><a name="p11801216143916"></a>user_grant</p>
89    </td>
90    <td class="cellrowborder" valign="top" width="35.55644435556444%" headers="mcps1.2.5.1.4 "><p id="p48010163399"><a name="p48010163399"></a><a name="p48010163399"></a>Allows an application to subscribe to the motion status.</p>
91    </td>
92    </tr>
93    <tr id="row20323104234015"><td class="cellrowborder" valign="top" width="23.46765323467653%" headers="mcps1.2.5.1.1 "><p id="p3139172144118"><a name="p3139172144118"></a><a name="p3139172144118"></a>Heart rate sensor</p>
94    </td>
95    <td class="cellrowborder" valign="top" width="27.61723827617238%" headers="mcps1.2.5.1.2 "><p id="p7323942174013"><a name="p7323942174013"></a><a name="p7323942174013"></a>ohos.permission.READ_HEALTH_DATA</p>
96    </td>
97    <td class="cellrowborder" valign="top" width="13.358664133586636%" headers="mcps1.2.5.1.3 "><p id="p1232314214407"><a name="p1232314214407"></a><a name="p1232314214407"></a>user_grant</p>
98    </td>
99    <td class="cellrowborder" valign="top" width="35.55644435556444%" headers="mcps1.2.5.1.4 "><p id="p12323144254018"><a name="p12323144254018"></a><a name="p12323144254018"></a>Allows an application to read health data.</p>
100    </td>
101    </tr>
102    </tbody>
103    </table>
104
105
106## Usage<a name="section1581412211528"></a>
107
108This section uses the sensor JS APIs as an example to describe their functionalities and usage.
109
110### Available APIs<a name="section15684191115524"></a>
111
112The sensor JS APIs listen for sensor data changes. If an API is called multiple times, the last call takes effect. The following table describes these APIs.
113
114**Table  2**  Sensor JS APIs
115
116<a name="table13821942165419"></a>
117<table><thead align="left"><tr id="row6821194225417"><th class="cellrowborder" valign="top" width="40.11%" id="mcps1.2.3.1.1"><p id="p3822542145420"><a name="p3822542145420"></a><a name="p3822542145420"></a>API</p>
118</th>
119<th class="cellrowborder" valign="top" width="59.89%" id="mcps1.2.3.1.2"><p id="p0822942175411"><a name="p0822942175411"></a><a name="p0822942175411"></a>Description</p>
120</th>
121</tr>
122</thead>
123<tbody><tr id="row1782284211544"><td class="cellrowborder" valign="top" width="40.11%" headers="mcps1.2.3.1.1 "><p id="p18666622171316"><a name="p18666622171316"></a><a name="p18666622171316"></a>on(type: SensorType, callback: Callback&lt;Response&gt;, options?: Options)</p>
124</td>
125<td class="cellrowborder" valign="top" width="59.89%" headers="mcps1.2.3.1.2 "><p id="p1476355071517"><a name="p1476355071517"></a><a name="p1476355071517"></a>Subscribes to a type of sensor that listens for changes of sensor data. <strong id="b14384115423516"><a name="b14384115423516"></a><a name="b14384115423516"></a>SensorType</strong> indicates the type of the sensor that can be subscribed to. <strong id="b6384145413356"><a name="b6384145413356"></a><a name="b6384145413356"></a>callback</strong> specifies whether the subscription is successful. <strong id="b562874515315"><a name="b562874515315"></a><a name="b562874515315"></a>options</strong> indicates the interval for reporting sensor data.</p>
126</td>
127</tr>
128<tr id="row1489993155613"><td class="cellrowborder" valign="top" width="40.11%" headers="mcps1.2.3.1.1 "><p id="p1490010315564"><a name="p1490010315564"></a><a name="p1490010315564"></a>once(type: SensorType, callback: Callback&lt;Response&gt;)</p>
129</td>
130<td class="cellrowborder" valign="top" width="59.89%" headers="mcps1.2.3.1.2 "><p id="p5900163115564"><a name="p5900163115564"></a><a name="p5900163115564"></a>Subscribes to a type of sensor that listens for the sensor data change once. <strong id="b16369115653114"><a name="b16369115653114"></a><a name="b16369115653114"></a>SensorType</strong> indicates the type of the sensor that can be subscribed to. <strong id="b2370135693114"><a name="b2370135693114"></a><a name="b2370135693114"></a>callback</strong> specifies whether the subscription is successful.</p>
131</td>
132</tr>
133<tr id="row1909165317562"><td class="cellrowborder" valign="top" width="40.11%" headers="mcps1.2.3.1.1 "><p id="p16909135319564"><a name="p16909135319564"></a><a name="p16909135319564"></a>off(type: SensorType, callback: Callback&lt;Response&gt;)</p>
134</td>
135<td class="cellrowborder" valign="top" width="59.89%" headers="mcps1.2.3.1.2 "><p id="p3909165315613"><a name="p3909165315613"></a><a name="p3909165315613"></a>Unsubscribes from a type of sensor that listens for data changes. <strong id="b63331753103316"><a name="b63331753103316"></a><a name="b63331753103316"></a>SensorType</strong> indicates the type of the sensor that can be unsubscribed from. <strong id="b153331453173315"><a name="b153331453173315"></a><a name="b153331453173315"></a>callback</strong> specifies whether the unsubscription is successful.</p>
136</td>
137</tr>
138</tbody>
139</table>
140
141
142
143### How to Use<a name="section1543714111810"></a>
144
1451.  Import the sensor package.
1462.  Subscribe to and listen for data changes of an acceleration sensor.
1473.  Unsubscribe from data changes of the acceleration sensor.
1484.  Subscribe to and listen for a data change of an acceleration sensor.
149
150Example:
151
152```
153// Step 1 Import the sensor package.
154import sensor from '@ohos.sensor';
155export default {
156    onCreate() {
157        // Step 2 Subscribe to and listen for data changes of a type of sensor.
158        sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, (data) => {
159            console.info("Acceleration data obtained. x: " + data.x + "; y: " + data.y + "; z: " + data.z);
160        }, {'interval':200000000});
161        // Step 3 Unsubscribe from data changes of the sensor 10 seconds later.
162        sensor.off(sensor.SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION);
163        // Step 4 Subscribe to and listen for a data change of a type of sensor.
164        sensor.once(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, (data) => {
165            console.info("Acceleration data obtained. x: " + data.x + "; y: " + data.y + "; z: " + data.z);
166       });
167    }
168    onDestroy() {
169        console.info('AceApplication onDestroy');
170    }
171}
172```
173
174## Repositories Involved<a name="section96071132185310"></a>
175
176Pan-sensor subsystem
177
178**sensors\_sensor**
179
180[sensors\_miscdevice](https://gitee.com/openharmony/sensors_miscdevice)
181
182

README_zh.md

1# Sensor组件<a name="ZH-CN_TOPIC_0000001148682248"></a>
2
3-   [简介](#section11660541593)
4-   [目录](#section44981327519)
5-   [约束](#section98068674513)
6-   [使用](#section1581412211528)
7    -   [接口说明](#section15684191115524)
8    -   [使用说明](#section1543714111810)
9
10-   [相关仓](#section96071132185310)
11
12## 简介<a name="section11660541593"></a>
13
14传感器是指用于侦测环境中所发生事件或变化,并将此消息发送至其他电子设备(如中央处理器)的设备,通常由敏感组件和转换组件组成。传感器是实现物联网智能化的重要基石,为实现全场景智慧化战略,支撑“1+8+N”产品需求,需要构筑统一的传感器管理框架,达到为各产品/业务提供低时延、低功耗的感知数据的目的。根据用途可分为以下六大类:
15
16-   运动类:加速度、陀螺仪、重力、线性加速度传感器等
17-   姿态类:旋转矢量、方向传感器等
18-   环境类:磁力计、气压、湿度传感器等
19-   光线类:环境光、接近光、色温传感器等
20-   健康类:心率、心跳传感器等
21-   其它:霍尔传感器、手握传感器等
22
23传感器架构图如下所示:
24
25**图 1**  Sensor架构图<a name="fig18632347122018"></a>
26
27
28![](figures/zh-cn_image_0000001199027515.png)
29
30## 目录<a name="section44981327519"></a>
31
32sensor导入模块的示例代码如下:
33
34```
35/base/sensors/sensor
36├── frameworks                 # 框架代码
37│   └── native                 # sensor客户端代码
38├── interfaces                 # 对外接口存放目录
39│   ├── native                 # sensor native实现
40│   └── plugin                 # Js API
41├── sa_profile                 # 服务名称和服务的动态库的配置文件
42├── services                   # 服务的代码目录
43│   └── sensor                 # 传感器服务,包括加速度、陀螺仪等,上报传感器数据
44└── utils                      # 公共代码,包括权限、通信等能力
45```
46
47## 约束<a name="section98068674513"></a>
48
49-   要使用传感器的功能,设备必须具有对应的传感器器件。
50
51-   针对某些传感器,开发者需要请求相应的权限,才能获取到相应传感器的数据。
52
53    **表 1**  传感器权限列表
54
55    <a name="table1379101653916"></a>
56    <table><thead align="left"><tr id="row137911161397"><th class="cellrowborder" valign="top" width="23.46765323467653%" id="mcps1.2.5.1.1"><p id="p15138132134118"><a name="p15138132134118"></a><a name="p15138132134118"></a>传感器</p>
57    </th>
58    <th class="cellrowborder" valign="top" width="27.61723827617238%" id="mcps1.2.5.1.2"><p id="p2798169398"><a name="p2798169398"></a><a name="p2798169398"></a>权限名</p>
59    </th>
60    <th class="cellrowborder" valign="top" width="13.358664133586636%" id="mcps1.2.5.1.3"><p id="p14791716163914"><a name="p14791716163914"></a><a name="p14791716163914"></a>敏感级别</p>
61    </th>
62    <th class="cellrowborder" valign="top" width="35.55644435556444%" id="mcps1.2.5.1.4"><p id="p379171633919"><a name="p379171633919"></a><a name="p379171633919"></a>权限描述</p>
63    </th>
64    </tr>
65    </thead>
66    <tbody><tr id="row12791216173912"><td class="cellrowborder" valign="top" width="23.46765323467653%" headers="mcps1.2.5.1.1 "><p id="p41385264116"><a name="p41385264116"></a><a name="p41385264116"></a>加速度传感器、加速度未校准传感器、线性加速度传感器</p>
67    </td>
68    <td class="cellrowborder" valign="top" width="27.61723827617238%" headers="mcps1.2.5.1.2 "><p id="p177941653914"><a name="p177941653914"></a><a name="p177941653914"></a>ohos.permission.ACCELEROMETER</p>
69    </td>
70    <td class="cellrowborder" valign="top" width="13.358664133586636%" headers="mcps1.2.5.1.3 "><p id="p207991683914"><a name="p207991683914"></a><a name="p207991683914"></a>system_grant</p>
71    </td>
72    <td class="cellrowborder" valign="top" width="35.55644435556444%" headers="mcps1.2.5.1.4 "><p id="p118061614399"><a name="p118061614399"></a><a name="p118061614399"></a>允许订阅加速度传感器的数据。</p>
73    </td>
74    </tr>
75    <tr id="row480116123911"><td class="cellrowborder" valign="top" width="23.46765323467653%" headers="mcps1.2.5.1.1 "><p id="p1213992174111"><a name="p1213992174111"></a><a name="p1213992174111"></a>陀螺仪传感器、陀螺仪未校准传感器</p>
76    </td>
77    <td class="cellrowborder" valign="top" width="27.61723827617238%" headers="mcps1.2.5.1.2 "><p id="p13807165395"><a name="p13807165395"></a><a name="p13807165395"></a>ohos.permission.GYROSCOPE</p>
78    </td>
79    <td class="cellrowborder" valign="top" width="13.358664133586636%" headers="mcps1.2.5.1.3 "><p id="p188031617391"><a name="p188031617391"></a><a name="p188031617391"></a>system_grant</p>
80    </td>
81    <td class="cellrowborder" valign="top" width="35.55644435556444%" headers="mcps1.2.5.1.4 "><p id="p4804168392"><a name="p4804168392"></a><a name="p4804168392"></a>允许订阅陀螺仪传感器的数据。</p>
82    </td>
83    </tr>
84    <tr id="row68051693916"><td class="cellrowborder" valign="top" width="23.46765323467653%" headers="mcps1.2.5.1.1 "><p id="p191397210413"><a name="p191397210413"></a><a name="p191397210413"></a>计步器</p>
85    </td>
86    <td class="cellrowborder" valign="top" width="27.61723827617238%" headers="mcps1.2.5.1.2 "><p id="p9802016183911"><a name="p9802016183911"></a><a name="p9802016183911"></a>ohos.permission.ACTIVITY_MOTION</p>
87    </td>
88    <td class="cellrowborder" valign="top" width="13.358664133586636%" headers="mcps1.2.5.1.3 "><p id="p11801216143916"><a name="p11801216143916"></a><a name="p11801216143916"></a>user_grant</p>
89    </td>
90    <td class="cellrowborder" valign="top" width="35.55644435556444%" headers="mcps1.2.5.1.4 "><p id="p48010163399"><a name="p48010163399"></a><a name="p48010163399"></a>允许订阅运动状态。</p>
91    </td>
92    </tr>
93    <tr id="row20323104234015"><td class="cellrowborder" valign="top" width="23.46765323467653%" headers="mcps1.2.5.1.1 "><p id="p3139172144118"><a name="p3139172144118"></a><a name="p3139172144118"></a>心率传感器</p>
94    </td>
95    <td class="cellrowborder" valign="top" width="27.61723827617238%" headers="mcps1.2.5.1.2 "><p id="p7323942174013"><a name="p7323942174013"></a><a name="p7323942174013"></a>ohos.permission.READ_HEALTH_DATA</p>
96    </td>
97    <td class="cellrowborder" valign="top" width="13.358664133586636%" headers="mcps1.2.5.1.3 "><p id="p1232314214407"><a name="p1232314214407"></a><a name="p1232314214407"></a>user_grant</p>
98    </td>
99    <td class="cellrowborder" valign="top" width="35.55644435556444%" headers="mcps1.2.5.1.4 "><p id="p12323144254018"><a name="p12323144254018"></a><a name="p12323144254018"></a>允许读取健康数据。</p>
100    </td>
101    </tr>
102    </tbody>
103    </table>
104
105
106## 使用<a name="section1581412211528"></a>
107
108本节以传感器 JS API为例,说明其提供的具体功能以及使用流程。
109
110### 接口说明<a name="section15684191115524"></a>
111
112传感器 JS API:监听传感器数据变化,如果多次调用该接口,则最后一次调用生效。JS API开放的能力如下:
113
114**表 2**  JS API的主要接口
115
116<a name="table13821942165419"></a>
117<table><thead align="left"><tr id="row6821194225417"><th class="cellrowborder" valign="top" width="40.11%" id="mcps1.2.3.1.1"><p id="p3822542145420"><a name="p3822542145420"></a><a name="p3822542145420"></a>接口名</p>
118</th>
119<th class="cellrowborder" valign="top" width="59.89%" id="mcps1.2.3.1.2"><p id="p0822942175411"><a name="p0822942175411"></a><a name="p0822942175411"></a>描述</p>
120</th>
121</tr>
122</thead>
123<tbody><tr id="row1782284211544"><td class="cellrowborder" valign="top" width="40.11%" headers="mcps1.2.3.1.1 "><p id="p18666622171316"><a name="p18666622171316"></a><a name="p18666622171316"></a>on(type: SensorType, callback: Callback&lt;Response&gt;, options?: Options)</p>
124</td>
125<td class="cellrowborder" valign="top" width="59.89%" headers="mcps1.2.3.1.2 "><p id="p1476355071517"><a name="p1476355071517"></a><a name="p1476355071517"></a>监听传感器数据变化。SensorType为支持订阅的传感器类型,callback表示订阅传感器的回调函数,options为设置传感器数据上报的时间间隔。</p>
126</td>
127</tr>
128<tr id="row1489993155613"><td class="cellrowborder" valign="top" width="40.11%" headers="mcps1.2.3.1.1 "><p id="p1490010315564"><a name="p1490010315564"></a><a name="p1490010315564"></a>once(type: SensorType, callback: Callback&lt;Response&gt;)</p>
129</td>
130<td class="cellrowborder" valign="top" width="59.89%" headers="mcps1.2.3.1.2 "><p id="p5900163115564"><a name="p5900163115564"></a><a name="p5900163115564"></a>监听传感器数据变化一次。SensorType为支持订阅的传感器类型,callback表示订阅传感器的回调函数。</p>
131</td>
132</tr>
133<tr id="row1909165317562"><td class="cellrowborder" valign="top" width="40.11%" headers="mcps1.2.3.1.1 "><p id="p16909135319564"><a name="p16909135319564"></a><a name="p16909135319564"></a>off(type: SensorType, callback: Callback&lt;Response&gt;)</p>
134</td>
135<td class="cellrowborder" valign="top" width="59.89%" headers="mcps1.2.3.1.2 "><p id="p3909165315613"><a name="p3909165315613"></a><a name="p3909165315613"></a>取消订阅传感器数据。SensorType为支持的取消订阅的传感器类型,callback表示取消订阅传感器是否成功。</p>
136</td>
137</tr>
138</tbody>
139</table>
140
141
142
143### 使用说明<a name="section1543714111810"></a>
144
1451.  导包。
1462.  注册并监听加速度传感器数据的变化。
1473.  取消订阅加速度传感器数据的变化。
1484.  注册并监听加速度传感器数据的变化一次。
149
150示例代码:
151
152```
153//步骤1 导包
154import sensor from '@ohos.sensor';
155export default {
156    onCreate() {
157        //步骤2 监听传感器数据变化,并注册传感器类型
158        sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, (data) => {
159            console.info("Acceleration data obtained. x: " + data.x + "; y: " + data.y + "; z: " + data.z);
160        }, {'interval':200000000});
161        //步骤3 设置10秒后取消订阅传感器数据
162        sensor.off(sensor.SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION);
163        //步骤4 监听传感器数据变化一次,并注册传感器类型
164        sensor.once(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, (data) => {
165            console.info("Acceleration data obtained. x: " + data.x + "; y: " + data.y + "; z: " + data.z);
166       });
167    }
168    onDestroy() {
169        console.info('AceApplication onDestroy');
170    }
171}
172```
173
174## 相关仓<a name="section96071132185310"></a>
175
176泛Sensor服务子系统
177
178**sensors\_sensor**
179
180[sensors\_miscdevice](https://gitee.com/openharmony/sensors_miscdevice)
181
182