提交 52cf4105 编写于 作者: W wuzhihuitmac

add interface of isSupportEffect&startVibration

Signed-off-by: Nwuzhihuitmac <wuzhihui4@huawei.com>
Change-Id: Ib92b323f642cd0e7b02dc9dd58924a087b35529b
上级 e293a181
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
```js ```js
import vibrator from '@ohos.vibrator'; import vibrator from '@ohos.vibrator';
try { try {
vibrator.startVibration({ vibrator.startVibration({ // 使用startVibration需要添加ohos.permission.VIBRATE权限
type: 'time', type: 'time',
duration: 1000, duration: 1000,
}, { }, {
...@@ -54,7 +54,7 @@ ...@@ -54,7 +54,7 @@
```js ```js
import vibrator from '@ohos.vibrator'; import vibrator from '@ohos.vibrator';
try { try {
// 按照VIBRATOR_STOP_MODE_TIME模式停止振动 // 按照VIBRATOR_STOP_MODE_TIME模式停止振动, 使用stopVibration需要添加ohos.permission.VIBRATE权限
vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME, function (error) { vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME, 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);
...@@ -71,7 +71,21 @@ ...@@ -71,7 +71,21 @@
```js ```js
import vibrator from '@ohos.vibrator'; import vibrator from '@ohos.vibrator';
// 使用startVibration、stopVibration需要添加ohos.permission.VIBRATE权限
try { try {
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.');
});
// 停止所有模式的马达振动 // 停止所有模式的马达振动
vibrator.stopVibration(function (error) { vibrator.stopVibration(function (error) {
if (error) { if (error) {
...@@ -91,30 +105,34 @@ ...@@ -91,30 +105,34 @@
import vibrator from '@ohos.vibrator'; import vibrator from '@ohos.vibrator';
try { try {
// 查询是否支持'haptic.clock.timer' // 查询是否支持'haptic.clock.timer'
vibrator.isSupportEffect('haptic.clock.timer', function (state) { vibrator.isSupportEffect('haptic.clock.timer', function (err, state) {
if (err) {
console.error('isSupportEffect failed, error:' + JSON.stringify(err));
return;
}
console.log('The effectId is ' + (state ? 'supported' : 'unsupported')); console.log('The effectId is ' + (state ? 'supported' : 'unsupported'));
if (state) { if (state) {
try { try {
vibrator.startVibration({ vibrator.startVibration({ // 使用startVibration需要添加ohos.permission.VIBRATE权限
type: 'preset', type: 'preset',
effectId: 'haptic.clock.timer', effectId: 'haptic.clock.timer',
count: 1, count: 1,
}, { }, {
usage: 'unknown' usage: 'unknown'
}, (error) => { }, (error) => {
if(error) { if(error) {
console.log('haptic.clock.timer vibrator error'); console.error('haptic.clock.timer vibrator error:' + JSON.stringify(error));
} else { } else {
console.log('haptic.clock.timer vibrator success'); console.log('haptic.clock.timer vibrator success');
} }
}); });
} catch (error) { } catch (error) {
console.error('errCode: ' + error.code + ' ,msg: ' + error.message); console.error('Exception in, error:' + JSON.stringify(error));
} }
} }
}) })
} catch (error) { } catch (error) {
console.error('exception in, error:' + JSON.stringify(error)); console.error('Exception in, error:' + JSON.stringify(error));
} }
``` ```
......
...@@ -42,6 +42,7 @@ startVibration(effect: VibrateEffect, attribute: VibrateAttribute, callback: Asy ...@@ -42,6 +42,7 @@ startVibration(effect: VibrateEffect, attribute: VibrateAttribute, callback: Asy
示例: 示例:
```js ```js
import vibrator from '@ohos.vibrator';
try { try {
vibrator.startVibration({ vibrator.startVibration({
type: 'time', type: 'time',
...@@ -95,6 +96,7 @@ startVibration(effect: VibrateEffect, attribute: VibrateAttribute): Promise&lt;v ...@@ -95,6 +96,7 @@ startVibration(effect: VibrateEffect, attribute: VibrateAttribute): Promise&lt;v
**示例:** **示例:**
```js ```js
import vibrator from '@ohos.vibrator';
try { try {
vibrator.startVibration({ vibrator.startVibration({
type: 'time', type: 'time',
...@@ -132,6 +134,7 @@ stopVibration(stopMode: VibratorStopMode, callback: AsyncCallback&lt;void&gt;): ...@@ -132,6 +134,7 @@ stopVibration(stopMode: VibratorStopMode, callback: AsyncCallback&lt;void&gt;):
**示例:** **示例:**
```js ```js
import vibrator from '@ohos.vibrator';
try { try {
// 按照固定时长振动 // 按照固定时长振动
vibrator.startVibration({ vibrator.startVibration({
...@@ -190,6 +193,7 @@ stopVibration(stopMode: VibratorStopMode): Promise&lt;void&gt; ...@@ -190,6 +193,7 @@ stopVibration(stopMode: VibratorStopMode): Promise&lt;void&gt;
**示例:** **示例:**
```js ```js
import vibrator from '@ohos.vibrator';
try { try {
// 按照固定时长振动 // 按照固定时长振动
vibrator.startVibration({ vibrator.startVibration({
...@@ -238,6 +242,7 @@ stopVibration(callback: AsyncCallback&lt;void&gt;): void ...@@ -238,6 +242,7 @@ stopVibration(callback: AsyncCallback&lt;void&gt;): void
**示例:** **示例:**
```js ```js
import vibrator from '@ohos.vibrator';
try { try {
// 按照固定时长振动 // 按照固定时长振动
vibrator.startVibration({ vibrator.startVibration({
...@@ -290,6 +295,7 @@ stopVibration(): Promise&lt;void&gt; ...@@ -290,6 +295,7 @@ stopVibration(): Promise&lt;void&gt;
**示例:** **示例:**
```js ```js
import vibrator from '@ohos.vibrator';
try { try {
// 按照固定时长振动 // 按照固定时长振动
vibrator.startVibration({ vibrator.startVibration({
...@@ -337,32 +343,37 @@ isSupportEffect(effectId: string, callback: AsyncCallback&lt;boolean&gt;): void ...@@ -337,32 +343,37 @@ isSupportEffect(effectId: string, callback: AsyncCallback&lt;boolean&gt;): void
**示例:** **示例:**
```js ```js
import vibrator from '@ohos.vibrator';
try { try {
// 查询是否支持'haptic.clock.timer' // 查询是否支持'haptic.clock.timer'
vibrator.isSupportEffect('haptic.clock.timer', function (state) { vibrator.isSupportEffect('haptic.clock.timer', function (err, state) {
if (err) {
console.error('isSupportEffect failed, error:' + JSON.stringify(err));
return;
}
console.log('The effectId is ' + (state ? 'supported' : 'unsupported')); console.log('The effectId is ' + (state ? 'supported' : 'unsupported'));
if (state) { if (state) {
try { try {
vibrator.startVibration({ vibrator.startVibration({ // 使用startVibration需要ohos.permission.VIBRATE权限
type: 'preset', type: 'preset',
effectId: 'haptic.clock.timer', effectId: 'haptic.clock.timer',
count: 1, count: 1,
}, { }, {
usage: 'unknown' usage: 'unknown'
}, (error) => { }, (error) => {
if(error) { if(error) {
console.log('haptic.clock.timer vibrator error'); console.error('haptic.clock.timer vibrator error:' + JSON.stringify(error));
} else { } else {
console.log('haptic.clock.timer vibrator success'); console.log('haptic.clock.timer vibrator success');
} }
}); });
} catch (error) { } catch (error) {
console.error('errCode: ' + error.code + ' ,msg: ' + error.message); console.error('Exception in, error:' + JSON.stringify(error));
} }
} }
}) })
} catch (error) { } catch (error) {
console.error('exception in, error:' + JSON.stringify(error)); console.error('Exception in, error:' + JSON.stringify(error));
} }
``` ```
...@@ -389,31 +400,36 @@ isSupportEffect(effectId: string): Promise&lt;boolean&gt; ...@@ -389,31 +400,36 @@ isSupportEffect(effectId: string): Promise&lt;boolean&gt;
**示例:** **示例:**
```js ```js
import vibrator from '@ohos.vibrator';
try { try {
vibrator.isSupportEffect('haptic.clock.timer').then((state) => { vibrator.isSupportEffect('haptic.clock.timer').then((err, state) => {
if (err) {
console.error('isSupportEffect failed. Error msg:' + JSON.stringify(err));
return;
}
console.log('The effectId is ' + (state ? 'supported' : 'unsupported')); console.log('The effectId is ' + (state ? 'supported' : 'unsupported'));
if (state) { if (state) {
try { try {
vibrator.startVibration({ vibrator.startVibration({ // 使用startVibration需要ohos.permission.VIBRATE权限
type: 'preset', type: 'preset',
effectId: 'haptic.clock.timer', effectId: 'haptic.clock.timer',
count: 1, count: 1,
}, { }, {
usage: 'unknown' usage: 'unknown'
}).then(()=>{ }).then(()=>{
console.log('Promise returned to indicate a successful vibration'); console.log('Promise returned to indicate a successful vibration');
}).catch((error)=>{ }).catch((error)=>{
console.error('error.code' + error.code + 'error.message' + error.message); console.error('Promise returned to indicate a failed vibration:' + JSON.stringify(error));
}); });
} catch (error) { } catch (error) {
console.error('exception in, error:' + JSON.stringify(error)); console.error('exception in, error:' + JSON.stringify(error));
} }
} }
}, (error) => { }, (error) => {
console.error('error.code' + error.code + 'error.message' + error.message); console.error('isSupportEffect failed, error:' + JSON.stringify(error));
}) })
} catch (error) { } catch (error) {
console.error('exception in, error:' + JSON.stringify(error)); console.error('Exception in, error:' + JSON.stringify(error));
} }
``` ```
......
# 泛Sensor子系统Miscdevice Changelog
## cl.vibrator.isSupportEffect接口新增
新增isSupportEffect接口。
**变更影响**
基于OpenHarmony4.0.5.2及之后的SDK版本开发的应用,可使用isSupportEffect接口查询传入effectId是否支持;使用stopVibration接口停止所有类型的振动的。
**关键接口/组件变更**
@ohos.vibrator.d.ts中新增isSupportEffect接口。
| 模块名 | 类名 | 方法/属性/枚举/常量 | 变更类型 |
| -- | -- | -- | -- |
| @ohos.vibrator.d.ts | vibrator | isSupportEffect(effectId: string, callback: AsyncCallback&lt;boolean&gt;): void | 新增 |
| @ohos.vibrator.d.ts | vibrator | isSupportEffect(effectId: string): Promise&lt;boolean&gt; | 新增 |
**适配指导**<br>
通过调用isSupportEffect接口查询是否支持传入的参数effectId。
```ts
import vibrator from '@ohos.vibrator';
try {
// 查询是否支持'haptic.clock.timer'
vibrator.isSupportEffect('haptic.clock.timer', function (err, state) {
if (err) {
console.error('isSupportEffect failed, error:' + JSON.stringify(err));
return;
}
console.log('The effectId is ' + (state ? 'supported' : 'unsupported'));
if (state) {
try {
vibrator.startVibration({ // 使用startVibration需要添加ohos.permission.VIBRATE权限
type: 'preset',
effectId: 'haptic.clock.timer',
count: 1,
}, {
usage: 'unknown'
}, (error) => {
if(error) {
console.error('haptic.clock.timer vibrator error:' + JSON.stringify(error));
} else {
console.log('haptic.clock.timer vibrator success');
}
});
} catch (error) {
console.error('Exception in, error:' + JSON.stringify(error));
}
}
})
} catch (error) {
console.error('Exception in, error:' + JSON.stringify(error));
}
```
## cl.vibrator.stopVibration接口新增
新增stopVibration接口。
**变更影响**
基于OpenHarmony4.0.5.2及之后的SDK版本开发的应用,可使用stopVibration接口停止所有类型的振动的。
**关键接口/组件变更**
@ohos.vibrator.d.ts中新增stopVibration接口。
| 模块名 | 类名 | 方法/属性/枚举/常量 | 变更类型 |
| ------------------- | -------- | -------------------------------------------------------- | -------- |
| @ohos.vibrator.d.ts | vibrator | stopVibration(callback: AsyncCallback&lt;void&gt;): void | 新增 |
| @ohos.vibrator.d.ts | vibrator | stopVibration(): Promise&lt;void&gt; | 新增 |
**适配指导**<br>
通过调用isSupportEffect接口查询是否支持传入的参数effectId。
```ts
import vibrator from '@ohos.vibrator';
try {
// 查询是否支持'haptic.clock.timer'
vibrator.isSupportEffect('haptic.clock.timer', function (err, state) {
if (err) {
console.error('isSupportEffect failed, error:' + JSON.stringify(err));
return;
}
console.log('The effectId is ' + (state ? 'supported' : 'unsupported'));
if (state) {
try {
vibrator.startVibration({ // 使用startVibration需要添加ohos.permission.VIBRATE权限
type: 'preset',
effectId: 'haptic.clock.timer',
count: 1,
}, {
usage: 'unknown'
}, (error) => {
if(error) {
console.error('haptic.clock.timer vibrator error:' + JSON.stringify(error));
} else {
console.log('haptic.clock.timer vibrator success');
}
});
} catch (error) {
console.error('Exception in, error:' + JSON.stringify(error));
}
}
})
} catch (error) {
console.error('Exception in, error:' + JSON.stringify(error));
}
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册