1# Vibrator Development 2 3 4## When to Use 5 6You can set different vibration effects for your device, for example, you can set vibration effects with different intensities and durations for buttons on the device. 7 8For details about the APIs, see [Vibrator](../../reference/apis-sensor-service-kit/js-apis-vibrator.md). 9 10 11## Available APIs 12 13| Name | Description | 14| ------------------------------------------------------------ | ------------------------------------------------------------ | 15| startVibration(effect: VibrateTime, attribute: VibrateAttribute): Promise<void> | Starts vibration with the specified effect and attribute. This API uses a promise to return the result.| 16| startVibration(effect: VibrateTime, attribute: VibrateAttribute, callback: AsyncCallback<void>): void | Starts vibration with the specified effect and attribute. This API uses an asynchronous callback to return the result.| 17| stopVibration(): Promise<void> | Stops vibration in all modes. This API uses a promise to return the result. | 18| stopVibration(callback: AsyncCallback<void>): void | Stops vibration in all modes. This API uses an asynchronous callback to return the result. | 19 20 21## How to Develop 22 231. 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). 24 252. Configure the vibrator to vibrate with the specified duration and attribute. 26 27 ```ts 28 import { vibrator } from '@kit.SensorServiceKit'; 29 import { BusinessError } from '@kit.BasicServicesKit'; 30 31 try { 32 // Start vibration. 33 vibrator.startVibration({ 34 type: 'time', 35 duration: 1000, 36 }, { 37 id: 0, 38 usage: 'alarm' 39 }, (error: BusinessError) => { 40 if (error) { 41 console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); 42 return; 43 } 44 console.info('Succeed in starting vibration'); 45 }); 46 } catch (err) { 47 let e: BusinessError = err as BusinessError; 48 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 49 } 50 ``` 51 523. Stop vibration. 53 54 ```ts 55 import { vibrator } from '@kit.SensorServiceKit'; 56 import { BusinessError } from '@kit.BasicServicesKit'; 57 58 try { 59 // Stop vibration in all modes. 60 vibrator.stopVibration((error: BusinessError) => { 61 if (error) { 62 console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); 63 return; 64 } 65 console.info('Succeed in stopping vibration'); 66 }) 67 } catch (error) { 68 let e: BusinessError = error as BusinessError; 69 console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); 70 } 71 ``` 72