未验证 提交 103f27d8 编写于 作者: O openharmony_ci 提交者: Gitee

!1482 Done! 1383 修改sensor示例demo

Merge pull request !1482 from wusongqing/OpenHarmony-3.1-Beta
此差异已折叠。
# Vibrator
> ![icon-note.gif](public_sys-resources/icon-note.gif) **Note:**
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import
```
import vibrate from '@ohos.vibrator';
```
## Required Permissions
ohos.permission.VIBRATE
## vibrator.vibrate
vibrate(duration: number): Promise<void>
Triggers vibration with a specific duration. This method uses a promise to return the execution result.
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| duration | number | Yes| Vibration duration.|
- Return value
| Type| Description|
| -------- | -------- |
| Promise<void> | Promise used to indicate whether the vibration is triggered successfully.|
- Example
```
vibrator.vibrate(1000).then(()=>{
console.log("Promise returned to indicate a successful vibration.");
}, (error)=>{
console.log("error.code"+error.code+"error.message"+error.message);
});
```
## vibrator.vibrate
vibrate(duration: number, callback?: AsyncCallback<void>): void
Triggers vibration with a specific duration. This method uses a callback to return the execution result.
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| duration | number | Yes| Vibration duration.|
| callback | AsyncCallback<void> | No| Callback used to indicate whether the vibration is triggered successfully.|
- Example
```
vibrator.vibrate(1000,function(error){
if(error){
console.log("error.code"+error.code+"error.message"+error.message);
}else{
console.log("Callback returned to indicate a successful vibration.");
}
})
```
## vibrator.vibrate
vibrate(effectId: EffectId): Promise<void>
Triggers vibration with a specific effect. This method uses a promise to return the execution result.
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| effectId | [EffectId](#effectid) | Yes| String that indicates the vibration effect.|
- Return value
| Type| Description|
| -------- | -------- |
| Promise<void> | Promise used to indicate whether the vibration is triggered successfully.|
- Example
```
vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER).then(()=>{
console.log("Promise returned to indicate a successful vibration.");
}, (error)=>{
console.log("error.code"+error.code+"error.message"+error.message);
});
```
## vibrator.vibrate
vibrate(effectId: EffectId, callback?: AsyncCallback<void>): void
Triggers vibration with a specific effect. This method uses a callback to return the execution result.
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| effectId | [EffectId](#effectid) | Yes| String that indicates the vibration effect.|
| callback | AsyncCallback<void> | No| Callback used to indicate whether the vibration is triggered successfully.|
- Example
```
vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, function(error){
if(error){
console.log("error.code"+error.code+"error.message"+error.message);
}else{
console.log("Callback returned to indicate a successful vibration.");
}
})
```
## vibrator.stop
stop(stopMode: VibratorStopMode): Promise<void>
Stops the vibration based on the specified **stopMode**. This method uses a promise to return the execution result. If the specified **stopMode** is different from the mode used to trigger the vibration, this method fails to be called.
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| stopMode | [VibratorStopMode](#vibratorstopmode) | Yes| Vibration mode to stop.|
- Return value
| Type| Description|
| -------- | -------- |
| Promise<void> | Promise used to indicate whether the vibration is stopped successfully.|
- Example
```
vibrator.vibrate(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(()=>{
console.log("Promise returned to indicate a successful vibration.");
}, (error)=>{
console.log("error.code"+error.code+"error.message"+error.message);
});
```
## vibrator.stop
stop(stopMode: VibratorStopMode, callback?: AsyncCallback<void>): void;
Stops the vibration based on the specified **stopMode**. This method uses an asynchronous callback to return the execution result. If the specified **stopMode** is different from the mode used to trigger the vibration, this method fails to be called.
- Parameters
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| stopMode | [VibratorStopMode](#vibratorstopmode) | Yes| Vibration mode to stop.|
| callback | AsyncCallback<void> | No| Callback used to indicate whether the vibration is stopped successfully.|
- Example
```
vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, function(error){
if(error){
console.log("error.code"+error.code+"error.message"+error.message);
}else{
Console.log("Callback returned to indicate a successful stop.");
}
})
```
## EffectId
Describes the vibration effect.
| Name| Default Value| Description|
| -------- | -------- | -------- |
| EFFECT_CLOCK_TIMER | "haptic.clock.timer" | Vibration effect of the vibrator when a user adjusts the timer.|
## VibratorStopMode
Describes the vibration mode to stop.
| Name| Default Value| Description|
| -------- | -------- | -------- |
| VIBRATOR_STOP_MODE_TIME | "time" | The vibration to stop is in **duration** mode. This vibration is triggered with the parameter **duration** of the **number** type.|
| VIBRATOR_STOP_MODE_PRESET | "preset" | The vibration to stop is in **EffectId** mode. This vibration is triggered with the parameter **effectId** of the **EffectId** type.|
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册