Vibrator Development
When to Use
You can set different vibration effects as needed, for example, customizing the vibration intensity, frequency, and duration for button touches, alarm clocks, and incoming calls.
For details about the APIs, see Vibrator.
Available APIs
Module | API | Description |
---|---|---|
ohos.vibrator | vibrate(duration: number): Promise<void> | Triggers vibration with the specified duration. This API uses a promise to return the result. |
ohos.vibrator | vibrate(duration: number, callback?: AsyncCallback<void>): void | Triggers vibration with the specified duration. This API uses a callback to return the result. |
ohos.vibrator | vibrate(effectId: EffectId): Promise<void> | Triggers vibration with the specified effect. This API uses a promise to return the result. |
ohos.vibrator | vibrate(effectId: EffectId, callback?: AsyncCallback<void>): void | Triggers vibration with the specified effect. This API uses a callback to return the result. |
ohos.vibrator | stop(stopMode: VibratorStopMode): Promise<void> | Stops vibration. This API uses a promise to return the result. |
ohos.vibrator | stop(stopMode: VibratorStopMode, callback?: AsyncCallback<void>): void | Stops vibration. This API uses a callback to return the result. |
How to Develop
-
Before using the vibrator on a device, you must declare the ohos.permission.VIBRATE permission. For details about how to configure a permission, see Declaring Permissions.
-
Trigger the device to vibrate.
import vibrator from "@ohos.vibrator" vibrator.vibrate(1000).then((error) => { if (error) { // The call fails, and error.code and error.message are printed. console.log("Promise return failed.error.code " + error.code + "error.message " + error.message); } else { // The call is successful, and the device starts to vibrate. console.log("Promise returned to indicate a successful vibration.") } })
-
Stop the vibration.
import vibrator from "@ohos.vibrator" vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then((error) => { if (error) { // The call fails, and error.code and error.message are printed. console.log("Promise return failed.error.code " + error.code + "error.message " + error.message); } else { // The call is successful, and the device stops vibrating. console.log("Promise returned to indicate successful."); } })