1e41f4b71Sopenharmony_ci# Vibrator Development (ArkTS)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ci## When to Use
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ciYou can set different vibration effects as needed, for example, customizing the vibration intensity, frequency, and duration for button touches, alarm clocks, and incoming calls.
7e41f4b71Sopenharmony_ci
8e41f4b71Sopenharmony_ciFor details about the APIs, see [Vibrator](../../reference/apis-sensor-service-kit/js-apis-vibrator.md).
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci## Available APIs
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci| Name                                                        | Description                                                        |
14e41f4b71Sopenharmony_ci| ------------------------------------------------------------ | ------------------------------------------------------------ |
15e41f4b71Sopenharmony_ci| startVibration(effect: VibrateEffect, attribute: VibrateAttribute): Promise<void> | Starts vibration with the specified effect and attribute. This API uses a promise to return the result.|
16e41f4b71Sopenharmony_ci| startVibration(effect: VibrateEffect, attribute: VibrateAttribute, callback: AsyncCallback<void>): void | Starts vibration with the specified effect and attribute. This API uses an asynchronous callback to return the result.|
17e41f4b71Sopenharmony_ci| stopVibration(stopMode: VibratorStopMode): Promise<void> | Stops vibration in the specified mode. This API uses a promise to return the result.           |
18e41f4b71Sopenharmony_ci| stopVibration(stopMode: VibratorStopMode, callback: AsyncCallback<void>): void | Stops vibration in the specified mode. This API uses an asynchronous callback to return the result.          |
19e41f4b71Sopenharmony_ci| stopVibration(): Promise<void>                         | Stops vibration in all modes. This API uses a promise to return the result.               |
20e41f4b71Sopenharmony_ci| stopVibration(callback: AsyncCallback<void>): void     | Stops vibration in all modes. This API uses an asynchronous callback to return the result.              |
21e41f4b71Sopenharmony_ci| isSupportEffect(effectId: string): Promise<boolean>    | Checks whether an effect ID is supported. This API uses a promise to return the result. This API uses a promise to return the result. The return value **true** means that the effect ID is supported, and **false** means the opposite.|
22e41f4b71Sopenharmony_ci| isSupportEffect(effectId: string, callback: AsyncCallback<boolean>): void | Checks whether an effect ID is supported. This API uses an asynchronous callback to return the result. This API uses an asynchronous callback to return the result. The return value **true** means that the effect ID is supported, and **false** means the opposite.|
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci## Vibration Effect Description
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ciCurrently, three types of vibration effects are supported.
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci### Fixed-Duration Vibration
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ciOnly a fixed duration is passed in, and the device vibrates based on the default intensity and frequency. For details about the vibration effect, see [VibrateTime](../../reference/apis-sensor-service-kit/js-apis-vibrator.md#vibratetime9).
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci### Preset Vibration
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ciCertain [vibration effects are preset](../../reference/apis-sensor-service-kit/js-apis-vibrator.md#effectid) for fixed scenes. For example, the effect "haptic.clock.timer" is preset to provide feedback when a user adjusts the timer. For details about the vibration effect, see [VibratePreset](../../reference/apis-sensor-service-kit/js-apis-vibrator.md#vibratepreset9).
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci### Custom Vibration
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ciCustom vibration enables you to design vibration effects by customizing a vibration configuration file and orchestrating vibration forms based on the corresponding rules. For details about the vibration effect, see [VibrateFromFile](../../reference/apis-sensor-service-kit/js-apis-vibrator.md#vibratefromfile10).
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ciThe custom vibration configuration file is in JSON format. An example file is as follows:
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci```json
44e41f4b71Sopenharmony_ci{
45e41f4b71Sopenharmony_ci    "MetaData": {
46e41f4b71Sopenharmony_ci        "Create": "2023-01-09",
47e41f4b71Sopenharmony_ci        "Description": "a haptic case",
48e41f4b71Sopenharmony_ci        "Version": 1.0,
49e41f4b71Sopenharmony_ci        "ChannelNumber": 1
50e41f4b71Sopenharmony_ci    },
51e41f4b71Sopenharmony_ci    "Channels": [
52e41f4b71Sopenharmony_ci        {
53e41f4b71Sopenharmony_ci            "Parameters": {
54e41f4b71Sopenharmony_ci                "Index": 0
55e41f4b71Sopenharmony_ci            },
56e41f4b71Sopenharmony_ci            "Pattern": [
57e41f4b71Sopenharmony_ci                {
58e41f4b71Sopenharmony_ci                    "Event": {
59e41f4b71Sopenharmony_ci                        "Type": "transient",
60e41f4b71Sopenharmony_ci                        "StartTime": 0,
61e41f4b71Sopenharmony_ci                        "Parameters": {
62e41f4b71Sopenharmony_ci                            "Frequency": 31,
63e41f4b71Sopenharmony_ci                            "Intensity": 100
64e41f4b71Sopenharmony_ci                        }
65e41f4b71Sopenharmony_ci                    }
66e41f4b71Sopenharmony_ci                },
67e41f4b71Sopenharmony_ci                {
68e41f4b71Sopenharmony_ci                    "Event": {
69e41f4b71Sopenharmony_ci                        "Type": "continuous",
70e41f4b71Sopenharmony_ci                        "StartTime": 40,
71e41f4b71Sopenharmony_ci                        "Duration": 54,
72e41f4b71Sopenharmony_ci                        "Parameters": {
73e41f4b71Sopenharmony_ci                            "Frequency": 30,
74e41f4b71Sopenharmony_ci                            "Intensity": 38,
75e41f4b71Sopenharmony_ci                            "Curve": [
76e41f4b71Sopenharmony_ci                                {
77e41f4b71Sopenharmony_ci                                    "Time": 0,
78e41f4b71Sopenharmony_ci                                    "Frequency": 0,
79e41f4b71Sopenharmony_ci                                    "Intensity": 0
80e41f4b71Sopenharmony_ci                                },
81e41f4b71Sopenharmony_ci                                {
82e41f4b71Sopenharmony_ci                                    "Time": 1,
83e41f4b71Sopenharmony_ci                                    "Frequency": 15,
84e41f4b71Sopenharmony_ci                                    "Intensity": 0.5
85e41f4b71Sopenharmony_ci                                },
86e41f4b71Sopenharmony_ci                                {
87e41f4b71Sopenharmony_ci                                    "Time": 40,
88e41f4b71Sopenharmony_ci                                    "Frequency": -8,
89e41f4b71Sopenharmony_ci                                    "Intensity": 1.0
90e41f4b71Sopenharmony_ci                                },
91e41f4b71Sopenharmony_ci                                {
92e41f4b71Sopenharmony_ci                                    "Time": 54,
93e41f4b71Sopenharmony_ci                                    "Frequency": 0,
94e41f4b71Sopenharmony_ci                                    "Intensity": 0
95e41f4b71Sopenharmony_ci                                }
96e41f4b71Sopenharmony_ci                            ]
97e41f4b71Sopenharmony_ci                        }
98e41f4b71Sopenharmony_ci                    }
99e41f4b71Sopenharmony_ci                }
100e41f4b71Sopenharmony_ci            ]
101e41f4b71Sopenharmony_ci        }
102e41f4b71Sopenharmony_ci    ]
103e41f4b71Sopenharmony_ci}
104e41f4b71Sopenharmony_ci```
105e41f4b71Sopenharmony_ci
106e41f4b71Sopenharmony_ciThis JSON file contains two attributes: **MetaData** and **Channels**.
107e41f4b71Sopenharmony_ci1. **MetaData** contains information about the file header. You can add the following attributes under **MetaData**.
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci     | Name         | Mandatory| Description                                         |
110e41f4b71Sopenharmony_ci     | ------------- | ------ | --------------------------------------------- |
111e41f4b71Sopenharmony_ci     | Version       | Yes    | Version number of the file format, which is forward compatible. Currently, only version 1.0 is supported.|
112e41f4b71Sopenharmony_ci     | ChannelNumber | Yes    | Number of channels for vibration. A maximum of two channels are supported.   |
113e41f4b71Sopenharmony_ci     | Create        | No    | Time when the file was created.                         |
114e41f4b71Sopenharmony_ci     | Description   | No    | Additional information such as the vibration effect and creation information.         |
115e41f4b71Sopenharmony_ci
116e41f4b71Sopenharmony_ci2. **Channels** provides information about the vibration channel.
117e41f4b71Sopenharmony_ci
118e41f4b71Sopenharmony_ci     It is a JSON array that holds information about each channel. It contains two attributes: **Parameters** and **Pattern**.
119e41f4b71Sopenharmony_ci
120e41f4b71Sopenharmony_ci     | Name      | Mandatory| Description                                                        |
121e41f4b71Sopenharmony_ci     | ---------- | ------ | ------------------------------------------------------------ |
122e41f4b71Sopenharmony_ci     | Parameters | Yes    | Channel parameters. Among them, **Index** indicates the channel ID. The value **0** indicates both channels, **1** indicates the left channel, and **2** indicates the right channel.|
123e41f4b71Sopenharmony_ci     | Pattern    | No    | Vibration sequence.                                              |
124e41f4b71Sopenharmony_ci
125e41f4b71Sopenharmony_ci     **Pattern** is a JSON array that holds the vibration events. Under it, **Event** indicates a vibration event, which can be either of the following types:
126e41f4b71Sopenharmony_ci
127e41f4b71Sopenharmony_ci     | Vibration Type  | Description                                          |
128e41f4b71Sopenharmony_ci     | ---------- | ---------------------------------------------- |
129e41f4b71Sopenharmony_ci     | transient  | Short vibration.                        |
130e41f4b71Sopenharmony_ci     | continuous | Long vibration.|
131e41f4b71Sopenharmony_ci
132e41f4b71Sopenharmony_ci     A vibration event contains the following attributes:
133e41f4b71Sopenharmony_ci
134e41f4b71Sopenharmony_ci     | Name     | Mandatory| Description                                                        |
135e41f4b71Sopenharmony_ci     | --------- | ------ | ------------------------------------------------------------ |
136e41f4b71Sopenharmony_ci     | Type      | Yes    | Type of the vibration event, which can be **transient** or **continuous**.                |
137e41f4b71Sopenharmony_ci     | StartTime | Yes    | Vibration start time. The value range is [0, 1800000], in ms.           |
138e41f4b71Sopenharmony_ci     | Duration  | Yes    | Vibration duration. This parameter is valid only when **Type** is set to **continuous**. The value range is [0, 5000], in ms.|
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ci3. **Parameters** provides the following parameters related to the vibration event and is mandatory.
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci     | Name     | Mandatory| Description                                                        |
143e41f4b71Sopenharmony_ci     | --------- | ------ | ------------------------------------------------------------ |
144e41f4b71Sopenharmony_ci     | Intensity | Yes    | Vibration intensity. The value range is [0, 100].                          |
145e41f4b71Sopenharmony_ci     | Frequency | Yes    | Vibration frequency. The value range is [0, 100].                          |
146e41f4b71Sopenharmony_ci     | Curve     | No    | Vibration curve. This parameter is valid only when **Type** is set to **continuous**. It is a JSON array that holds 4 to 16 adjustment points. Each adjustment point must contain the following attributes:<br>**Time**: offset relative to the event start time. The value ranges from 0 to the vibration duration.<br>**Intensity**: gain relative to the vibration intensity. The value range is [0, 1]. This value multiplied by the vibration intensity is the adjusted intensity at the corresponding time point.<br>**Frequency**: change relative to the vibration frequency. The value range is [-100, 100]. This value plus the vibration frequency is the adjusted frequency at the corresponding time point.|
147e41f4b71Sopenharmony_ci
148e41f4b71Sopenharmony_ciThe following requirements must be met:
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ci| Item| Description                |
151e41f4b71Sopenharmony_ci| -------- | ------------------------ |
152e41f4b71Sopenharmony_ci| Number of vibration events| No more than 128|
153e41f4b71Sopenharmony_ci| Length of the vibration configuration file| Not greater than 64 KB|
154e41f4b71Sopenharmony_ci
155e41f4b71Sopenharmony_ci
156e41f4b71Sopenharmony_ci## How to Develop
157e41f4b71Sopenharmony_ci
158e41f4b71Sopenharmony_ci1. Before using the vibrator on a device, you must declare the **ohos.permission.VIBRATE** permission. For details, see [Declaring Permissions](../../security/AccessToken/declare-permissions.md).
159e41f4b71Sopenharmony_ci
160e41f4b71Sopenharmony_ci2. Start vibration with the specified effect and attribute.
161e41f4b71Sopenharmony_ci
162e41f4b71Sopenharmony_ci   Scenario 1: Trigger vibration with the specified duration.
163e41f4b71Sopenharmony_ci
164e41f4b71Sopenharmony_ci   ```ts
165e41f4b71Sopenharmony_ci   import { vibrator } from '@kit.SensorServiceKit';
166e41f4b71Sopenharmony_ci   import { BusinessError } from '@kit.BasicServicesKit';
167e41f4b71Sopenharmony_ci   
168e41f4b71Sopenharmony_ci   try {
169e41f4b71Sopenharmony_ci     // Start vibration.
170e41f4b71Sopenharmony_ci     vibrator.startVibration({
171e41f4b71Sopenharmony_ci       type: 'time',
172e41f4b71Sopenharmony_ci       duration: 1000,
173e41f4b71Sopenharmony_ci     }, {
174e41f4b71Sopenharmony_ci       id: 0,
175e41f4b71Sopenharmony_ci       usage: 'alarm'
176e41f4b71Sopenharmony_ci     }, (error: BusinessError) => {
177e41f4b71Sopenharmony_ci       if (error) {
178e41f4b71Sopenharmony_ci         console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
179e41f4b71Sopenharmony_ci         return;
180e41f4b71Sopenharmony_ci       }
181e41f4b71Sopenharmony_ci       console.info('Succeed in starting vibration');
182e41f4b71Sopenharmony_ci     });
183e41f4b71Sopenharmony_ci   } catch (err) {
184e41f4b71Sopenharmony_ci     let e: BusinessError = err as BusinessError;
185e41f4b71Sopenharmony_ci     console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
186e41f4b71Sopenharmony_ci   }
187e41f4b71Sopenharmony_ci   ```
188e41f4b71Sopenharmony_ci
189e41f4b71Sopenharmony_ci   Scenario 2: Trigger vibration with a preset effect. You can check whether the preset effect is supported before calling **startVibration()**.
190e41f4b71Sopenharmony_ci
191e41f4b71Sopenharmony_ci   ```ts
192e41f4b71Sopenharmony_ci   import { vibrator } from '@kit.SensorServiceKit';
193e41f4b71Sopenharmony_ci   import { BusinessError } from '@kit.BasicServicesKit';
194e41f4b71Sopenharmony_ci   
195e41f4b71Sopenharmony_ci   try {
196e41f4b71Sopenharmony_ci     // Check whether 'haptic.effect.soft' is supported.
197e41f4b71Sopenharmony_ci     vibrator.isSupportEffect('haptic.effect.soft', (err: BusinessError, state: boolean) => {
198e41f4b71Sopenharmony_ci       if (err) {
199e41f4b71Sopenharmony_ci         console.error(`Failed to query effect. Code: ${err.code}, message: ${err.message}`);
200e41f4b71Sopenharmony_ci         return;
201e41f4b71Sopenharmony_ci       }
202e41f4b71Sopenharmony_ci       console.info('Succeed in querying effect');
203e41f4b71Sopenharmony_ci       if (state) {
204e41f4b71Sopenharmony_ci         try {
205e41f4b71Sopenharmony_ci           // Start vibration.
206e41f4b71Sopenharmony_ci           vibrator.startVibration({
207e41f4b71Sopenharmony_ci             type: 'preset',
208e41f4b71Sopenharmony_ci             effectId: 'haptic.effect.soft',
209e41f4b71Sopenharmony_ci             count: 1,
210e41f4b71Sopenharmony_ci             intensity: 50,
211e41f4b71Sopenharmony_ci           }, {
212e41f4b71Sopenharmony_ci             usage: 'unknown'
213e41f4b71Sopenharmony_ci           }, (error: BusinessError) => {
214e41f4b71Sopenharmony_ci             if (error) {
215e41f4b71Sopenharmony_ci               console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
216e41f4b71Sopenharmony_ci             } else {
217e41f4b71Sopenharmony_ci               console.info('Succeed in starting vibration');
218e41f4b71Sopenharmony_ci             }
219e41f4b71Sopenharmony_ci           });
220e41f4b71Sopenharmony_ci         } catch (error) {
221e41f4b71Sopenharmony_ci           let e: BusinessError = error as BusinessError;
222e41f4b71Sopenharmony_ci           console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
223e41f4b71Sopenharmony_ci         }
224e41f4b71Sopenharmony_ci       }
225e41f4b71Sopenharmony_ci     })
226e41f4b71Sopenharmony_ci   } catch (error) {
227e41f4b71Sopenharmony_ci     let e: BusinessError = error as BusinessError;
228e41f4b71Sopenharmony_ci     console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
229e41f4b71Sopenharmony_ci   }
230e41f4b71Sopenharmony_ci   ```
231e41f4b71Sopenharmony_ci
232e41f4b71Sopenharmony_ci   Scenario 3: Trigger vibration according to a custom vibration configuration file.
233e41f4b71Sopenharmony_ci
234e41f4b71Sopenharmony_ci   ```ts
235e41f4b71Sopenharmony_ci   import { vibrator } from '@kit.SensorServiceKit';
236e41f4b71Sopenharmony_ci   import { resourceManager } from '@kit.LocalizationKit';
237e41f4b71Sopenharmony_ci   import { BusinessError } from '@kit.BasicServicesKit';
238e41f4b71Sopenharmony_ci   
239e41f4b71Sopenharmony_ci   const fileName: string = 'xxx.json';
240e41f4b71Sopenharmony_ci   
241e41f4b71Sopenharmony_ci   // Obtain the file descriptor of the vibration configuration file.
242e41f4b71Sopenharmony_ci   let rawFd: resourceManager.RawFileDescriptor = getContext().resourceManager.getRawFdSync(fileName);
243e41f4b71Sopenharmony_ci   
244e41f4b71Sopenharmony_ci   // Start vibration.
245e41f4b71Sopenharmony_ci   try {
246e41f4b71Sopenharmony_ci     vibrator.startVibration({
247e41f4b71Sopenharmony_ci       type: "file",
248e41f4b71Sopenharmony_ci       hapticFd: { fd: rawFd.fd, offset: rawFd.offset, length: rawFd.length }
249e41f4b71Sopenharmony_ci     }, {
250e41f4b71Sopenharmony_ci       id: 0,
251e41f4b71Sopenharmony_ci       usage: 'alarm'
252e41f4b71Sopenharmony_ci     }, (error: BusinessError) => {
253e41f4b71Sopenharmony_ci       if (error) {
254e41f4b71Sopenharmony_ci         console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
255e41f4b71Sopenharmony_ci         return;
256e41f4b71Sopenharmony_ci       }
257e41f4b71Sopenharmony_ci       console.info('Succeed in starting vibration');
258e41f4b71Sopenharmony_ci     });
259e41f4b71Sopenharmony_ci   } catch (err) {
260e41f4b71Sopenharmony_ci     let e: BusinessError = err as BusinessError;
261e41f4b71Sopenharmony_ci     console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
262e41f4b71Sopenharmony_ci   }
263e41f4b71Sopenharmony_ci   
264e41f4b71Sopenharmony_ci   // Close the file descriptor of the vibration configuration file.
265e41f4b71Sopenharmony_ci   getContext().resourceManager.closeRawFdSync(fileName);
266e41f4b71Sopenharmony_ci   ```
267e41f4b71Sopenharmony_ci
268e41f4b71Sopenharmony_ci3. Stop vibration.
269e41f4b71Sopenharmony_ci
270e41f4b71Sopenharmony_ci   Method 1: Stop vibration in the specified mode. This method is invalid for custom vibration.
271e41f4b71Sopenharmony_ci
272e41f4b71Sopenharmony_ci   ​	Stop fixed-duration vibration.
273e41f4b71Sopenharmony_ci
274e41f4b71Sopenharmony_ci   ```ts
275e41f4b71Sopenharmony_ci   import { vibrator } from '@kit.SensorServiceKit';
276e41f4b71Sopenharmony_ci   import { BusinessError } from '@kit.BasicServicesKit';
277e41f4b71Sopenharmony_ci   
278e41f4b71Sopenharmony_ci   try {
279e41f4b71Sopenharmony_ci     // Stop vibration in VIBRATOR_STOP_MODE_TIME mode.
280e41f4b71Sopenharmony_ci     vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME, (error: BusinessError) => {
281e41f4b71Sopenharmony_ci       if (error) {
282e41f4b71Sopenharmony_ci         console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
283e41f4b71Sopenharmony_ci         return;
284e41f4b71Sopenharmony_ci       }
285e41f4b71Sopenharmony_ci       console.info('Succeed in stopping vibration');
286e41f4b71Sopenharmony_ci     })
287e41f4b71Sopenharmony_ci   } catch (err) {
288e41f4b71Sopenharmony_ci     let e: BusinessError = err as BusinessError;
289e41f4b71Sopenharmony_ci     console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
290e41f4b71Sopenharmony_ci   }
291e41f4b71Sopenharmony_ci   ```
292e41f4b71Sopenharmony_ci
293e41f4b71Sopenharmony_ci   ​	Stop preset vibration.
294e41f4b71Sopenharmony_ci
295e41f4b71Sopenharmony_ci   ```ts
296e41f4b71Sopenharmony_ci   import { vibrator } from '@kit.SensorServiceKit';
297e41f4b71Sopenharmony_ci   import { BusinessError } from '@kit.BasicServicesKit';
298e41f4b71Sopenharmony_ci   
299e41f4b71Sopenharmony_ci   try {
300e41f4b71Sopenharmony_ci     // Stop vibration in VIBRATOR_STOP_MODE_PRESET mode.
301e41f4b71Sopenharmony_ci     vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, (error: BusinessError) => {
302e41f4b71Sopenharmony_ci       if (error) {
303e41f4b71Sopenharmony_ci         console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
304e41f4b71Sopenharmony_ci         return;
305e41f4b71Sopenharmony_ci       }
306e41f4b71Sopenharmony_ci       console.info('Succeed in stopping vibration');
307e41f4b71Sopenharmony_ci     })
308e41f4b71Sopenharmony_ci   } catch (err) {
309e41f4b71Sopenharmony_ci     let e: BusinessError = err as BusinessError;
310e41f4b71Sopenharmony_ci     console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
311e41f4b71Sopenharmony_ci   }
312e41f4b71Sopenharmony_ci   ```
313e41f4b71Sopenharmony_ci
314e41f4b71Sopenharmony_ci   Method 2: Stop vibration in all modes, including custom vibration.
315e41f4b71Sopenharmony_ci
316e41f4b71Sopenharmony_ci   ```ts
317e41f4b71Sopenharmony_ci   import { vibrator } from '@kit.SensorServiceKit';
318e41f4b71Sopenharmony_ci   import { BusinessError } from '@kit.BasicServicesKit';
319e41f4b71Sopenharmony_ci   
320e41f4b71Sopenharmony_ci   try {
321e41f4b71Sopenharmony_ci     // Stop vibration in all modes.
322e41f4b71Sopenharmony_ci     vibrator.stopVibration((error: BusinessError) => {
323e41f4b71Sopenharmony_ci       if (error) {
324e41f4b71Sopenharmony_ci         console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
325e41f4b71Sopenharmony_ci         return;
326e41f4b71Sopenharmony_ci       }
327e41f4b71Sopenharmony_ci       console.info('Succeed in stopping vibration');
328e41f4b71Sopenharmony_ci     })
329e41f4b71Sopenharmony_ci   } catch (error) {
330e41f4b71Sopenharmony_ci     let e: BusinessError = error as BusinessError;
331e41f4b71Sopenharmony_ci     console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
332e41f4b71Sopenharmony_ci   }
333e41f4b71Sopenharmony_ci   ```
334