• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Stationary Development
2
3
4## When to Use
5
6An application can call the **Stationary** module to obtain the device status, for example, whether the device is absolutely or relatively still.
7
8For details about the APIs, see [Stationary](../../reference/apis-multimodalawareness-kit/js-apis-stationary.md).
9
10## Device Status Type Parameters
11
12| Name| Description|
13| -------- | -------- |
14| still | Absolutely still.|
15| relativeStill | Relatively still.|
16
17## Parameters for Subscribing to Device Status events
18
19| Name                            | Value   | Description                                      |
20| ------------------------------ | ---- | ---------------------------------------- |
21| ENTER         | 1    | Event indicating entering device status.  |
22| EXIT | 2   | Event indicating exiting device status.|
23| ENTER_EXIT | 3   | Event indicating entering and exiting device status.|
24
25## Returned Device Status Parameters
26
27| Name                            | Value   | Description                                      |
28| ------------------------------ | ---- | ---------------------------------------- |
29| ENTER         | 1    | Entering device status.  |
30| EXIT | 2   | Exiting device status.|
31
32## Available APIs
33
34| Module         | Name                                                      | Description                                                        |
35| ------------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
36| ohos.stationary | on(activity: ActivityType, event: ActivityEvent, reportLatencyNs: number, callback: Callback<ActivityResponse>): void | Subscribes to the device status. This API uses an asynchronous callback to return the result.|
37| ohos.stationary | once(activity: ActivityType, callback: Callback<ActivityResponse>): void | Obtains the device status. This API uses an asynchronous callback to return the result.|
38| ohos.stationary | off(activity: ActivityType, event: ActivityEvent, callback?: Callback<ActivityResponse>): void | Unsubscribes from the device status.                                |
39
40## Constraints
41
42The device must support the acceleration sensor.
43
44Currently, only the algorithm framework is provided. The API test framework returns the following result: data={"type":3,"value":-1}.
45
46If the relative stationary and absolute stationary capabilities are required, you must implement them in **device_status/libs/src/algorithm**. The following code snippet is for reference:
47
48   ```ts
49   algoPara_.resultantAcc =
50      sqrt((algoPara_.x * algoPara_.x) + (algoPara_.y * algoPara_.y) + (algoPara_.z * algoPara_.z));
51   if ((algoPara_.resultantAcc > RESULTANT_ACC_LOW_THRHD) && (algoPara_.resultantAcc < RESULTANT_ACC_UP_THRHD)) {
52      if (state_ == STILL) {
53         return;
54      }
55      counter_--;
56      if (counter_ == 0) {
57         counter_ = COUNTER_THRESHOLD;
58         UpdateStateAndReport(VALUE_ENTER, STILL, TYPE_ABSOLUTE_STILL);
59      }
60   } else {
61      counter_ = COUNTER_THRESHOLD;
62      if (state_ == UNSTILL) {
63         return;
64      }
65      UpdateStateAndReport(VALUE_EXIT, UNSTILL, TYPE_ABSOLUTE_STILL);
66   }
67   ```
68
69## How to Develop
70
711. Subscribe to the event indicating entering the absolute still state, and the event is reported every 1 second.
72
73   ```ts
74   import stationary from '@ohos.stationary';
75   import { BusinessError } from '@ohos.base';
76   let reportLatencyNs = 1000000000;
77   try {
78      stationary.on('still', stationary.ActivityEvent.ENTER, reportLatencyNs, (data) => {
79         console.log('data='+ JSON.stringify(data));
80      })
81   } catch (error) {
82      let message = (error as BusinessError).message;
83      console.error('stationary on failed:' + message);
84   }
85   ```
86
872. Obtain the event indicating entering the absolute still state.
88
89   ```ts
90   import stationary from '@ohos.stationary';
91   import { BusinessError } from '@ohos.base';
92   try {
93      stationary.once('still', (data) => {
94         console.log('data='+ JSON.stringify(data));
95      })
96   } catch (error) {
97      let message = (error as BusinessError).message;
98      console.error('stationary once failed:' + message);
99   }
100   ```
101
1023. Unsubscribe from the event indicating entering the absolute still state.
103
104   ```ts
105   import stationary from '@ohos.stationary';
106   import { BusinessError } from '@ohos.base';
107   try {
108      stationary.off('still', stationary.ActivityEvent.ENTER, (data) => {
109         console.log('data='+ JSON.stringify(data));
110      })
111   } catch (error) {
112      let message = (error as BusinessError).message;
113      console.error('stationary off failed:' + message);
114   }
115   ```
116