1e41f4b71Sopenharmony_ci# @ohos.sensor (Sensor) (System API) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe **Sensor** module provides APIs for obtaining the sensor list and subscribing to sensor data. It also provides some common sensor algorithms. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> 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. 8e41f4b71Sopenharmony_ci> 9e41f4b71Sopenharmony_ci> The APIs provided by this module are system APIs. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci## Modules to Import 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci```ts 15e41f4b71Sopenharmony_ciimport { sensor } from '@kit.SensorServiceKit'; 16e41f4b71Sopenharmony_ci``` 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci## sensor.on 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci### COLOR<sup>10+</sup> 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_cion(type: SensorId.COLOR, callback: Callback<ColorResponse>, options?: Options): void 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ciSubscribes to data of the color sensor. 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.Sensor 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci**System API**: This is a system API. 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci**Parameters** 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 33e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------- | ---- | ----------------------------------------------------------- | 34e41f4b71Sopenharmony_ci| type | [SensorId](#sensorid9).COLOR | Yes | Sensor type. The value is fixed at **SensorId.COLOR**. | 35e41f4b71Sopenharmony_ci| callback | Callback<[ColorResponse](#colorresponse10)> | Yes | Callback used to report the sensor data, which is a **ColorResponse** object. | 36e41f4b71Sopenharmony_ci| options | [Options](js-apis-sensor.md#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns.| 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci**Error codes** 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ciFor details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci| ID| Error Message | 43e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | 44e41f4b71Sopenharmony_ci| 202 | Permission check failed. A non-system application uses the system API. | 45e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 46e41f4b71Sopenharmony_ci| 14500101 | Service exception. | 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci**Example** 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci```ts 51e41f4b71Sopenharmony_ciimport { sensor } from '@kit.SensorServiceKit'; 52e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_citry{ 55e41f4b71Sopenharmony_ci sensor.on(sensor.SensorId.COLOR, (data: sensor.ColorResponse) => { 56e41f4b71Sopenharmony_ci console.log('Succeeded in getting the intensity of light: ' + data.lightIntensity); 57e41f4b71Sopenharmony_ci console.log('Succeeded in getting the color temperature: ' + data.colorTemperature); 58e41f4b71Sopenharmony_ci }, { interval: 100000000 }); 59e41f4b71Sopenharmony_ci setTimeout(() => { 60e41f4b71Sopenharmony_ci sensor.off(sensor.SensorId.COLOR); 61e41f4b71Sopenharmony_ci }, 500); 62e41f4b71Sopenharmony_ci} catch (error) { 63e41f4b71Sopenharmony_ci let e: BusinessError = error as BusinessError; 64e41f4b71Sopenharmony_ci console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 65e41f4b71Sopenharmony_ci} 66e41f4b71Sopenharmony_ci``` 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci### SAR<sup>10+</sup> 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_cion(type: SensorId.SAR, callback: Callback<SarResponse>, options?: Options): void 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ciSubscribes to data of the Sodium Adsorption Ratio (SAR) sensor. 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.Sensor 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ci**System API**: This is a system API. 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci**Parameters** 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 81e41f4b71Sopenharmony_ci| -------- | --------------------------------------------- | ---- | ----------------------------------------------------------- | 82e41f4b71Sopenharmony_ci| type | [SensorId](#sensorid9).SAR | Yes | Sensor type. The value is fixed at **SensorId.SAR**. | 83e41f4b71Sopenharmony_ci| callback | Callback<[SarResponse](#sarresponse10)> | Yes | Callback used to report the sensor data, which is a **SarResponse** object. | 84e41f4b71Sopenharmony_ci| options | [Options](js-apis-sensor.md#options) | No | List of optional parameters. This parameter is used to set the data reporting frequency. The default value is 200,000,000 ns.| 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci**Error codes** 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ciFor details about the error codes, see [Sensor Error Codes](errorcode-sensor.md) and [Universal Error Codes](../errorcode-universal.md). 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci| ID| Error Message | 91e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | 92e41f4b71Sopenharmony_ci| 202 | Permission check failed. A non-system application uses the system API. | 93e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 94e41f4b71Sopenharmony_ci| 14500101 | Service exception. | 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci**Example** 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci```ts 99e41f4b71Sopenharmony_ciimport { sensor } from '@kit.SensorServiceKit'; 100e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_citry { 103e41f4b71Sopenharmony_ci sensor.on(sensor.SensorId.SAR, (data: sensor.SarResponse) => { 104e41f4b71Sopenharmony_ci console.info('Succeeded in getting specific absorption rate : ' + data.absorptionRatio); 105e41f4b71Sopenharmony_ci }, { interval: 100000000 }); 106e41f4b71Sopenharmony_ci setTimeout(() => { 107e41f4b71Sopenharmony_ci sensor.off(sensor.SensorId.SAR); 108e41f4b71Sopenharmony_ci }, 500); 109e41f4b71Sopenharmony_ci} catch (error) { 110e41f4b71Sopenharmony_ci let e: BusinessError = error as BusinessError; 111e41f4b71Sopenharmony_ci console.error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`); 112e41f4b71Sopenharmony_ci} 113e41f4b71Sopenharmony_ci``` 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci## sensor.off 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci### COLOR<sup>10+</sup> 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_cioff(type: SensorId.COLOR, callback?: Callback<ColorResponse>): void 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ciUnsubscribes from data of the color sensor. 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.Sensor 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci**System API**: This is a system API. 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ci**Parameters** 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 130e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------- | ---- | ------------------------------------------------------------ | 131e41f4b71Sopenharmony_ci| type | [SensorId](#sensorid9).COLOR | Yes | Sensor type. The value is fixed at **SensorId.COLOR**. | 132e41f4b71Sopenharmony_ci| callback | Callback<[ColorResponse](#colorresponse10)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_ci**Error codes** 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 137e41f4b71Sopenharmony_ci 138e41f4b71Sopenharmony_ci| ID| Error Message | 139e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | 140e41f4b71Sopenharmony_ci| 202 | Permission check failed. A non-system application uses the system API. | 141e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ci**Example** 144e41f4b71Sopenharmony_ci 145e41f4b71Sopenharmony_ci```ts 146e41f4b71Sopenharmony_ciimport { sensor } from '@kit.SensorServiceKit'; 147e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_cifunction callback1(data: object) { 150e41f4b71Sopenharmony_ci console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 151e41f4b71Sopenharmony_ci} 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_cifunction callback2(data: object) { 154e41f4b71Sopenharmony_ci console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 155e41f4b71Sopenharmony_ci} 156e41f4b71Sopenharmony_ci 157e41f4b71Sopenharmony_citry { 158e41f4b71Sopenharmony_ci sensor.on(sensor.SensorId.COLOR, callback1); 159e41f4b71Sopenharmony_ci sensor.on(sensor.SensorId.COLOR, callback2); 160e41f4b71Sopenharmony_ci // Unsubscribe from callback1. 161e41f4b71Sopenharmony_ci sensor.off(sensor.SensorId.COLOR, callback1); 162e41f4b71Sopenharmony_ci // Unsubscribe from all callbacks of the SensorId.COLOR type. 163e41f4b71Sopenharmony_ci sensor.off(sensor.SensorId.COLOR); 164e41f4b71Sopenharmony_ci} catch (error) { 165e41f4b71Sopenharmony_ci let e: BusinessError = error as BusinessError; 166e41f4b71Sopenharmony_ci console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 167e41f4b71Sopenharmony_ci} 168e41f4b71Sopenharmony_ci``` 169e41f4b71Sopenharmony_ci 170e41f4b71Sopenharmony_ci### SAR<sup>10+</sup> 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_cioff(type: SensorId.SAR, callback?: Callback<SarResponse>): void 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_ciUnsubscribes from data of the SAR sensor. 175e41f4b71Sopenharmony_ci 176e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.Sensor 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_ci**System API**: This is a system API. 179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_ci**Parameters** 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 183e41f4b71Sopenharmony_ci| -------- | --------------------------------------------- | ---- | ------------------------------------------------------------ | 184e41f4b71Sopenharmony_ci| type | [SensorId](#sensorid9).SAR | Yes | Sensor type. The value is fixed at **SensorId.SAR**. | 185e41f4b71Sopenharmony_ci| callback | Callback<[SarResponse](#sarresponse10)> | No | Callback used for unsubscription. If this parameter is not specified, all callbacks of the specified sensor type are unsubscribed from.| 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_ci**Error codes** 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci| ID| Error Message | 192e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | 193e41f4b71Sopenharmony_ci| 202 | Permission check failed. A non-system application uses the system API. | 194e41f4b71Sopenharmony_ci| 401 | Parameter error.Possible causes:1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 195e41f4b71Sopenharmony_ci 196e41f4b71Sopenharmony_ci**Example** 197e41f4b71Sopenharmony_ci 198e41f4b71Sopenharmony_ci```ts 199e41f4b71Sopenharmony_ciimport { sensor } from '@kit.SensorServiceKit'; 200e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 201e41f4b71Sopenharmony_ci 202e41f4b71Sopenharmony_cifunction callback1(data: object) { 203e41f4b71Sopenharmony_ci console.info('Succeeded in getting callback1 data: ' + JSON.stringify(data)); 204e41f4b71Sopenharmony_ci} 205e41f4b71Sopenharmony_ci 206e41f4b71Sopenharmony_cifunction callback2(data: object) { 207e41f4b71Sopenharmony_ci console.info('Succeeded in getting callback2 data: ' + JSON.stringify(data)); 208e41f4b71Sopenharmony_ci} 209e41f4b71Sopenharmony_ci 210e41f4b71Sopenharmony_citry { 211e41f4b71Sopenharmony_ci sensor.on(sensor.SensorId.SAR, callback1); 212e41f4b71Sopenharmony_ci sensor.on(sensor.SensorId.SAR, callback2); 213e41f4b71Sopenharmony_ci // Unsubscribe from callback1. 214e41f4b71Sopenharmony_ci sensor.off(sensor.SensorId.SAR, callback1); 215e41f4b71Sopenharmony_ci // Unsubscribe from all callbacks of the SensorId.SAR type. 216e41f4b71Sopenharmony_ci sensor.off(sensor.SensorId.SAR); 217e41f4b71Sopenharmony_ci} catch (error) { 218e41f4b71Sopenharmony_ci let e: BusinessError = error as BusinessError; 219e41f4b71Sopenharmony_ci console.error(`Failed to invoke off. Code: ${e.code}, message: ${e.message}`); 220e41f4b71Sopenharmony_ci} 221e41f4b71Sopenharmony_ci``` 222e41f4b71Sopenharmony_ci 223e41f4b71Sopenharmony_ci## SensorId<sup>9+</sup> 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ciEnumerates the sensor types. 226e41f4b71Sopenharmony_ci 227e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.Sensor 228e41f4b71Sopenharmony_ci 229e41f4b71Sopenharmony_ci| Name | Value | Description | 230e41f4b71Sopenharmony_ci| ------------------- | ---- | --------------------------------------------- | 231e41f4b71Sopenharmony_ci| COLOR<sup>10+</sup> | 14 | Color sensor.<br>System API: This is a system API. | 232e41f4b71Sopenharmony_ci| SAR<sup>10+</sup> | 15 | Sodium Adsorption Ratio (SAR) sensor.<br>System API: This is a system API.| 233e41f4b71Sopenharmony_ci 234e41f4b71Sopenharmony_ci## ColorResponse<sup>10+</sup> 235e41f4b71Sopenharmony_ci 236e41f4b71Sopenharmony_ciDescribes the color sensor data. It extends from [Response](js-apis-sensor.md#response). 237e41f4b71Sopenharmony_ci 238e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.Sensor 239e41f4b71Sopenharmony_ci 240e41f4b71Sopenharmony_ci**System API**: This is a system API. 241e41f4b71Sopenharmony_ci 242e41f4b71Sopenharmony_ci 243e41f4b71Sopenharmony_ci| Name | Type | Readable| Writable| Description | 244e41f4b71Sopenharmony_ci| ---------------- | ------ | ---- | ---- | ----------------------------- | 245e41f4b71Sopenharmony_ci| lightIntensity | number | Yes | Yes | Intensity of light, in lux.| 246e41f4b71Sopenharmony_ci| colorTemperature | number | Yes | Yes | Color temperature, in Kelvin. | 247e41f4b71Sopenharmony_ci 248e41f4b71Sopenharmony_ci## SarResponse<sup>10+ </sup> 249e41f4b71Sopenharmony_ci 250e41f4b71Sopenharmony_ciDescribes the SAR sensor data. It extends from [Response](js-apis-sensor.md#response). 251e41f4b71Sopenharmony_ci 252e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Sensors.Sensor 253e41f4b71Sopenharmony_ci 254e41f4b71Sopenharmony_ci**System API**: This is a system API. 255e41f4b71Sopenharmony_ci 256e41f4b71Sopenharmony_ci 257e41f4b71Sopenharmony_ci| Name | Type | Readable| Writable| Description | 258e41f4b71Sopenharmony_ci| --------------- | ------ | ---- | ---- | ------------------------------- | 259e41f4b71Sopenharmony_ci| absorptionRatio | number | Yes | Yes | Absorption ratio, in W/kg.| 260