Go to file
openharmony_ci 72202d63a1
!126 修改EventRunner调用,删除多余代码
Merge pull request !126 from SUE/master
2022-04-07 09:39:52 +00:00
figures Signed-off-by:hellohyh001<huiyuehong@huawei.com> 2021-09-13 13:02:55 +00:00
frameworks/native/sensor modify kora warning 2022-04-07 15:27:10 +08:00
interfaces modify kora warning 2022-04-07 16:35:33 +08:00
sa_profile Signed-off-by:hellohyh001<huiyuehong@huawei.com> 2021-12-27 14:58:39 +08:00
services/sensor !125 修改sensor告警 2022-04-06 08:53:25 +00:00
utils !125 修改sensor告警 2022-04-06 08:53:25 +00:00
bundle.json Signed-off-by:hellohyh001<huiyuehong@huawei.com> 2022-02-22 21:18:57 +08:00
LICENSE Signed-off-by:vocoal<gaofeng99@huawei.com> 2021-09-10 02:00:14 +00:00
OAT.xml Signed-off-by:hellohyh001<huiyuehong@huawei.com> 2021-09-13 13:02:55 +00:00
README_zh.md Signed-off-by:hellohyh001<huiyuehong@huawei.com> 2021-10-29 08:26:22 +00:00
README.md Signed-off-by:hellohyh001<huiyuehong@huawei.com> 2021-10-29 08:26:22 +00:00

Sensor

Introduction

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 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, etc.
  • Orientation: rotation vector sensors, orientation sensors, etc.
  • Environment: magnetic field sensors, barometric pressure sensors, humidity sensors, etc.
  • Light: ambient light sensors, proximity sensors, color temperature sensors, etc.
  • Body: heart rate sensors, heartbeat sensors, etc.
  • Other: Hall effect sensors, grip detection sensors, etc.

The following figure shows the sensor architecture.

Figure 1 Sensor architecture

Directory Structure

The sample code for importing the sensor module is as follows:

/base/sensors/sensor
├── frameworks                 # Framework code
│   └── native                 # Sensor client code
├── interfaces                 # External APIs
│   ├── native                 # Native Implementation for sensors
│   └── plugin                 # JS APIs
├── sa_profile                 # Configuration file of system ability names and dynamic libraries
├── services                   # Code of services
│   └── sensor                 # Sensor service for reporting data about sensors, such as the acceleration and gyroscope sensors
└── utils                      # Common code, including permissions and communication capabilities 

Constraints

  • To use sensor functions, ensure that the device where your application runs has the required sensor components.

  • To obtain data of some sensors, you need to request the required permissions.

    Table 1 Permissions required by sensors

    Sensor

    Permission Name

    Sensitivity

    Permission Description

    Acceleration sensor, uncalibrated acceleration sensor, and linear acceleration sensor

    ohos.permission.ACCELEROMETER

    system_grant

    Allows an application to subscribe to data of these acceleration-related sensors.

    Gyroscope sensor and uncalibrated gyroscope sensor

    ohos.permission.GYROSCOPE

    system_grant

    Allows an application to subscribe to data of these gyroscope-related sensors.

    Pedometer sensor

    ohos.permission.ACTIVITY_MOTION

    user_grant

    Allows an application to subscribe to the motion status.

    Heart rate sensor

    ohos.permission.READ_HEALTH_DATA

    user_grant

    Allows an application to read health data.

Usage

This section uses the sensor JS APIs as an example to describe their functionalities and usage.

Available APIs

The 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.

Table 2 Sensor JS APIs

API

Description

on(type: SensorType, callback: AsyncCallback<Response>, options?: Options)

Subscribes to a type of sensor that listens for changes of sensor data. SensorType indicates the type of the sensor that can be subscribed to. callback specifies whether the subscription is successful. options indicates the interval for reporting sensor data.

once(type: SensorType, callback: AsyncCallback<Response>)

Subscribes to a type of sensor that listens for the sensor data change once. SensorType indicates the type of the sensor that can be subscribed to. callback specifies whether the subscription is successful.

off(type: SensorType, callback: AsyncCallback<void>)

Unsubscribes from a type of sensor that listens for data changes. SensorType indicates the type of the sensor that can be unsubscribed from. callback specifies whether the unsubscription is successful.

How to Use

  1. Import the sensor package.
  2. Subscribe to and listen for data changes of an acceleration sensor.
  3. Unsubscribe from data changes of the acceleration sensor.
  4. Subscribe to and listen for a data change of a acceleration sensor.

Example:

// Step 1 Import the sensor package.
import sensor from '@ohos.sensor';
export default {
    onCreate() {
        // Step 2 Subscribe to and listen for data changes of a type of sensor.
        sensor.on(sensor.SENSOR_TYPE_ID_ACCELEROMETER, (error, data) => {
            if (error) {
                console.error("Failed to subscribe to acceleration data. Error code: " + error.code + "; message: " + error.message);
                return;
            }
            console.info("Acceleration data obtained. x: " + data.x + "; y: " + data.y + "; z: " + data.z);
        }, {'interval':200000000});
        // Step 3 Unsubscribe from data changes of the sensor 10 seconds later.
        setTimeout(function(){
            sensor.off(sensor.SENSOR_TYPE_ID_ACCELEROMETER, function(error) {
                if (error) {
                    console.error("Failed to unsubscribe from acceleration data. Error code: " + error.code + "; message: " + error.message);
                    return;
                }
                console.info("Succeeded in unsubscribe from sensor data");
            });
        } ,10000);
        // Step 4 Subscribe to and listen for a data change of a type of sensor.
        sensor.once(sensor.SENSOR_TYPE_ID_ACCELEROMETER, (error, data) => {
            if (error) {
                console.error("Failed to subscribe to gravity data. Error code: " + error.code + "; message: " + error.message);
                return;
            }
            console.info("Acceleration data obtained. x: " + data.x + "; y: " + data.y + "; z: " + data.z);
       });
    }
    onDestroy() {
        console.info('AceApplication onDestroy');
    }
}

Repositories Involved

Pan-sensor subsystem

sensors_sensor

sensors_miscdevice