• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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