提交 4e294b56 编写于 作者: H h00514358

update docs

Signed-off-by: Nh00514358 <huiyuehong@huawei.com>
上级 81f22a2f
# 振动 # 振动
vibrator模块提供控制马达振动的能力,如通过接口控制马达启动马达振动,停止马达振动等 vibrator模块提供控制马达振动启、停的能力
> **说明:** > **说明:**
> >
...@@ -17,7 +17,7 @@ import vibrator from '@ohos.vibrator'; ...@@ -17,7 +17,7 @@ import vibrator from '@ohos.vibrator';
startVibration(effect: VibrateEffect, attribute: VibrateAttribute, callback: AsyncCallback&lt;void&gt;): void startVibration(effect: VibrateEffect, attribute: VibrateAttribute, callback: AsyncCallback&lt;void&gt;): void
按照指定振动效果和振动属性触发马达振动。 根据指定振动效果和振动属性触发马达振动。
**需要权限**:ohos.permission.VIBRATE **需要权限**:ohos.permission.VIBRATE
...@@ -29,7 +29,7 @@ startVibration(effect: VibrateEffect, attribute: VibrateAttribute, callback: Asy ...@@ -29,7 +29,7 @@ startVibration(effect: VibrateEffect, attribute: VibrateAttribute, callback: Asy
| --------- | -------------------------------------- | ---- | :--------------------------------------------------------- | | --------- | -------------------------------------- | ---- | :--------------------------------------------------------- |
| effect | [VibrateEffect](#vibrateeffect9) | 是 | 马达振动效果。 | | effect | [VibrateEffect](#vibrateeffect9) | 是 | 马达振动效果。 |
| attribute | [VibrateAttribute](#vibrateattribute9) | 是 | 马达振动属性。 | | attribute | [VibrateAttribute](#vibrateattribute9) | 是 | 马达振动属性。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数当马达振动成功,err为undefined,否则为错误对象。 | | callback | AsyncCallback&lt;void&gt; | 是 | 回调函数当马达振动成功,err为undefined,否则为错误对象。 |
**错误码** **错误码**
...@@ -44,20 +44,20 @@ startVibration(effect: VibrateEffect, attribute: VibrateAttribute, callback: Asy ...@@ -44,20 +44,20 @@ startVibration(effect: VibrateEffect, attribute: VibrateAttribute, callback: Asy
```js ```js
try { try {
vibrator.startVibration({ vibrator.startVibration({
type:'time', type: 'time',
duration:1000, duration: 1000,
},{ }, {
id:0, id: 0,
usage: 'alarm' usage: 'alarm'
}, (error)=>{ }, (error) => {
if(error){ if (error) {
console.log('vibrate fail, error.code: ' + error.code + 'error.message: ', + error.message); console.error('vibrate fail, error.code: ' + error.code + 'error.message: ', + error.message);
}else{ return;
console.log('Callback returned to indicate a successful vibration.');
} }
console.log('Callback returned to indicate a successful vibration.');
}); });
} catch(err) { } catch (err) {
console.info('errCode: ' + err.code + ' ,msg: ' + err.message); console.error('errCode: ' + err.code + ' ,msg: ' + err.message);
} }
``` ```
...@@ -65,7 +65,7 @@ try { ...@@ -65,7 +65,7 @@ try {
startVibration(effect: VibrateEffect, attribute: VibrateAttribute): Promise&lt;void&gt; startVibration(effect: VibrateEffect, attribute: VibrateAttribute): Promise&lt;void&gt;
按照指定振动效果和振动属性触发马达振动。 根据指定振动效果和振动属性触发马达振动。
**需要权限**:ohos.permission.VIBRATE **需要权限**:ohos.permission.VIBRATE
...@@ -82,7 +82,7 @@ startVibration(effect: VibrateEffect, attribute: VibrateAttribute): Promise&lt;v ...@@ -82,7 +82,7 @@ startVibration(effect: VibrateEffect, attribute: VibrateAttribute): Promise&lt;v
| 类型 | 说明 | | 类型 | 说明 |
| ------------------- | -------------------------------------- | | ------------------- | -------------------------------------- |
| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。 | | Promise&lt;void&gt; | Promise对象。 |
**错误码** **错误码**
...@@ -97,18 +97,18 @@ startVibration(effect: VibrateEffect, attribute: VibrateAttribute): Promise&lt;v ...@@ -97,18 +97,18 @@ startVibration(effect: VibrateEffect, attribute: VibrateAttribute): Promise&lt;v
```js ```js
try { try {
vibrator.startVibration({ vibrator.startVibration({
type: 'time', type: 'time',
duration: 1000 duration: 1000
}, { }, {
id: 0, id: 0,
usage: 'alarm' usage: 'alarm'
}).then(()=>{ }).then(() => {
console.log('Promise returned to indicate a successful vibration'); console.log('Promise returned to indicate a successful vibration');
}).catch((error)=>{ }, (error) => {
console.log('error.code' + error.code + 'error.message' + error.message); console.error('error.code' + error.code + 'error.message' + error.message);
}) });
} catch(err) { } catch (err) {
console.info('errCode: ' + err.code + ' ,msg: ' + err.message); console.error('errCode: ' + err.code + ' ,msg: ' + err.message);
} }
``` ```
...@@ -116,7 +116,7 @@ try { ...@@ -116,7 +116,7 @@ try {
stopVibration(stopMode: VibratorStopMode, callback: AsyncCallback&lt;void&gt;): void stopVibration(stopMode: VibratorStopMode, callback: AsyncCallback&lt;void&gt;): void
按照要停止指定的振动模式来停止马达的振动。如果要停止的振动模式与触发马达振动时的模式不相同,则调用本接口会失败 按照指定模式停止马达的振动
**需要权限**:ohos.permission.VIBRATE **需要权限**:ohos.permission.VIBRATE
...@@ -126,22 +126,42 @@ stopVibration(stopMode: VibratorStopMode, callback: AsyncCallback&lt;void&gt;): ...@@ -126,22 +126,42 @@ stopVibration(stopMode: VibratorStopMode, callback: AsyncCallback&lt;void&gt;):
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------- | ---- | ------------------------------------------------------------ | | -------- | ------------------------------------- | ---- | ------------------------------------------------------------ |
| stopMode | [VibratorStopMode](#vibratorstopmode) | 是 | 马达停止指定的振动模式。 | | stopMode | [VibratorStopMode](#vibratorstopmode) | 是 | 指定的停止振动模式。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。当马达停止振动成功,err为undefined,否则为错误对象。 | | callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。当马达停止振动成功,err为undefined,否则为错误对象。 |
**示例:** **示例:**
```js ```js
try { try {
vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, function(error){ // 按照固定时长振动
if(error){ vibrator.startVibration({
type: 'time',
duration: 1000,
}, {
id: 0,
usage: 'alarm'
}, (error) => {
if (error) {
console.error('vibrate fail, error.code: ' + error.code + 'error.message: ', + error.message);
return;
}
console.log('Callback returned to indicate a successful vibration.');
});
} catch (err) {
console.error('errCode: ' + err.code + ' ,msg: ' + err.message);
}
try {
// 按照VIBRATOR_STOP_MODE_TIME模式停止振动
vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME, function (error) {
if (error) {
console.log('error.code' + error.code + 'error.message' + error.message); console.log('error.code' + error.code + 'error.message' + error.message);
}else{ return;
console.log('Callback returned to indicate successful.');
} }
console.log('Callback returned to indicate successful.');
}) })
} catch(err) { } catch (err) {
console.info('errCode: ' + err.code + ' ,msg: ' + err.message); console.info('errCode: ' + err.code + ' ,msg: ' + err.message);
} }
``` ```
...@@ -149,7 +169,7 @@ try { ...@@ -149,7 +169,7 @@ try {
stopVibration(stopMode: VibratorStopMode): Promise&lt;void&gt; stopVibration(stopMode: VibratorStopMode): Promise&lt;void&gt;
按照要停止指定的振动模式来停止马达的振动。如果要停止的振动模式与触发马达振动时的模式不相同,则调用本接口会失败 按照指定模式停止马达的振动
**需要权限**:ohos.permission.VIBRATE **需要权限**:ohos.permission.VIBRATE
...@@ -165,43 +185,61 @@ stopVibration(stopMode: VibratorStopMode): Promise&lt;void&gt; ...@@ -165,43 +185,61 @@ stopVibration(stopMode: VibratorStopMode): Promise&lt;void&gt;
| 类型 | 说明 | | 类型 | 说明 |
| ------------------- | -------------------------------------- | | ------------------- | -------------------------------------- |
| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。 | | Promise&lt;void&gt; | Promise对象。 |
**示例:** **示例:**
```js ```js
try { try {
vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(()=>{ // 按照固定时长振动
console.log('Promise returned to indicate a successful vibration.'); vibrator.startVibration({
}, (error)=>{ type: 'time',
duration: 1000
}, {
id: 0,
usage: 'alarm'
}).then(() => {
console.log('Promise returned to indicate a successful vibration');
}, (error) => {
console.error('error.code' + error.code + 'error.message' + error.message);
});
} catch (err) {
console.error('errCode: ' + err.code + ' ,msg: ' + err.message);
}
try {
// 按照VIBRATOR_STOP_MODE_TIME模式停止振动
vibrator.stopVibration(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); console.log('error.code' + error.code + 'error.message' + error.message);
}); });
} catch(err) { } catch (err) {
console.info('errCode: ' + err.code + ' ,msg: ' + err.message); console.info('errCode: ' + err.code + ' ,msg: ' + err.message);
} }
``` ```
## EffectId ## EffectId
马达振动效果的字符串 预置的振动效果
**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.MiscDevice **系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.MiscDevice
| 名称 | 默认值 | 说明 | | 名称 | 默认值 | 说明 |
| ------------------ | -------------------- | ------------------ | | ------------------ | -------------------- | ------------------ |
| EFFECT_CLOCK_TIMER | "haptic.clock.timer" | 预置的振动效果ID。 | | EFFECT_CLOCK_TIMER | "haptic.clock.timer" | 描述用户调整计时器时的振动效果。 |
## VibratorStopMode ## VibratorStopMode
马达要停止指定的振动模式。 停止的振动模式。
**系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.MiscDevice **系统能力**:以下各项对应的系统能力均为SystemCapability.Sensors.MiscDevice
| 名称 | 默认值 | 说明 | | 名称 | 默认值 | 说明 |
| ------------------------- | -------- | ------------------------------------------------------------ | | ------------------------- | -------- | ------------------------------------------------------------ |
| VIBRATOR_STOP_MODE_TIME | "time" | 停止模式为duration模式的振动。即触发振动时参数类型为number,参数本身为振动持续时间的触发方式。 | | VIBRATOR_STOP_MODE_TIME | "time" | 停止模式为duration模式的振动。 |
| VIBRATOR_STOP_MODE_PRESET | "preset" | 停止模式为预置EffectId的振动。即触发振动时参数类型为EffectId,参数本身为马达振动效果的字符串的触发方式。 | | VIBRATOR_STOP_MODE_PRESET | "preset" | 停止模式为预置EffectId的振动。 |
## VibrateEffect<sup>9+</sup> ## VibrateEffect<sup>9+</sup>
...@@ -223,7 +261,7 @@ try { ...@@ -223,7 +261,7 @@ try {
| 名称 | 默认值 | 说明 | | 名称 | 默认值 | 说明 |
| -------- | ------ | ------------------------------ | | -------- | ------ | ------------------------------ |
| type | "time" | 按照指定持续时间触发马达振动。 | | type | "time" | 按照指定持续时间触发马达振动。 |
| duration | - | 马达振动时长, 单位ms。 | | duration | - | 马达持续振动时长, 单位ms。 |
## VibratePreset<sup>9+</sup> ## VibratePreset<sup>9+</sup>
...@@ -246,7 +284,7 @@ try { ...@@ -246,7 +284,7 @@ try {
| 名称 | 默认值 | 说明 | | 名称 | 默认值 | 说明 |
| ----- | ------ | -------------- | | ----- | ------ | -------------- |
| id | 0 | 振动器id。 | | id | 0 | 振动器id。 |
| usage | - | 马达振动场景。 | | usage | - | 马达振动的使用场景。 |
## Usage<sup>9+</sup> ## Usage<sup>9+</sup>
...@@ -257,14 +295,14 @@ try { ...@@ -257,14 +295,14 @@ try {
| 名称 | 类型 | 说明 | | 名称 | 类型 | 说明 |
| ---------------- | ------ | ------------------------------ | | ---------------- | ------ | ------------------------------ |
| unknown | string | 没有明确使用场景,最低优先级。 | | unknown | string | 没有明确使用场景,最低优先级。 |
| alarm | string | 用于警报振动的场景。 | | alarm | string | 用于警报场景。 |
| ring | string | 用于铃声振动的场景。 | | ring | string | 用于铃声场景。 |
| notification | string | 用于通知振动的场景。 | | notification | string | 用于通知场景。 |
| communication | string | 用于通信振动的场景。 | | communication | string | 用于通信场景。 |
| touch | string | 用于触摸振动的场景。 | | touch | string | 用于触摸场景。 |
| media | string | 用于多媒体振动的场景。 | | media | string | 用于多媒体场景。 |
| physicalFeedback | string | 用于物理反馈振动的场景。 | | physicalFeedback | string | 用于物理反馈场景。 |
| simulateReality | string | 用于模拟现实振动的场景。 | | simulateReality | string | 用于模拟现实场景。 |
## vibrator.vibrate<sup>(deprecated)</sup> ## vibrator.vibrate<sup>(deprecated)</sup>
...@@ -288,14 +326,14 @@ vibrate(duration: number): Promise&lt;void&gt; ...@@ -288,14 +326,14 @@ vibrate(duration: number): Promise&lt;void&gt;
| 类型 | 说明 | | 类型 | 说明 |
| ------------------- | -------------------------------------- | | ------------------- | -------------------------------------- |
| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。 | | Promise&lt;void&gt; | Promise对象。 |
**示例:** **示例:**
```js ```js
vibrator.vibrate(1000).then(()=>{ vibrator.vibrate(1000).then(() => {
console.log('Promise returned to indicate a successful vibration.'); console.log('Promise returned to indicate a successful vibration.');
}, (error)=>{ }, (error) => {
console.log('error.code' + error.code + 'error.message' + error.message); console.log('error.code' + error.code + 'error.message' + error.message);
}); });
``` ```
...@@ -322,10 +360,10 @@ vibrate(duration: number, callback?: AsyncCallback&lt;void&gt;): void ...@@ -322,10 +360,10 @@ vibrate(duration: number, callback?: AsyncCallback&lt;void&gt;): void
**示例:** **示例:**
```js ```js
vibrator.vibrate(1000,function(error){ vibrator.vibrate(1000, function (error) {
if(error){ if (error) {
console.log('error.code' + error.code + 'error.message' + error.message); console.log('error.code' + error.code + 'error.message' + error.message);
}else{ } else {
console.log('Callback returned to indicate a successful vibration.'); console.log('Callback returned to indicate a successful vibration.');
} }
}) })
...@@ -354,14 +392,14 @@ vibrate(effectId: EffectId): Promise&lt;void&gt; ...@@ -354,14 +392,14 @@ vibrate(effectId: EffectId): Promise&lt;void&gt;
| 类型 | 说明 | | 类型 | 说明 |
| ------------------- | -------------------------------------- | | ------------------- | -------------------------------------- |
| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。 | | Promise&lt;void&gt; | Promise对象。 |
**示例:** **示例:**
```js ```js
vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER).then(()=>{ vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER).then(() => {
console.log('Promise returned to indicate a successful vibration.'); console.log('Promise returned to indicate a successful vibration.');
}, (error)=>{ }, (error) => {
console.log('error.code' + error.code + 'error.message' + error.message); console.log('error.code' + error.code + 'error.message' + error.message);
}); });
``` ```
...@@ -389,10 +427,10 @@ vibrate(effectId: EffectId, callback?: AsyncCallback&lt;void&gt;): void ...@@ -389,10 +427,10 @@ vibrate(effectId: EffectId, callback?: AsyncCallback&lt;void&gt;): void
**示例:** **示例:**
```js ```js
vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, function(error){ vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, function (error) {
if(error){ if (error) {
console.log('error.code' + error.code + 'error.message' + error.message); console.log('error.code' + error.code + 'error.message' + error.message);
}else{ } else {
console.log('Callback returned to indicate a successful vibration.'); console.log('Callback returned to indicate a successful vibration.');
} }
}) })
...@@ -402,7 +440,7 @@ vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, function(error){ ...@@ -402,7 +440,7 @@ vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, function(error){
stop(stopMode: VibratorStopMode): Promise&lt;void&gt; stop(stopMode: VibratorStopMode): Promise&lt;void&gt;
按照要停止指定的振动模式来停止马达的振动。如果要停止的振动模式与触发马达振动时的模式不相同,则调用本接口会失败 按照指定模式停止马达的振动
从API version 9 开始不再维护,建议使用 [vibrator.stopVibration](#vibratorstopvibration9-1) 代替。 从API version 9 开始不再维护,建议使用 [vibrator.stopVibration](#vibratorstopvibration9-1) 代替。
...@@ -420,14 +458,23 @@ stop(stopMode: VibratorStopMode): Promise&lt;void&gt; ...@@ -420,14 +458,23 @@ stop(stopMode: VibratorStopMode): Promise&lt;void&gt;
| 类型 | 说明 | | 类型 | 说明 |
| ------------------- | -------------------------------------- | | ------------------- | -------------------------------------- |
| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。 | | Promise&lt;void&gt; | Promise对象。 |
**示例:** **示例:**
```js ```js
vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(()=>{ // 按照effectId类型启动振动
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_MODE_PRESET模式停止振动
vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(() => {
console.log('Promise returned to indicate a successful vibration.'); console.log('Promise returned to indicate a successful vibration.');
}, (error)=>{ }, (error) => {
console.log('error.code' + error.code + 'error.message' + error.message); console.log('error.code' + error.code + 'error.message' + error.message);
}); });
``` ```
...@@ -437,7 +484,7 @@ vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(()=>{ ...@@ -437,7 +484,7 @@ vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(()=>{
stop(stopMode: VibratorStopMode, callback?: AsyncCallback&lt;void&gt;): void stop(stopMode: VibratorStopMode, callback?: AsyncCallback&lt;void&gt;): void
按照要停止指定的振动模式来停止马达的振动。如果要停止的振动模式与触发马达振动时的模式不相同,则调用本接口会失败。 按照指定模式停止马达的振动。
从API version 9 开始不再维护,建议使用 [vibrator.stopVibration](#vibratorstopvibration9) 代替。 从API version 9 开始不再维护,建议使用 [vibrator.stopVibration](#vibratorstopvibration9) 代替。
...@@ -455,10 +502,19 @@ stop(stopMode: VibratorStopMode, callback?: AsyncCallback&lt;void&gt;): void ...@@ -455,10 +502,19 @@ stop(stopMode: VibratorStopMode, callback?: AsyncCallback&lt;void&gt;): void
**示例:** **示例:**
```js ```js
vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, function(error){ // 按照effectId类型启动振动
if(error){ 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_MODE_PRESET模式停止振动
vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, function (error) {
if (error) {
console.log('error.code' + error.code + 'error.message' + error.message); console.log('error.code' + error.code + 'error.message' + error.message);
}else{ } else {
console.log('Callback returned to indicate successful.'); console.log('Callback returned to indicate successful.');
} }
}) })
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册