以下代码在手机上正常,在需要的场景可以触发震动马达,但是在平板上不生效。
经测试,平版是带震动马达的,比如进入任务管理器(底部上滑停留)时有震动反馈。
示例代码:
//ZZ.ets
import { BusinessError } from '@kit.BasicServicesKit';
import { vibrator } from '@kit.SensorServiceKit';
class ZZ{
zz(duration:number){
try {
// 触发马达振动
vibrator.startVibration({
type: 'time',
duration: duration,
}, {
id: 0,
usage: 'touch'
}, (error: BusinessError) => {
if (error) {
console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
return;
}
console.info('Succeed in starting vibration');
});
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
}
}
}
let zz:ZZ = new ZZ
export default zz as ZZ
可以先使用vibrator.isSupportEffect检查是否支持震动,返回结果是false就是不支持。相关接口的使用可参考文档:
https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-vibrator-V5\#vibratorissupporteffect10