openharmony 鸿蒙 js-apis-sensor-sys

2025-06-12 浏览 (1)

@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 '@kit.SensorServiceKit';

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

NameTypeMandatoryDescription
typeSensorId.COLORYesSensor type. The value is fixed at SensorId.COLOR.
callbackCallback<ColorResponse>YesCallback used to report the sensor data, which is a ColorResponse object.
optionsOptionsNoList 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 error codes, see Sensor Error Codes and Universal Error Codes.

IDError Message
202Permission check failed. A non-system application uses the system API.
401Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed.
14500101Service exception.

Example

import { sensor } from '@kit.SensorServiceKit';
import { BusinessError } from '@kit.BasicServicesKit';

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 = error as 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

NameTypeMandatoryDescription
typeSensorId.SARYesSensor type. The value is fixed at SensorId.SAR.
callbackCallback<SarResponse>YesCallback used to report the sensor data, which is a SarResponse object.
optionsOptionsNoList 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 error codes, see Sensor Error Codes and Universal Error Codes.

IDError Message
202Permission check failed. A non-system application uses the system API.
401Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed.
14500101Service exception.

Example

import { sensor } from '@kit.SensorServiceKit';
import { BusinessError } from '@kit.BasicServicesKit';

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 = error as 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

NameTypeMandatoryDescription
typeSensorId.COLORYesSensor type. The value is fixed at SensorId.COLOR.
callbackCallback<ColorResponse>NoCallback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
202Permission check failed. A non-system application uses the system API.
401Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed.

Example

import { sensor } from '@kit.SensorServiceKit';
import { BusinessError } from '@kit.BasicServicesKit';

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 = error as 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

NameTypeMandatoryDescription
typeSensorId.SARYesSensor type. The value is fixed at SensorId.SAR.
callbackCallback<SarResponse>NoCallback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
202Permission check failed. A non-system application uses the system API.
401Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed.

Example

import { sensor } from '@kit.SensorServiceKit';
import { BusinessError } from '@kit.BasicServicesKit';

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 = error as BusinessError;
  console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`);
}

SensorId9+

Enumerates the sensor types.

System capability: SystemCapability.Sensors.Sensor

NameValueDescription
COLOR10+14Color sensor.
System API: This is a system API.
SAR10+15Sodium Adsorption Ratio (SAR) sensor.
System API: This is a system API.

ColorResponse10+

Describes the color sensor data. It extends from Response.

System capability: SystemCapability.Sensors.Sensor

System API: This is a system API.

NameTypeReadableWritableDescription
lightIntensitynumberYesYesIntensity of light, in lux.
colorTemperaturenumberYesYesColor temperature, in Kelvin.

SarResponse10+

Describes the SAR sensor data. It extends from Response.

System capability: SystemCapability.Sensors.Sensor

System API: This is a system API.

NameTypeReadableWritableDescription
absorptionRationumberYesYesAbsorption ratio, in W/kg.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Sensor Service Kit

harmony 鸿蒙Sensor

harmony 鸿蒙Vibrator

harmony 鸿蒙Vibrator_Attribute

harmony 鸿蒙Vibrator_FileDescription

harmony 鸿蒙Sensor Error Codes

harmony 鸿蒙Vibrator Error Codes

harmony 鸿蒙js-apis-sensor

harmony 鸿蒙@system.sensor (Sensor)

harmony 鸿蒙@system.vibrator (Vibrator)

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/vuWxzm