sensor-guidelines.md 4.5 KB
Newer Older
W
wusongqing 已提交
1
# Sensor Development
H
update  
HelloCrease 已提交
2 3


W
wusongqing 已提交
4
## When to Use
H
update  
HelloCrease 已提交
5

W
wusongqing 已提交
6
- Data provided by the compass sensor denotes the current orientation of the user device, which helps your application accurately navigate for the user.
H
update  
HelloCrease 已提交
7

W
wusongqing 已提交
8
- Data provided by the proximity sensor denotes the distance between the device and a visible object, which enables the device to automatically turn on or off its screen accordingly to prevent accidental touch on the screen.
H
update  
HelloCrease 已提交
9

W
wusongqing 已提交
10
- Data provided by the barometer sensor helps your application accurately determine the altitude of the device.
H
update  
HelloCrease 已提交
11

W
wusongqing 已提交
12
- Data provided by the ambient light sensor helps your device automatically adjust its backlight.
H
update  
HelloCrease 已提交
13

W
wusongqing 已提交
14
- Data provided by the Hall effect sensor implements the smart cover mode of your device.
H
update  
HelloCrease 已提交
15

W
wusongqing 已提交
16
- Data provided by the heart rate sensor helps your application track the health of a user.
H
update  
HelloCrease 已提交
17

W
wusongqing 已提交
18
- Data provided by the pedometer sensor helps your application obtain the number steps a user has walked.
H
update  
HelloCrease 已提交
19

W
wusongqing 已提交
20
- Data provided by the wear detection sensor helps your application detect whether a user is wearing a wearable device.
H
update  
HelloCrease 已提交
21 22


W
wusongqing 已提交
23
## Available APIs
H
update  
HelloCrease 已提交
24

W
wusongqing 已提交
25
| Module| API| Description|
H
update  
HelloCrease 已提交
26
| -------- | -------- | -------- |
W
wusongqing 已提交
27 28 29
| ohos.sensor | sensor.on(sensorType, callback:AsyncCallback<Response>): void | Subscribes to data changes of a type of sensor.|
| ohos.sensor | sensor.once(sensorType, callback:AsyncCallback<Response>): void | Subscribes to only one data change of a type of sensor.|
| ohos.sensor | sensor.off(sensorType, callback:AsyncCallback<void>): void | Unsubscribes from sensor data changes.|
H
update  
HelloCrease 已提交
30 31


W
wusongqing 已提交
32
## How to Develop
H
update  
HelloCrease 已提交
33

W
wusongqing 已提交
34
1. To obtain data from a type of sensor, configure the request permissions in the **config.json** file.  
H
update  
HelloCrease 已提交
35 36
  
   ```
W
wusongqing 已提交
37
   "reqPermissions":[
H
update  
HelloCrease 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
     {
        "name":"ohos.permission.ACCELEROMETER",
        "reason"":"", 
        "usedScene":{
         "ability": ["sensor.index.MainAbility",".MainAbility"],
         "when":"inuse"
       }
     },
     {
        "name":"ohos.permission.GYROSCOPE",
        "reason"":"", 
        "usedScene":{
         "ability": ["sensor.index.MainAbility",".MainAbility"],
         "when":"inuse"
       }
     },
     {
        "name":"ohos.permission.ACTIVITY_MOTION",
        "reason"":"ACTIVITY_MOTION_TEST", 
        "usedScene":{
         "ability": ["sensor.index.MainAbility",".MainAbility"],
         "when":"inuse"
       }
     },
     {
        "name":"ohos.permission.READ_HEALTH_DATA",
        "reason"":"HEALTH_DATA_TEST", 
        "usedScene":{
         "ability": ["sensor.index.MainAbility",".MainAbility"],
         "when":"inuse"
       }
     },
   ]
   ```
C
cff-gite 已提交
72
   
W
wusongqing 已提交
73
2. Subscribe to data changes of a type of sensor.
H
update  
HelloCrease 已提交
74 75 76
  
   ```
   import sensor from "@ohos.sensor"
C
cff-gite 已提交
77
   sensor.on(sensor.sensorType.SENSOR_TYPE_ACCELEROMETER,function(data){
W
wusongqing 已提交
78
          console.info("Subscription succeeded. data = "+ data);// The call is successful, and the obtained sensor data is printed.
H
update  
HelloCrease 已提交
79 80 81
     }
   );
   ```
C
cff-gite 已提交
82
   
W
wusongqing 已提交
83
   The following figure shows the successful call result when **SensorType** is **SENSOR_TYPE_ID_ACCELEROMETER**.
C
cff-gite 已提交
84
   
W
wusongqing 已提交
85
   ![en-us_image_0000001241693881](figures/en-us_image_0000001241693881.png)
H
update  
HelloCrease 已提交
86

W
wusongqing 已提交
87
3. Unsubscribe from sensor data changes.
H
update  
HelloCrease 已提交
88 89 90
  
   ```
   import sensor from "@ohos.sensor"
C
cff-gite 已提交
91
   sensor.off(sensor.sensorType.SENSOR_TYPE_ACCELEROMETER,function() {
W
wusongqing 已提交
92
       console.info("Succeeded in unsubscribing from acceleration sensor data.");// The unsubscription is successful, and the result is printed.
H
update  
HelloCrease 已提交
93 94 95
     }
   );
   ```
C
cff-gite 已提交
96
   
W
wusongqing 已提交
97
   The following figure shows the successful call result when **SensorType** is **SENSOR_TYPE_ID_ACCELEROMETER**.
C
cff-gite 已提交
98
   
W
wusongqing 已提交
99
   ![en-us_image_0000001196654004](figures/en-us_image_0000001196654004.png)
H
update  
HelloCrease 已提交
100

W
wusongqing 已提交
101
4. Subscribe to only one data change of a type of sensor.
H
update  
HelloCrease 已提交
102 103 104
  
   ```
   import sensor from "@ohos.sensor"
C
cff-gite 已提交
105
   sensor.once(sensor.sensorType.SENSOR_TYPE_ACCELEROMETER,function(data) {
W
wusongqing 已提交
106
           console.info("Data obtained successfully. data=" + data);// The call is successful, and the obtained sensor data is printed.
H
update  
HelloCrease 已提交
107 108 109
     }
   );
   ```
C
cff-gite 已提交
110
   
W
wusongqing 已提交
111
   The following figure shows the successful call result when **SensorType** is **SENSOR_TYPE_ID_ACCELEROMETER**.
C
cff-gite 已提交
112
   
W
wusongqing 已提交
113
   ![en-us_image_0000001241733907](figures/en-us_image_0000001241733907.png)
114

W
wusongqing 已提交
115
   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:
116 117 118 119

    ```
    try {
      sensor.once(sensor.sensorType.SENSOR_TYPE_ACCELEROMETER,function(data) {
W
wusongqing 已提交
120
          console.info("Data obtained successfully. data=" + data);// The call is successful, and the obtained sensor data is printed.
121 122 123 124 125
      });
    } catch (error) {
      console.error(error);
    }
    ```