提交 a40b9869 编写于 作者: W wusongqing

update docs against 6313+6772

Signed-off-by: Nwusongqing <wusongqing@huawei.com>
上级 749bca31
# Sensor # Sensor
The **Sensor** module provides APIs for subscribing to and unsubscribing from sensor data, and obtaining the sensor list. It also provides common sensor algorithm APIs, for example, APIs for obtaining the altitude based on the atmospheric pressure and obtaining the device orientation based on the rotation matrix.
A 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 Internet of Things (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:
- Motion: acceleration sensors, gyroscope sensors, gravity sensors, linear acceleration sensors, and more
- Orientation: rotation vector sensors, orientation sensors, and more
- Environment: magnetic field sensors, barometric pressure sensors, humidity sensors, and more
- Light: ambient light sensors, proximity sensors, color temperature sensors, and more
- Body: heart rate sensors, heartbeat sensors, and more
- Other: Hall effect sensors, grip detection sensors, and more
> **NOTE** > **NOTE**
> >
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version. > The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
...@@ -23,8 +34,8 @@ Subscribes to data changes of the acceleration sensor. If this API is called mul ...@@ -23,8 +34,8 @@ Subscribes to data changes of the acceleration sensor. If this API is called mul
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor
**Parameters** **Parameters**
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- | | -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type | [SensorType](#sensortype) | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_ACCELEROMETER**.| | type | [SensorType](#sensortype) | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_ACCELEROMETER**.|
...@@ -42,12 +53,14 @@ Subscribes to data changes of the acceleration sensor. If this API is called mul ...@@ -42,12 +53,14 @@ Subscribes to data changes of the acceleration sensor. If this API is called mul
); );
``` ```
### LINEAR_ACCELERATION ### LINEAR_ACCELERATION<sup>deprecated</sup>
on(type: SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION,callback:Callback&lt;LinearAccelerometerResponse&gt;, options?: Options): void on(type: SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION,callback:Callback&lt;LinearAccelerometerResponse&gt;, options?: Options): void
Subscribes to data changes of the linear acceleration sensor. If this API is called multiple times for the same application, the last call takes effect. Subscribes to data changes of the linear acceleration sensor. If this API is called multiple times for the same application, the last call takes effect.
This API is deprecated since API version 9. You are advised to use **Sensor.on.LINEAR_ACCELEROMETER<sup>9+</sup>** instead.
**Required permissions**: ohos.permission.ACCELEROMETER (a system permission) **Required permissions**: ohos.permission.ACCELEROMETER (a system permission)
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor
...@@ -70,6 +83,35 @@ Subscribes to data changes of the linear acceleration sensor. If this API is cal ...@@ -70,6 +83,35 @@ Subscribes to data changes of the linear acceleration sensor. If this API is cal
); );
``` ```
### LINEAR_ACCELEROMETER<sup>9+</sup>
on(type: SensorType.SENSOR_TYPE_ID_LINEAR_ACCELEROMETER,callback:Callback&lt;LinearAccelerometerResponse&gt;, options?: Options): void
Subscribes to data changes of the linear acceleration sensor. If this API is called multiple times for the same application, the last call takes effect.
**Required permissions**: ohos.permission.ACCELEROMETER (a system permission)
**System capability**: SystemCapability.Sensors.Sensor
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type | [SensorType](#sensortype) | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_LINEAR_ACCELEROMETER**.|
| callback | Callback&lt;[LinearAccelerometerResponse](#linearaccelerometerresponse)&gt; | Yes | Callback used to return the linear acceleration sensor data. The reported data type in the callback is **LinearAccelerometerResponse**.|
| options | [Options](#options) | No | Interval at which the callback is invoked to return the sensor data. The default value is 200,000,000 ns. |
**Example**
```js
sensor.on(sensor.SensorType.SENSOR_TYPE_ID_LINEAR_ACCELEROMETER,function(data){
console.info('X-coordinate component: ' + data.x);
console.info('Y-coordinate component: ' + data.y);
console.info('Z-coordinate component: ' + data.z);
},
{interval: 10000000}
);
```
### ACCELEROMETER_UNCALIBRATED ### ACCELEROMETER_UNCALIBRATED
on(type: SensorType.SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED,callback: Callback&lt;AccelerometerUncalibratedResponse&gt;, options?: Options): void on(type: SensorType.SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED,callback: Callback&lt;AccelerometerUncalibratedResponse&gt;, options?: Options): void
...@@ -487,12 +529,14 @@ Subscribes to data changes of the orientation sensor. If this API is called mult ...@@ -487,12 +529,14 @@ Subscribes to data changes of the orientation sensor. If this API is called mult
); );
``` ```
### HEART_RATE ### HEART_RATE<sup>deprecated</sup>
on(type: SensorType.SENSOR_TYPE_ID_HEART_RATE, callback: Callback&lt;HeartRateResponse&gt;, options?: Options): void on(type: SensorType.SENSOR_TYPE_ID_HEART_RATE, callback: Callback&lt;HeartRateResponse&gt;, options?: Options): void
Subscribes to only one data change of the heart rate sensor. Subscribes to only one data change of the heart rate sensor.
This API is deprecated since API version 9. You are advised to use **sensor.on.HEART_BEAT_RATE<sup>9+</sup>** instead.
**Required permissions**: ohos.permission.READ_HEALTH_DATA **Required permissions**: ohos.permission.READ_HEALTH_DATA
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor
...@@ -514,6 +558,33 @@ sensor.on(sensor.SensorType.SENSOR_TYPE_ID_HEART_RATE,function(data){ ...@@ -514,6 +558,33 @@ sensor.on(sensor.SensorType.SENSOR_TYPE_ID_HEART_RATE,function(data){
); );
``` ```
### HEART_BEAT_RATE<sup>9+</sup>
on(type: SensorType.SENSOR_TYPE_ID_HEART_BEAT_RATE, callback: Callback&lt;HeartRateResponse&gt;, options?: Options): void
Subscribes to only one data change of the heart rate sensor.
**Required permissions**: ohos.permission.READ_HEALTH_DATA
**System capability**: SystemCapability.Sensors.Sensor
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type | [SensorType](#sensortype) | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_HEART_BEAT_RATE**. |
| callback | Callback&lt;[HeartRateResponse](#heartrateresponse)&gt; | Yes | One-shot callback used to return the heart rate sensor data. The reported data type in the callback is **HeartRateResponse**.|
**Example**
```js
sensor.on(sensor.SensorType.SENSOR_TYPE_ID_HEART_BEAT_RATE,function(data){
console.info("Heart rate: " + data.heartRate);
},
{interval: 10000000}
);
```
### ROTATION_VECTOR ### ROTATION_VECTOR
on(type: SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR,callback: Callback&lt;RotationVectorResponse&gt;,options?: Options): void on(type: SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR,callback: Callback&lt;RotationVectorResponse&gt;,options?: Options): void
...@@ -593,12 +664,14 @@ Subscribes to only one data change of the acceleration sensor. ...@@ -593,12 +664,14 @@ Subscribes to only one data change of the acceleration sensor.
); );
``` ```
### LINEAR_ACCELERATION ### LINEAR_ACCELERATION<sup>deprecated</sup>
once(type: SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION,callback:Callback&lt;LinearAccelerometerResponse&gt;): void once(type: SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION,callback:Callback&lt;LinearAccelerometerResponse&gt;): void
Subscribes to only one data change of the linear acceleration sensor. Subscribes to only one data change of the linear acceleration sensor.
This API is deprecated since API version 9. You are advised to use **sensor.once.LINEAR_ACCELEROMETER<sup>9+</sup>** instead.
**Required permissions**: ohos.permission.ACCELEROMETER (a system permission) **Required permissions**: ohos.permission.ACCELEROMETER (a system permission)
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor
...@@ -619,6 +692,32 @@ Subscribes to only one data change of the linear acceleration sensor. ...@@ -619,6 +692,32 @@ Subscribes to only one data change of the linear acceleration sensor.
); );
``` ```
### LINEAR_ACCELEROMETER<sup>9+</sup>
once(type: SensorType.SENSOR_TYPE_ID_LINEAR_ACCELEROMETER,callback:Callback&lt;LinearAccelerometerResponse&gt;): void
Subscribes to only one data change of the linear acceleration sensor.
**Required permissions**: ohos.permission.ACCELEROMETER (a system permission)
**System capability**: SystemCapability.Sensors.Sensor
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type | [SensorType](#sensortype) | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_LINEAR_ACCELEROMETER**.|
| callback | Callback&lt;[LinearAccelerometerResponse](#linearaccelerometerresponse)&gt; | Yes | One-shot callback used to return the linear acceleration sensor data. The reported data type in the callback is **LinearAccelerometerResponse**.|
**Example**
```js
sensor.once(sensor.SensorType.SENSOR_TYPE_ID_LINEAR_ACCELEROMETER, function(data) {
console.info('X-coordinate component: ' + data.x);
console.info('Y-coordinate component: ' + data.y);
console.info('Z-coordinate component: ' + data.z);
}
);
```
### ACCELEROMETER_UNCALIBRATED ### ACCELEROMETER_UNCALIBRATED
once(type: SensorType.SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED,callback: Callback&lt;AccelerometerUncalibratedResponse&gt;): void once(type: SensorType.SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED,callback: Callback&lt;AccelerometerUncalibratedResponse&gt;): void
...@@ -1029,12 +1128,14 @@ Subscribes to only one data change of the rotation vector sensor. ...@@ -1029,12 +1128,14 @@ Subscribes to only one data change of the rotation vector sensor.
); );
``` ```
### HEART_RATE ### HEART_RATE<sup>deprecated</sup>
once(type: SensorType.SENSOR_TYPE_ID_HEART_RATE, callback: Callback&lt;HeartRateResponse&gt;): void once(type: SensorType.SENSOR_TYPE_ID_HEART_RATE, callback: Callback&lt;HeartRateResponse&gt;): void
Subscribes to only one data change of the heart rate sensor. Subscribes to only one data change of the heart rate sensor.
This API is deprecated since API version 9. You are advised to use **sensor.once.HEART_BEAT_RATE<sup>9+</sup>** instead.
**Required permissions**: ohos.permission.READ_HEALTH_DATA **Required permissions**: ohos.permission.READ_HEALTH_DATA
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor
...@@ -1053,6 +1154,30 @@ Subscribes to only one data change of the heart rate sensor. ...@@ -1053,6 +1154,30 @@ Subscribes to only one data change of the heart rate sensor.
); );
``` ```
### HEART_BEAT_RATE<sup>9+</sup>
once(type: SensorType.SENSOR_TYPE_ID_HEART_BEAT_RATE, callback: Callback&lt;HeartRateResponse&gt;): void
Subscribes to only one data change of the heart rate sensor.
**Required permissions**: ohos.permission.READ_HEALTH_DATA
**System capability**: SystemCapability.Sensors.Sensor
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type | [SensorType](#sensortype) | Yes | Type of the sensor to subscribe to, which is **SENSOR_TYPE_ID_HEART_BEAT_RATE**. |
| callback | Callback&lt;[HeartRateResponse](#heartrateresponse)&gt; | Yes | One-shot callback used to return the heart rate sensor data. The reported data type in the callback is **HeartRateResponse**.|
**Example**
```js
sensor.once(sensor.SensorType.SENSOR_TYPE_ID_HEART_BEAT_RATE, function(data) {
console.info("Heart rate: " + data.heartRate);
}
);
```
### WEAR_DETECTION ### WEAR_DETECTION
once(type: SensorType.SENSOR_TYPE_ID_WEAR_DETECTION, callback: Callback&lt;WearDetectionResponse&gt;): void once(type: SensorType.SENSOR_TYPE_ID_WEAR_DETECTION, callback: Callback&lt;WearDetectionResponse&gt;): void
...@@ -1314,12 +1439,14 @@ function callback(data) { ...@@ -1314,12 +1439,14 @@ function callback(data) {
sensor.off(sensor.SensorType.SENSOR_TYPE_ID_HALL, callback); sensor.off(sensor.SensorType.SENSOR_TYPE_ID_HALL, callback);
``` ```
### HEART_RATE ### HEART_RATE<sup>deprecated</sup>
off(type: SensorType.SENSOR_TYPE_ID_HEART_RATE, callback?: Callback&lt;HeartRateResponse&gt;): void off(type: SensorType.SENSOR_TYPE_ID_HEART_RATE, callback?: Callback&lt;HeartRateResponse&gt;): void
Unsubscribes from sensor data changes. Unsubscribes from sensor data changes.
This API is deprecated since API version 9. You are advised to use **sensor.off.HEART_BEAT_RATE<sup>9+</sup>** instead.
**Required permissions**: ohos.permission.READ_HEALTH_DATA **Required permissions**: ohos.permission.READ_HEALTH_DATA
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor
...@@ -1340,6 +1467,32 @@ function callback(data) { ...@@ -1340,6 +1467,32 @@ function callback(data) {
sensor.off(sensor.SensorType.SENSOR_TYPE_ID_HEART_RATE, callback); sensor.off(sensor.SensorType.SENSOR_TYPE_ID_HEART_RATE, callback);
``` ```
### HEART_BEAT_RATE<sup>9+</sup>
off(type: SensorType.SENSOR_TYPE_ID_HEART_BEAT_RATE, callback?: Callback&lt;HeartRateResponse&gt;): void
Unsubscribes from sensor data changes.
**Required permissions**: ohos.permission.READ_HEALTH_DATA
**System capability**: SystemCapability.Sensors.Sensor
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type | [SensorType](#sensortype)[SensorType](#sensortype) | Yes | Type of the sensor to unsubscribe from, which is **SENSOR_TYPE_ID_HEART_BEAT_RATE**.|
| callback | Callback&lt;[HeartRateResponse](#heartrateresponse)&gt; | Yes | One-shot callback used to return the heart rate sensor data. The reported data type in the callback is **HeartRateResponse**.|
**Example**
```js
function callback(data) {
console.info("Heart rate: " + data.heartRate);
}
sensor.off(sensor.SensorType.SENSOR_TYPE_ID_HEART_BEAT_RATE, callback);
```
### HUMIDITY ### HUMIDITY
off(type: SensorType.SENSOR_TYPE_ID_HUMIDITY, callback?: Callback&lt;HumidityResponse&gt;): void off(type: SensorType.SENSOR_TYPE_ID_HUMIDITY, callback?: Callback&lt;HumidityResponse&gt;): void
...@@ -1364,12 +1517,14 @@ function callback(data) { ...@@ -1364,12 +1517,14 @@ function callback(data) {
sensor.off(sensor.SensorType.SENSOR_TYPE_ID_HUMIDITY, callback); sensor.off(sensor.SensorType.SENSOR_TYPE_ID_HUMIDITY, callback);
``` ```
### LINEAR_ACCELERATION ### LINEAR_ACCELERATION<sup>deprecated</sup>
off(type: SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION, callback?: Callback&lt;LinearAccelerometerResponse&gt;): void off(type: SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION, callback?: Callback&lt;LinearAccelerometerResponse&gt;): void
Unsubscribes from sensor data changes. Unsubscribes from sensor data changes.
This API is deprecated since API version 9. You are advised to use **sensor.off.LINEAR_ACCELEROMETER<sup>9+</sup>** instead.
**Required permissions**: ohos.permission.ACCELEROMETER (a system permission) **Required permissions**: ohos.permission.ACCELEROMETER (a system permission)
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor
...@@ -1392,6 +1547,34 @@ function callback(data) { ...@@ -1392,6 +1547,34 @@ function callback(data) {
sensor.off(sensor.SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION, callback); sensor.off(sensor.SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION, callback);
``` ```
### LINEAR_ACCELEROMETER<sup>9+</sup>
off(type: SensorType.SENSOR_TYPE_ID_LINEAR_ACCELEROMETER, callback?: Callback&lt;LinearAccelerometerResponse&gt;): void
Unsubscribes from sensor data changes.
**Required permissions**: ohos.permission.ACCELEROMETER (a system permission)
**System capability**: SystemCapability.Sensors.Sensor
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type | [SensorType](#sensortype) | Yes | Type of the sensor to unsubscribe from, which is **SENSOR_TYPE_ID_LINEAR_ACCELEROMETER**.|
| callback | Callback&lt;[LinearAccelerometerResponse](#linearaccelerometerresponse)&gt; | Yes | Callback used to return the acceleration sensor data. The reported data type in the callback is **LinearAccelerometerResponse**.|
**Example**
```js
function callback(data) {
console.info('X-coordinate component: ' + data.x);
console.info('Y-coordinate component: ' + data.y);
console.info('Z-coordinate component: ' + data.z);
}
sensor.off(sensor.SensorType.SENSOR_TYPE_ID_LINEAR_ACCELEROMETER, callback);
```
### MAGNETIC_FIELD ### MAGNETIC_FIELD
off(type: SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD, callback?: Callback&lt;MagneticFieldResponse&gt;): void off(type: SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD, callback?: Callback&lt;MagneticFieldResponse&gt;): void
...@@ -2160,7 +2343,7 @@ Creates a rotation matrix based on the gravity vector and geomagnetic vector. Th ...@@ -2160,7 +2343,7 @@ Creates a rotation matrix based on the gravity vector and geomagnetic vector. Th
err.message); err.message);
return; return;
} }
for (var i=0; i < data.length; i++) { for (var i=0; i < data.rotation.length; i++) {
console.info("data[" + i + "]: " + data[i]) console.info("data[" + i + "]: " + data[i])
} }
}) })
...@@ -2194,7 +2377,7 @@ Creates a rotation matrix based on the gravity vector and geomagnetic vector. Th ...@@ -2194,7 +2377,7 @@ Creates a rotation matrix based on the gravity vector and geomagnetic vector. Th
const promise = sensor.createRotationMatrix([-0.27775216, 0.5351276, 9.788099], [210.87253, -78.6096, -111.44444]); const promise = sensor.createRotationMatrix([-0.27775216, 0.5351276, 9.788099], [210.87253, -78.6096, -111.44444]);
promise.then((data) => { promise.then((data) => {
console.info('createRotationMatrix_promise successed'); console.info('createRotationMatrix_promise successed');
for (var i=0; i < data.length; i++) { for (var i=0; i < data.rotation.length; i++) {
console.info("data[" + i + "]: " + data[i]); console.info("data[" + i + "]: " + data[i]);
} }
}).catch((err) => { }).catch((err) => {
...@@ -2222,7 +2405,8 @@ Enumerates the sensor types. ...@@ -2222,7 +2405,8 @@ Enumerates the sensor types.
| SENSOR_TYPE_ID_HUMIDITY | 13 | Humidity sensor. | | SENSOR_TYPE_ID_HUMIDITY | 13 | Humidity sensor. |
| SENSOR_TYPE_ID_ORIENTATION | 256 | Orientation sensor. | | SENSOR_TYPE_ID_ORIENTATION | 256 | Orientation sensor. |
| SENSOR_TYPE_ID_GRAVITY | 257 | Gravity sensor. | | SENSOR_TYPE_ID_GRAVITY | 257 | Gravity sensor. |
| SENSOR_TYPE_ID_LINEAR_ACCELERATION | 258 | Linear acceleration sensor. | | SENSOR_TYPE_ID_LINEAR_ACCELERATION<sup>deprecated</sup> | 258 | Linear acceleration sensor. |
| SENSOR_TYPE_ID_LINEAR_ACCELEROMETER | 258 | Linear acceleration sensor. |
| SENSOR_TYPE_ID_ROTATION_VECTOR | 259 | Rotation vector sensor. | | SENSOR_TYPE_ID_ROTATION_VECTOR | 259 | Rotation vector sensor. |
| SENSOR_TYPE_ID_AMBIENT_TEMPERATURE | 260 | Ambient temperature sensor. | | SENSOR_TYPE_ID_AMBIENT_TEMPERATURE | 260 | Ambient temperature sensor. |
| SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED | 261 | Uncalibrated magnetic field sensor. | | SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED | 261 | Uncalibrated magnetic field sensor. |
...@@ -2230,7 +2414,8 @@ Enumerates the sensor types. ...@@ -2230,7 +2414,8 @@ Enumerates the sensor types.
| SENSOR_TYPE_ID_SIGNIFICANT_MOTION | 264 | Significant motion sensor. | | SENSOR_TYPE_ID_SIGNIFICANT_MOTION | 264 | Significant motion sensor. |
| SENSOR_TYPE_ID_PEDOMETER_DETECTION | 265 | Pedometer detection sensor. | | SENSOR_TYPE_ID_PEDOMETER_DETECTION | 265 | Pedometer detection sensor. |
| SENSOR_TYPE_ID_PEDOMETER | 266 | Pedometer sensor. | | SENSOR_TYPE_ID_PEDOMETER | 266 | Pedometer sensor. |
| SENSOR_TYPE_ID_HEART_RATE | 278 | Heart rate sensor. | | SENSOR_TYPE_ID_HEART_RATE<sup>deprecated</sup> | 278 | Heart rate sensor. |
| SENSOR_TYPE_ID_HEART_BEAT_RATE | 278 | Heart rate sensor. |
| SENSOR_TYPE_ID_WEAR_DETECTION | 280 | Wear detection sensor. | | SENSOR_TYPE_ID_WEAR_DETECTION | 280 | Wear detection sensor. |
| SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED | 281 | Uncalibrated acceleration sensor.| | SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED | 281 | Uncalibrated acceleration sensor.|
...@@ -2420,11 +2605,11 @@ Describes the magnetic field sensor data. It extends from [Response](#response). ...@@ -2420,11 +2605,11 @@ Describes the magnetic field sensor data. It extends from [Response](#response).
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor
| Name | Type | Readable | Writable | Description | | Name| Type| Readable| Writable| Description |
| ---- | ------ | ---- | ---- | ------------------ | | ---- | -------- | ---- | ---- | ---------------------------- |
| x | number | Yes | Yes | Magnetic field strength on the x-axis, in μT. | | x | number | Yes | Yes | Magnetic field strength on the x-axis, in μT.|
| y | number | Yes | Yes | Magnetic field strength on the y-axis, in μT. | | y | number | Yes | Yes | Magnetic field strength on the y-axis, in μT.|
| z | number | Yes | Yes | Magnetic field strength on the z-axis, in μT.| | z | number | Yes | Yes | Magnetic field strength on the z-axis, in μT.|
## MagneticFieldUncalibratedResponse ## MagneticFieldUncalibratedResponse
......
# Sensor # Sensor
The **Sensor** module provides APIs for querying the sensor list, subscribing to or unsubscribing from sensor data, and executing control commands.
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE** The sensors are classified into the following categories based on their functions: motion, environment, orientation, light, body, and other categories (such as Hall effect sensors). Each category includes different sensor types. A sensor type may be a single hardware sensor or a composite of multiple hardware sensors.
> **NOTE**
>
> - The APIs of this module are no longer maintained since API version 8. You are advised to use [`@ohos.sensor`](js-apis-sensor.md) instead.
> - The initial APIs of this module are supported since API version 4. Newly added APIs will be marked with a superscript to indicate their earliest API version. > - The initial APIs of this module are supported since API version 4. Newly added APIs will be marked with a superscript to indicate their earliest API version.
> - The APIs of this module are no longer maintained since API version 8. You are advised to use [Sensor](js-apis-sensor.md) instead.
> - This module requires hardware support and can only be debugged on real devices. > - This module requires hardware support and can only be debugged on real devices.
...@@ -14,12 +19,11 @@ ...@@ -14,12 +19,11 @@
import sensor from '@system.sensor'; import sensor from '@system.sensor';
``` ```
## Error Codes ## Error Codes
| Error Code | Description | | Error Code | Description |
| ---------- | ---------------------------------------- | | ---- | -------------- |
| 900 | The current device does not support the corresponding sensor. | | 900 | The current device does not support the corresponding sensor.|
## sensor.subscribeAccelerometer ## sensor.subscribeAccelerometer
...@@ -33,19 +37,19 @@ Subscribes to data changes of the acceleration sensor. If this API is called mul ...@@ -33,19 +37,19 @@ Subscribes to data changes of the acceleration sensor. If this API is called mul
**Parameters** **Parameters**
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| -------- | -------- | --------- | ---------------------------------------- | | -------- | -------- | ---- | ---------------------------------------- |
| interval | string | Yes | Execution frequency of the callback for returning the acceleration sensor data.<br>The default value is **normal**. The options are as follows:<br>- **game**: called at an interval of 20 ms, which is applicable to gaming scenarios.<br>- **ui**: called at an interval of 60 ms, which is applicable to UI updating scenarios.<br>- **normal**: called at an interval of 200 ms, which is applicable to power-saving scenarios. | | interval | string | Yes | Execution frequency of the callback for returning the acceleration sensor data.<br>The default value is **normal**. The options are as follows:<br>- **game**: called at an interval of 20 ms, which is applicable to gaming scenarios.<br>- **ui**: called at an interval of 60 ms, which is applicable to UI updating scenarios.<br>- **normal**: called at an interval of 200 ms, which is applicable to power-saving scenarios.|
| success | Function | Yes | Called when the acceleration sensor data changes. | | success | Function | Yes | Called when the acceleration sensor data changes. |
| fail | Function | No | Callback upon failure. | | fail | Function | No | Callback upon failure. |
Return values of the success callback Return values of the success callback
| Name | Type | Description | | Name | Type | Description |
| ---- | ------ | --------------------------- | | ---- | ------ | ------- |
| x | number | Acceleration on the x-axis. | | x | number | Acceleration on the x-axis.|
| y | number | Acceleration on the y-axis. | | y | number | Acceleration on the y-axis.|
| z | number | Acceleration on the z-axis. | | z | number | Acceleration on the z-axis.|
**Example** **Example**
...@@ -63,7 +67,8 @@ sensor.subscribeAccelerometer({ ...@@ -63,7 +67,8 @@ sensor.subscribeAccelerometer({
}); });
``` ```
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE** > **NOTE**
>
> To reduce performance overhead, you are advised to unsubscribe from the sensor data in the **onDestory** callback. > To reduce performance overhead, you are advised to unsubscribe from the sensor data in the **onDestory** callback.
## sensor.unsubscribeAccelerometer ## sensor.unsubscribeAccelerometer
...@@ -92,16 +97,16 @@ Subscribes to data changes of the compass sensor. If this API is called multiple ...@@ -92,16 +97,16 @@ Subscribes to data changes of the compass sensor. If this API is called multiple
**Parameters** **Parameters**
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| ------- | -------- | --------- | ---------------------------------------- | | ------- | -------- | ---- | --------------- |
| success | Function | Yes | Called when the compass sensor data changes. | | success | Function | Yes | Called when the compass sensor data changes.|
| fail | Function | No | Callback upon failure. | | fail | Function | No | Callback upon failure. |
Return values of the success callback Return values of the success callback
| Name | Type | Description | | Name | Type | Description |
| --------- | ------ | ------------------------------------ | | --------- | ------ | ---------- |
| direction | number | Direction of the device, in degrees. | | direction | number | Direction of the device, in degrees.|
**Example** **Example**
...@@ -116,7 +121,8 @@ sensor.subscribeCompass({ ...@@ -116,7 +121,8 @@ sensor.subscribeCompass({
}); });
``` ```
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE** > **NOTE**
>
> To reduce performance overhead, you are advised to unsubscribe from the sensor data in the **onDestory** callback. > To reduce performance overhead, you are advised to unsubscribe from the sensor data in the **onDestory** callback.
## sensor.unsubscribeCompass ## sensor.unsubscribeCompass
...@@ -143,16 +149,16 @@ Subscribes to data changes of the proximity sensor. If this API is called multip ...@@ -143,16 +149,16 @@ Subscribes to data changes of the proximity sensor. If this API is called multip
**Parameters** **Parameters**
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| ------- | -------- | --------- | ---------------------------------------- | | ------- | -------- | ---- | ----------------- |
| success | Function | Yes | Called when the proximity sensor data changes. | | success | Function | Yes | Called when the proximity sensor data changes.|
| fail | Function | No | Callback upon failure. | | fail | Function | No | Callback upon failure. |
Return values of the success callback Return values of the success callback
| Name | Type | Description | | Name | Type | Description |
| -------- | ------ | ---------------------------------------- | | -------- | ------ | --------------------- |
| distance | number | Distance between a visible object and the device screen. | | distance | number | Distance between a visible object and the device screen.|
**Example** **Example**
...@@ -167,7 +173,8 @@ sensor.subscribeProximity({ ...@@ -167,7 +173,8 @@ sensor.subscribeProximity({
}); });
``` ```
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE** > **NOTE**
>
> To reduce performance overhead, you are advised to unsubscribe from the sensor data in the **onDestory** callback. > To reduce performance overhead, you are advised to unsubscribe from the sensor data in the **onDestory** callback.
## sensor.unsubscribeProximity ## sensor.unsubscribeProximity
...@@ -194,16 +201,16 @@ Subscribes to data changes of the ambient light sensor. If this API is called mu ...@@ -194,16 +201,16 @@ Subscribes to data changes of the ambient light sensor. If this API is called mu
**Parameters** **Parameters**
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| ------- | -------- | --------- | ---------------------------------------- | | ------- | -------- | ---- | --------------- |
| success | Function | Yes | Called when the ambient light sensor data changes | | success | Function | Yes | Called when the ambient light sensor data changes|
| fail | Function | No | Callback upon failure. | | fail | Function | No | Callback upon failure. |
Return values of the success callback Return values of the success callback
| Name | Type | Description | | Name | Type | Description |
| --------- | ------ | ------------------------ | | --------- | ------ | ------------ |
| intensity | number | Light intensity, in lux. | | intensity | number | Light intensity, in lux.|
**Example** **Example**
...@@ -218,7 +225,8 @@ sensor.subscribeLight({ ...@@ -218,7 +225,8 @@ sensor.subscribeLight({
}); });
``` ```
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE** > **NOTE**
>
> To reduce performance overhead, you are advised to unsubscribe from the sensor data in the **onDestory** callback. > To reduce performance overhead, you are advised to unsubscribe from the sensor data in the **onDestory** callback.
## sensor.unsubscribeLight ## sensor.unsubscribeLight
...@@ -247,16 +255,16 @@ Subscribes to data changes of the step counter sensor. If this API is called mul ...@@ -247,16 +255,16 @@ Subscribes to data changes of the step counter sensor. If this API is called mul
**Parameters** **Parameters**
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| ------- | -------- | --------- | ---------------------------------------- | | ------- | -------- | ---- | ---------------- |
| success | Function | Yes | Called when the step counter sensor data changes. | | success | Function | Yes | Called when the step counter sensor data changes.|
| fail | Function | No | Callback upon failure. | | fail | Function | No | Callback upon failure. |
Return values of the success callback Return values of the success callback
| Name | Type | Description | | Name | Type | Description |
| ----- | ------ | ---------------------------------------- | | ----- | ------ | --------------------- |
| steps | number | Number of counted steps after the sensor is restarted.<br> | | steps | number | Number of counted steps after the sensor is restarted.<br>|
**Example** **Example**
...@@ -271,7 +279,8 @@ sensor.subscribeStepCounter({ ...@@ -271,7 +279,8 @@ sensor.subscribeStepCounter({
}); });
``` ```
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE** > **NOTE**
>
> To reduce performance overhead, you are advised to unsubscribe from the sensor data in the **onDestory** callback. > To reduce performance overhead, you are advised to unsubscribe from the sensor data in the **onDestory** callback.
## sensor.unsubscribeStepCounter ## sensor.unsubscribeStepCounter
...@@ -301,16 +310,16 @@ Subscribes to data changes of the barometer sensor. If this API is called multip ...@@ -301,16 +310,16 @@ Subscribes to data changes of the barometer sensor. If this API is called multip
**Parameters** **Parameters**
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| ------- | -------- | --------- | ---------------------------------------- | | ------- | -------- | ---- | ---------------- |
| success | Function | Yes | Called when the barometer sensor data changes. | | success | Function | Yes | Called when the barometer sensor data changes.|
| fail | Function | No | Callback upon failure. | | fail | Function | No | Callback upon failure. |
Return values of the success callback Return values of the success callback
| Name | Type | Description | | Name | Type | Description |
| -------- | ------ | -------------------- | | -------- | ------ | ----------- |
| pressure | number | Pressure, in pascal. | | pressure | number | Pressure, in pascal.|
**Example** **Example**
...@@ -325,7 +334,8 @@ sensor.subscribeBarometer({ ...@@ -325,7 +334,8 @@ sensor.subscribeBarometer({
}); });
``` ```
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE** > **NOTE**
>
> To reduce performance overhead, you are advised to unsubscribe from the sensor data in the **onDestory** callback. > To reduce performance overhead, you are advised to unsubscribe from the sensor data in the **onDestory** callback.
...@@ -356,16 +366,16 @@ Subscribes to data changes of the heart rate sensor. If this API is called multi ...@@ -356,16 +366,16 @@ Subscribes to data changes of the heart rate sensor. If this API is called multi
**Parameters** **Parameters**
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| ------- | -------- | --------- | ---------------------------------------- | | ------- | -------- | ---- | ------------------------- |
| success | Function | Yes | Called when the heart rate sensor data changes. This callback is invoked every five seconds. | | success | Function | Yes | Called when the heart rate sensor data changes. This callback is invoked every five seconds.|
| fail | Function | No | Callback upon failure. | | fail | Function | No | Callback upon failure. |
Return values of the success callback Return values of the success callback
| Name | Type | Description | | Name | Type | Description |
| --------- | ------ | ----------- | | --------- | ------ | ---- |
| heartRate | number | Heart rate. | | heartRate | number | Heart rate.|
**Example** **Example**
...@@ -380,7 +390,8 @@ sensor.subscribeHeartRate({ ...@@ -380,7 +390,8 @@ sensor.subscribeHeartRate({
}); });
``` ```
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE** > **NOTE**
>
> To reduce performance overhead, you are advised to unsubscribe from the sensor data in the **onDestory** callback. > To reduce performance overhead, you are advised to unsubscribe from the sensor data in the **onDestory** callback.
...@@ -410,16 +421,16 @@ Subscribes to changes of the wearing state of a wearable device. If this API is ...@@ -410,16 +421,16 @@ Subscribes to changes of the wearing state of a wearable device. If this API is
**Parameters** **Parameters**
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| ------- | -------- | --------- | -------------------------------------- | | ------- | -------- | ---- | ------------- |
| success | Function | Yes | Called when the wearing state changes. | | success | Function | Yes | Called when the wearing state changes.|
| fail | Function | No | Callback upon failure. | | fail | Function | No | Callback upon failure. |
Return values of the success callback Return values of the success callback
| Name | Type | Description | | Name | Type | Description |
| ----- | ------- | ------------------------------------ | | ----- | ------- | ------ |
| value | boolean | Whether the wearable device is worn. | | value | boolean | Whether the wearable device is worn.|
**Example** **Example**
...@@ -434,7 +445,8 @@ sensor.subscribeOnBodyState({ ...@@ -434,7 +445,8 @@ sensor.subscribeOnBodyState({
}); });
``` ```
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE** > **NOTE**
>
> To reduce performance overhead, you are advised to unsubscribe from the sensor data in the **onDestory** callback. > To reduce performance overhead, you are advised to unsubscribe from the sensor data in the **onDestory** callback.
## sensor.unsubscribeOnBodyState ## sensor.unsubscribeOnBodyState
...@@ -461,17 +473,17 @@ Obtains the wearing state of a wearable device. ...@@ -461,17 +473,17 @@ Obtains the wearing state of a wearable device.
**Parameters** **Parameters**
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| -------- | -------- | --------- | -------------------------------------- | | -------- | -------- | ---- | ------------ |
| success | Function | No | Callback upon success. | | success | Function | No | Callback upon success.|
| fail | Function | No | Callback upon failure. | | fail | Function | No | Callback upon failure.|
| complete | Function | No | Called when the execution is complete. | | complete | Function | No | Called when the execution is complete.|
Return values of the success callback Return values of the success callback
| Name | Type | Description | | Name | Type | Description |
| ----- | ------- | ------------------------------------ | | ----- | ------- | ------ |
| value | boolean | Whether the wearable device is worn. | | value | boolean | Whether the wearable device is worn.|
**Example** **Example**
...@@ -498,18 +510,18 @@ If this API is called multiple times for the same application, the last call tak ...@@ -498,18 +510,18 @@ If this API is called multiple times for the same application, the last call tak
**Parameters** **Parameters**
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| -------- | -------- | --------- | ---------------------------------------- | | -------- | -------- | ---- | ---------------------------------------- |
| interval | string | Yes | Interval at which the callback is invoked to return the device orientation sensor data.<br>The default value is **normal**. The options are as follows:<br>- **game**: called at an interval of 20 ms, which is applicable to gaming scenarios.<br>- **ui**: called at an interval of 60 ms, which is applicable to UI updating scenarios.<br>- **normal**: called at an interval of 200 ms, which is applicable to power-saving scenarios. | | interval | string | Yes | Interval at which the callback is invoked to return the device orientation sensor data.<br>The default value is **normal**. The options are as follows:<br>- **game**: called at an interval of 20 ms, which is applicable to gaming scenarios.<br>- **ui**: called at an interval of 60 ms, which is applicable to UI updating scenarios.<br>- **normal**: called at an interval of 200 ms, which is applicable to power-saving scenarios.|
| success | Function | Yes | Called when the device orientation sensor data changes. | | success | Function | Yes | Called when the device orientation sensor data changes. |
| fail | Function | No | Callback upon failure. | | fail | Function | No | Callback upon failure. |
Return values of the success callback Return values of the success callback
| Name | Type | Description | | Name | Type | Description |
| ----- | ------ | ---------------------------------------- | | ----- | ------ | ---------------------------------------- |
| alpha | number | Rotation angle around the Z axis when the X/Y axis of the mobile device coincides with the X/Y axis of the earth. | | alpha | number | Rotation angle around the Z axis when the X/Y axis of the device coincides with the X/Y axis of the earth.|
| beta | number | Rotation angle around the X axis when the Y/Z axis of the mobile device coincides with the Y/Z axis of the earth. | | beta | number | Rotation angle around the X axis when the Y/Z axis of the device coincides with the Y/Z axis of the earth.|
| gamma | number | Rotation angle around the Y axis when the X/Z axis of the mobile device coincides with the X/Z axis of the earth. | | gamma | number | Rotation angle around the Y axis when the X/Z axis of the device coincides with the X/Z axis of the earth.|
**Example** **Example**
...@@ -527,7 +539,8 @@ sensor.subscribeDeviceOrientation({ ...@@ -527,7 +539,8 @@ sensor.subscribeDeviceOrientation({
}); });
``` ```
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE** > **NOTE**
>
> To reduce performance overhead, you are advised to unsubscribe from the sensor data in the **onDestory** callback. > To reduce performance overhead, you are advised to unsubscribe from the sensor data in the **onDestory** callback.
## sensor.unsubscribeDeviceOrientation<sup>6+</sup> ## sensor.unsubscribeDeviceOrientation<sup>6+</sup>
...@@ -558,19 +571,19 @@ If this API is called multiple times for the same application, the last call tak ...@@ -558,19 +571,19 @@ If this API is called multiple times for the same application, the last call tak
**Parameters** **Parameters**
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
| -------- | -------- | --------- | ---------------------------------------- | | -------- | -------- | ---- | ---------------------------------------- |
| interval | string | Yes | Interval at which the callback is invoked to return the gyroscope sensor data.<br>The default value is **normal**. The options are as follows:<br>- **game**: called at an interval of 20 ms, which is applicable to gaming scenarios.<br>- **ui**: called at an interval of 60 ms, which is applicable to UI updating scenarios.<br>- **normal**: called at an interval of 200 ms, which is applicable to power-saving scenarios. | | interval | string | Yes | Interval at which the callback is invoked to return the gyroscope sensor data.<br>The default value is **normal**. The options are as follows:<br>- **game**: called at an interval of 20 ms, which is applicable to gaming scenarios.<br>- **ui**: called at an interval of 60 ms, which is applicable to UI updating scenarios.<br>- **normal**: called at an interval of 200 ms, which is applicable to power-saving scenarios.|
| success | Function | Yes | Called when the gyroscope sensor data changes. | | success | Function | Yes | Called when the gyroscope sensor data changes. |
| fail | Function | No | Callback upon failure. | | fail | Function | No | Callback upon failure. |
Return values of the success callback Return values of the success callback
| Name | Type | Description | | Name | Type | Description |
| ---- | ------ | ---------------------------------------- | | ---- | ------ | --------- |
| x | number | Rotation angular velocity of the X axis. | | x | number | Rotation angular velocity of the X axis.|
| y | number | Rotation angular velocity of the Y axis. | | y | number | Rotation angular velocity of the Y axis.|
| z | number | Rotation angular velocity of the Z axis. | | z | number | Rotation angular velocity of the Z axis.|
**Example** **Example**
...@@ -588,7 +601,8 @@ sensor.subscribeGyroscope({ ...@@ -588,7 +601,8 @@ sensor.subscribeGyroscope({
}); });
``` ```
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE** > **NOTE**
>
> To reduce performance overhead, you are advised to unsubscribe from the sensor data in the **onDestory** callback. > To reduce performance overhead, you are advised to unsubscribe from the sensor data in the **onDestory** callback.
## sensor.unsubscribeGyroscope<sup>6+</sup> ## sensor.unsubscribeGyroscope<sup>6+</sup>
......
# Vibrator # Vibrator
The **Vibrate** module provides APIs for controlling LED lights and vibrators. You can use the APIs to query the LED light list, turn on and off the LED light, query the vibrator list, query the vibrator effect, and trigger and turn off the vibrator.
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/> Misc devices refer to LED lights and vibrators on devices. LED lights are mainly used for indication (for example, indicating the charging state) and blinking (such as tri-colored lights). Vibrators are mainly used in scenarios such as the alarm clock, power-on/off, and incoming call vibration.
> **NOTE**
> - The initial APIs of this module are supported since API version 4. Newly added APIs will be marked with a superscript to indicate their earliest API version. > - The initial APIs of this module are supported since API version 4. Newly added APIs will be marked with a superscript to indicate their earliest API version.
> - The APIs of this module are no longer maintained since API version 8. You are advised to use [`@ohos.vibrator`](js-apis-vibrator.md) instead. > - The APIs of this module are no longer maintained since API version 8. You are advised to use [`@ohos.vibrator`](js-apis-vibrator.md) instead.
> - This module requires hardware support and can only be debugged on real devices. > - This module requires hardware support and can only be debugged on real devices.
...@@ -14,7 +18,6 @@ ...@@ -14,7 +18,6 @@
import vibrator from '@system.vibrator'; import vibrator from '@system.vibrator';
``` ```
## vibrator.vibrate ## vibrator.vibrate
vibrate(Object): void vibrate(Object): void
......
# Vibrator
The **Vibrator** module provides APIs for triggering or stopping vibration.
# Vibrator Misc devices, including vibrators and LED lights, are used to send signals externally. You can call the APIs of this module to control the vibration of vibrators and turning-on/off of LED lights.
> **NOTE** > **NOTE**
> >
...@@ -13,7 +15,6 @@ ...@@ -13,7 +15,6 @@
import vibrator from '@ohos.vibrator'; import vibrator from '@ohos.vibrator';
``` ```
## vibrator.vibrate ## vibrator.vibrate
vibrate(duration: number): Promise&lt;void&gt; vibrate(duration: number): Promise&lt;void&gt;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册