Go to file
openharmony_ci 4007f9a0d5
!675 Alarm modification
Merge pull request !675 from 白露/master
2024-11-22 08:59:57 +00:00
figures Signed-off-by:hellohyh001<huiyuehong@huawei.com> 2021-09-13 13:02:55 +00:00
frameworks Alarm modification 2024-11-22 15:21:07 +08:00
interfaces !670 【bug】添加维测日志 2024-11-13 07:39:46 +00:00
rust/utils/socket_ipc_rust_ffi 目录整改代码规范 2023-10-18 02:58:34 +00:00
sa_profile xml改成json 2023-05-24 06:10:25 +00:00
services Alarm modification 2024-11-22 15:21:07 +08:00
test Modify the issue about report data send failed 2024-11-16 14:48:16 +08:00
utils Alarm modification 2024-11-22 15:21:07 +08:00
vibration_convert Alarm modification 2024-11-22 15:21:07 +08:00
bundle.json [cj]: support cj api 2024-10-15 00:46:21 +08:00
CODEOWNERS update 2023-06-12 11:53:39 +00:00
hisysevent.yaml 修改yaml 2022-12-28 15:37:36 +08:00
LICENSE Signed-off-by:vocoal<gaofeng99@huawei.com> 2021-09-10 02:00:14 +00:00
OAT.xml sensor Adds a binary file type policy filter 2024-05-28 21:46:42 +08:00
README_zh.md 修改sensor资料 2022-04-19 09:35:41 +08:00
README.md Fix typo in sensor repo 2022-11-04 11:21:26 +08:00
sensor.gni sensor解耦hitrace 2024-08-28 16:44:04 +08: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: Callback<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: Callback<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: Callback<Response>)

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 an 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.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, (data) => {
            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.
        sensor.off(sensor.SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION);
        // Step 4 Subscribe to and listen for a data change of a type of sensor.
        sensor.once(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, (data) => {
            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