1# Sensor Development (ArkTS) 2 3 4## When to Use 5 6With the sensor module, a device can obtain sensor data. For example, the device can subscribe to data of the orientation sensor to detect its own orientation, and data of the pedometer sensor to learn the number of steps the user walks every day. 7 8For details about the APIs, see [Sensor](../../reference/apis-sensor-service-kit/js-apis-sensor.md). 9 10 11## Available APIs 12 13| Name| Description| 14| -------- | -------- | 15| sensor.on(sensorId, callback:AsyncCallback<Response>): void | Subscribes to data changes of a type of sensor.| 16| sensor.once(sensorId, callback:AsyncCallback<Response>): void | Subscribes to only one data change of a type of sensor.| 17| sensor.off(sensorId, callback?:AsyncCallback<void>): void | Unsubscribes from sensor data changes.| 18| sensor.getSensorList(callback: AsyncCallback\<Array\<Sensor>>): void| Obtains information about all sensors on the device. This API uses an asynchronous callback to return the result.| 19 20 21## How to Develop 22 23The acceleration sensor is used as an example. 24 251. Import the module. 26 27 ```ts 28 import { sensor } from '@kit.SensorServiceKit'; 29 import { BusinessError } from '@kit.BasicServicesKit'; 30 ``` 31 322. Obtain information about all sensors on the device. 33 34 ```ts 35 sensor.getSensorList((error: BusinessError, data: Array<sensor.Sensor>) => { 36 if (error) { 37 console.info('getSensorList failed'); 38 } else { 39 console.info('getSensorList success'); 40 for (let i = 0; i < data.length; i++) { 41 console.info(JSON.stringify(data[i])); 42 } 43 } 44 }); 45 ``` 46 47  48 49 The minimum and the maximum sampling periods supported by the sensor are 5000000 ns and 200000000 ns, respectively. The sampling interval may vary depending on the sensor type. The specified sampling interval must be within this range. If the configured value is smaller than the minimum sampling interval of the sensor, the minimum sampling interval is used. If the configured value is larger than the maximum sampling interval of the sensor, the maximum sampling interval is used. A smaller value means a higher reporting frequency and a higher power consumption. 50 513. Check whether the corresponding permission has been configured. For details, see [Declaring Permissions](../../security/AccessToken/declare-permissions.md). 52 534. Register a listener. You can call **on()** or **once()** to listen for sensor data changes. 54 55 The **on()** API is used to continuously listen for data changes of the sensor. The sensor reporting interval is set to 100000000 ns. 56 57 ```ts 58 sensor.on(sensor.SensorId.ACCELEROMETER, (data: sensor.AccelerometerResponse) => { 59 console.info("Succeeded in obtaining data. x: " + data.x + " y: " + data.y + " z: " + data.z); 60 }, { interval: 100000000 }); 61 ``` 62 63  64 65 The **once()** API is used to listen for only one data change of the sensor. 66 67 ```ts 68 sensor.once(sensor.SensorId.ACCELEROMETER, (data: sensor.AccelerometerResponse) => { 69 console.info("Succeeded in obtaining data. x: " + data.x + " y: " + data.y + " z: " + data.z); 70 }); 71 ``` 72 73  74 755. Cancel continuous listening. 76 77 ```ts 78 sensor.off(sensor.SensorId.ACCELEROMETER); 79 ``` 80