50edf55950
Merge pull request !650 from 陈飞飞/OpenHarmony-5.0-Release |
||
---|---|---|
figures | ||
frameworks | ||
interfaces | ||
rust/utils/socket_ipc_rust_ffi | ||
sa_profile | ||
services | ||
test | ||
utils | ||
vibration_convert | ||
bundle.json | ||
CODEOWNERS | ||
hisysevent.yaml | ||
LICENSE | ||
OAT.xml | ||
README_zh.md | ||
README.md | ||
sensor.gni |
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.
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
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
How to Use
- Import the sensor package.
- Subscribe to and listen for data changes of an acceleration sensor.
- Unsubscribe from data changes of the acceleration sensor.
- 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