@ohos.sensor (Sensor) (System API)
The Sensor module provides APIs for obtaining the sensor list and subscribing to sensor data. It also provides some common sensor algorithms.
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 APIs provided by this module are system APIs.
Modules to Import
import sensor from '@ohos.sensor';
sensor.on
COLOR10+
on(type: SensorId.COLOR, callback: Callback<ColorResponse>, options?: Options): void
Subscribes to data of the color sensor.
System capability: SystemCapability.Sensors.Sensor
System API: This is a system API.
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
type | SensorId.COLOR | Yes | Sensor type. The value is fixed at SensorId.COLOR. |
callback | Callback<ColorResponse> | Yes | Callback used to report the sensor data, which is a ColorResponse object. |
options | Options | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns. |
Error codes
For details about the following error codes, see Sensor Error Codes.
ID | Error Message |
---|---|
14500101 | Service exception. |
Example
import sensor from "@ohos.sensor";
import BusinessError from "@ohos.base";
try{
sensor.on(sensor.SensorId.COLOR, (data: sensor.ColorResponse) => {
console.log('Succeeded in getting the intensity of light: ' + data.lightIntensity);
console.log('Succeeded in getting the color temperature: ' + data.colorTemperature);
}, { interval: 100000000 });
setTimeout(() => {
sensor.off(sensor.SensorId.COLOR);
}, 500);
} catch (error) {
let e: BusinessError.BusinessError = error as BusinessError.BusinessError;
console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`);
}
SAR10+
on(type: SensorId.SAR, callback: Callback<SarResponse>, options?: Options): void
Subscribes to data of the Sodium Adsorption Ratio (SAR) sensor.
System capability: SystemCapability.Sensors.Sensor
System API: This is a system API.
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
type | SensorId.SAR | Yes | Sensor type. The value is fixed at SensorId.SAR. |
callback | Callback<SarResponse> | Yes | Callback used to report the sensor data, which is a SarResponse object. |
options | Options | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns. |
Error codes
For details about the following error codes, see Sensor Error Codes.
ID | Error Message |
---|---|
14500101 | Service exception. |
Example
import sensor from "@ohos.sensor";
import BusinessError from "@ohos.base";
try {
sensor.on(sensor.SensorId.SAR, (data: sensor.SarResponse) => {
console.info('Succeeded in getting specific absorption rate : ' + data.absorptionRatio);
}, { interval: 100000000 });
setTimeout(() => {
sensor.off(sensor.SensorId.SAR);
}, 500);
} catch (error) {
let e: BusinessError.BusinessError = error as BusinessError.BusinessError;
console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`);
}
sensor.off
COLOR10+
off(type: SensorId.COLOR, callback?: Callback<ColorResponse>): void
Unsubscribes from data of the color sensor.
System capability: SystemCapability.Sensors.Sensor
System API: This is a system API.
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
type | SensorId.COLOR | Yes | Sensor type. The value is fixed at SensorId.COLOR. |
callback | Callback<ColorResponse> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from. |
Example
import sensor from "@ohos.sensor";
import BusinessError from "@ohos.base";
function callback1(data: object) {
console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data));
}
function callback2(data: object) {
console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data));
}
try {
sensor.on(sensor.SensorId.COLOR, callback1);
sensor.on(sensor.SensorId.COLOR, callback2);
// Unsubscribe from callback1.
sensor.off(sensor.SensorId.COLOR, callback1);
// Unsubscribe from all callbacks of the SensorId.COLOR type.
sensor.off(sensor.SensorId.COLOR);
} catch (error) {
let e: BusinessError.BusinessError = error as BusinessError.BusinessError;
console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`);
}
SAR10+
off(type: SensorId.SAR, callback?: Callback<SarResponse>): void
Unsubscribes from data of the SAR sensor.
System capability: SystemCapability.Sensors.Sensor
System API: This is a system API.
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
type | SensorId.SAR | Yes | Sensor type. The value is fixed at SensorId.SAR. |
callback | Callback<SarResponse> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from. |
Example
import sensor from "@ohos.sensor";
import BusinessError from "@ohos.base";
function callback1(data: object) {
console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data));
}
function callback2(data: object) {
console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data));
}
try {
sensor.on(sensor.SensorId.SAR, callback1);
sensor.on(sensor.SensorId.SAR, callback2);
// Unsubscribe from callback1.
sensor.off(sensor.SensorId.SAR, callback1);
// Unsubscribe from all callbacks of the SensorId.SAR type.
sensor.off(sensor.SensorId.SAR);
} catch (error) {
let e: BusinessError.BusinessError = error as BusinessError.BusinessError;
console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`);
}
SensorId9+
Enumerates the sensor types.
System capability: SystemCapability.Sensors.Sensor
Name | Value | Description |
---|---|---|
COLOR10+ | 14 | Color sensor. System API: This is a system API. |
SAR10+ | 15 | Sodium Adsorption Ratio (SAR) sensor. System API: This is a system API. |
ColorResponse10+
Describes the color sensor data.
System capability: SystemCapability.Sensors.Sensor
System API: This is a system API.
Name | Type | Readable | Writable | Description |
---|---|---|---|---|
lightIntensity | number | Yes | Yes | Intensity of light, in lux. |
colorTemperature | number | Yes | Yes | Color temperature, in Kelvin. |
SarResponse10+
Describes the SAR sensor data.
System capability: SystemCapability.Sensors.Sensor
System API: This is a system API.
Name | Type | Readable | Writable | Description |
---|---|---|---|---|
absorptionRatio | number | Yes | Yes | Absorption ratio, in W/kg. |