sensor-guidelines.md 2.9 KB
Newer Older
Z
zengyawen 已提交
1 2 3 4 5
# Sensor Development


## When to Use

G
gloria 已提交
6
With 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.
Z
zengyawen 已提交
7

G
gloria 已提交
8
For details about the APIs, see [Sensor](../reference/apis/js-apis-sensor.md).
Z
zengyawen 已提交
9 10 11 12


## Available APIs

W
wusongqing 已提交
13
| Module| API| Description|
Z
zengyawen 已提交
14
| -------- | -------- | -------- |
G
gloria 已提交
15 16 17
| ohos.sensor | sensor.on(sensorId, callback:AsyncCallback<Response>): void | Subscribes to data changes of a type of sensor.|
| ohos.sensor | sensor.once(sensorId, callback:AsyncCallback<Response>): void | Subscribes to only one data change of a type of sensor.|
| ohos.sensor | sensor.off(sensorId, callback?:AsyncCallback<void>): void | Unsubscribes from sensor data changes.|
Z
zengyawen 已提交
18 19 20 21


## How to Develop

G
Gloria 已提交
22 23 24 25 26 27 28 29 30 31 32
1. Before obtaining data from a type of sensor, check whether the required permission has been configured.<br>
     The system provides the following sensor-related permissions:
   - ohos.permission.ACCELEROMETER

   - ohos.permission.GYROSCOPE

   - ohos.permission.ACTIVITY_MOTION

   - ohos.permission.READ_HEALTH_DATA

   For details about how to configure a permission, see [Declaring Permissions](../security/accesstoken-guidelines.md).
W
wusongqing 已提交
33
   
G
gloria 已提交
34
2. Subscribe to data changes of a type of sensor. The following uses the acceleration sensor as an example. 
Z
zengyawen 已提交
35
  
G
gloria 已提交
36
   ```js
G
Gloria 已提交
37
   import sensor from "@ohos.sensor";
G
gloria 已提交
38
   sensor.on(sensor.SensorId.ACCELEROMETER, function(data){
G
Gloria 已提交
39 40
      console.info("Data obtained successfully. x: " + data.x + "y: " + data.y + "z: " + data.z); // Data is obtained.
   });
Z
zengyawen 已提交
41
   ```
W
wusongqing 已提交
42
   
G
gloria 已提交
43
   ![171e6f30-a8d9-414c-bafa-b430340305fb](figures/171e6f30-a8d9-414c-bafa-b430340305fb.png)
Z
zengyawen 已提交
44 45 46

3. Unsubscribe from sensor data changes.
  
G
gloria 已提交
47
   ```js
G
Gloria 已提交
48
   import sensor from "@ohos.sensor";
G
gloria 已提交
49
   sensor.off(sensor.SensorId.ACCELEROMETER);
Z
zengyawen 已提交
50
   ```
W
wusongqing 已提交
51
   
G
gloria 已提交
52
   ![65d69983-29f6-4381-80a3-f9ef2ec19e53](figures/65d69983-29f6-4381-80a3-f9ef2ec19e53.png)
Z
zengyawen 已提交
53 54 55

4. Subscribe to only one data change of a type of sensor.
  
G
gloria 已提交
56
   ```js
G
Gloria 已提交
57
   import sensor from "@ohos.sensor";
G
gloria 已提交
58
   sensor.once(sensor.SensorId.ACCELEROMETER, function(data) {
G
Gloria 已提交
59 60
      console.info("Data obtained successfully. x: " + data.x + "y: " + data.y + "z: " + data.z); // Data is obtained.
   });
Z
zengyawen 已提交
61
   ```
W
wusongqing 已提交
62
   
G
gloria 已提交
63
   ![db5d017d-6c1c-4a71-a2dd-f74b7f23239e](figures/db5d017d-6c1c-4a71-a2dd-f74b7f23239e.png)
W
wusongqing 已提交
64 65 66

   If the API fails to be called, you are advised to use the **try/catch** statement to capture error information that may occur in the code. Example:

G
gloria 已提交
67
    ```js
G
Gloria 已提交
68
   import sensor from "@ohos.sensor";
W
wusongqing 已提交
69
    try {
G
gloria 已提交
70
      sensor.once(sensor.SensorId.ACCELEROMETER, function(data) {
G
Gloria 已提交
71
          console.info("Data obtained successfully. x: " + data.x + "y: " + data.y + "z: " + data.z); // Data is obtained.
W
wusongqing 已提交
72 73
      });
    } catch (error) {
G
gloria 已提交
74
      console.error("Get sensor data error. data:" + error.data, " msg:", error.message);
W
wusongqing 已提交
75
    }
G
gloria 已提交
76
    ```