Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
a8424bab
D
Docs
项目概览
OpenHarmony
/
Docs
接近 2 年 前同步成功
通知
159
Star
292
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
a8424bab
编写于
7月 10, 2023
作者:
O
openharmony_ci
提交者:
Gitee
7月 10, 2023
浏览文件
操作
浏览文件
下载
差异文件
!20595 【泛Sensor】-- 根据规范修改代码示例格式
Merge pull request !20595 from lixiangpeng5/doc-r
上级
31c55724
4be326ea
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
351 addition
and
352 deletion
+351
-352
zh-cn/application-dev/device/vibrator-guidelines.md
zh-cn/application-dev/device/vibrator-guidelines.md
+113
-122
zh-cn/application-dev/reference/apis/js-apis-system-vibrate.md
.../application-dev/reference/apis/js-apis-system-vibrate.md
+4
-4
zh-cn/application-dev/reference/apis/js-apis-vibrator.md
zh-cn/application-dev/reference/apis/js-apis-vibrator.md
+234
-226
未找到文件。
zh-cn/application-dev/device/vibrator-guidelines.md
浏览文件 @
a8424bab
...
@@ -110,171 +110,162 @@ Json文件共包含2个属性。
...
@@ -110,171 +110,162 @@ Json文件共包含2个属性。
2.
根据指定振动效果和振动属性触发马达振动。
2.
根据指定振动效果和振动属性触发马达振动。
```
j
s
```
t
s
import
vibrator
from
'
@ohos.vibrator
'
;
import
vibrator
from
'
@ohos.vibrator
'
;
try
{
try
{
vibrator
.
startVibration
({
// 使用startVibration需要添加ohos.permission.VIBRATE权限
vibrator
.
startVibration
({
// 使用startVibration需要添加ohos.permission.VIBRATE权限
type
:
'
time
'
,
type
:
'
time
'
,
duration
:
1000
,
duration
:
1000
,
},
{
},
{
id
:
0
,
id
:
0
,
usage
:
'
alarm
'
usage
:
'
alarm
'
},
(
error
)
=>
{
},
(
error
)
=>
{
if
(
error
)
{
if
(
error
)
{
console
.
error
(
'
vibrate fail, error.code:
'
+
error
.
code
+
'
error.message:
'
,
+
error
.
message
);
console
.
error
(
`Failed to start vibration. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
return
;
return
;
}
}
console
.
log
(
'
Callback returned to indicate a successful
vibration.
'
);
console
.
info
(
'
Succeed in starting
vibration.
'
);
});
});
}
catch
(
err
)
{
}
catch
(
err
)
{
console
.
error
(
'
errCode:
'
+
err
.
code
+
'
,msg:
'
+
err
.
message
);
console
.
error
(
`An unexpected error occurred. Code:
${
err
.
code
}
, message:
${
err
.
message
}
`
);
}
}
```
```
3.
按照指定模式停止马达的振动。
3.
按照指定模式停止马达的振动。
```
j
s
```
t
s
import
vibrator
from
'
@ohos.vibrator
'
;
import
vibrator
from
'
@ohos.vibrator
'
;
try
{
try
{
// 按照VIBRATOR_STOP_MODE_TIME模式停止振动, 使用stopVibration需要添加ohos.permission.VIBRATE权限
// 按照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
.
error
(
`Failed to stop vibration. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
return
;
return
;
}
}
console
.
log
(
'
Callback returned to indicate successful
.
'
);
console
.
info
(
'
Succeeded in stopping vibration
.
'
);
})
})
}
catch
(
err
)
{
}
catch
(
err
)
{
console
.
info
(
'
errCode:
'
+
err
.
code
+
'
,msg:
'
+
err
.
message
);
console
.
error
(
`An unexpected error occurred. Code:
${
err
.
code
}
, message:
${
err
.
message
}
`
);
}
}
```
```
4.
停止所有模式的马达振动。
4.
停止所有模式的马达振动。
```
j
s
```
t
s
import
vibrator
from
'
@ohos.vibrator
'
;
import
vibrator
from
'
@ohos.vibrator
'
;
// 使用startVibration、stopVibration需要添加ohos.permission.VIBRATE权限
// 使用startVibration、stopVibration需要添加ohos.permission.VIBRATE权限
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
.
error
(
'
vibrate fail, error.code:
'
+
error
.
code
+
'
error.message:
'
,
+
error
.
message
);
console
.
error
(
`Failed to start vibration. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
return
;
return
;
}
}
console
.
log
(
'
Callback returned to indicate a successful vibration.
'
);
console
.
info
(
'
Succeed in starting vibration
'
);
});
});
// 停止所有类型的马达振动
// 停止所有类型的马达振动
vibrator
.
stopVibration
(
function
(
error
)
{
vibrator
.
stopVibration
(
function
(
error
)
{
if
(
error
)
{
if
(
error
)
{
console
.
log
(
'
error.code
'
+
error
.
code
+
'
error.message
'
+
error
.
message
);
console
.
error
(
`Failed to stop vibration. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
return
;
return
;
}
}
console
.
log
(
'
Callback returned to indicate successful.
'
);
console
.
info
(
'
Succeed in stopping vibration
'
);
})
})
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
info
(
'
errCode:
'
+
error
.
code
+
'
,msg:
'
+
error
.
message
);
console
.
error
(
`An unexpected error occurred. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
}
}
```
```
5.
查询是否支持传入的参数effectId。
5.
查询是否支持传入的参数effectId。
```
j
s
```
t
s
import
vibrator
from
'
@ohos.vibrator
'
;
import
vibrator
from
'
@ohos.vibrator
'
;
try
{
try
{
// 查询是否支持'haptic.clock.timer'
// 查询是否支持'haptic.clock.timer'
vibrator
.
isSupportEffect
(
'
haptic.clock.timer
'
,
function
(
err
,
state
)
{
vibrator
.
isSupportEffect
(
'
haptic.clock.timer
'
,
function
(
err
,
state
)
{
if
(
err
)
{
if
(
err
)
{
console
.
error
(
'
isSupportEffect failed, error:
'
+
JSON
.
stringify
(
err
)
);
console
.
error
(
`Failed to query effect. Code:
${
err
.
code
}
, message:
${
err
.
message
}
`
);
return
;
return
;
}
}
console
.
log
(
'
The effectId is
'
+
(
state
?
'
supported
'
:
'
unsupported
'
)
);
console
.
info
(
'
Succeed in querying effect
'
);
if
(
state
)
{
if
(
state
)
{
try
{
try
{
vibrator
.
startVibration
({
// 使用startVibration需要添加ohos.permission.VIBRATE权限
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
.
error
(
'
haptic.clock.timer vibrator error:
'
+
JSON
.
stringify
(
error
)
);
console
.
error
(
`Failed to start vibration. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
}
else
{
}
else
{
console
.
log
(
'
haptic.clock.timer vibrator success
'
);
console
.
info
(
'
Succeed in starting vibration
'
);
}
}
});
});
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
error
(
'
Exception in, error:
'
+
JSON
.
stringify
(
error
)
);
console
.
error
(
`An unexpected error occurred. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
}
}
}
}
})
})
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
error
(
'
Exception in, error:
'
+
JSON
.
stringify
(
error
)
);
console
.
error
(
`An unexpected error occurred. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
}
}
```
```
6.
启动和停止自定义振动
6.
启动和停止自定义振动
```
j
s
```
t
s
import
vibrator
from
'
@ohos.vibrator
'
;
import
vibrator
from
'
@ohos.vibrator
'
;
import
resourceManager
from
'
@ohos.resourceManager
'
;
const
FILE_NAME
=
"
xxx.json
"
;
const
FILE_NAME
=
"
xxx.json
"
;
async
function
openResource
(
fileName
)
{
let
fileDescriptor
=
undefined
;
let
mgr
=
await
resourceManager
.
getResourceManager
();
await
mgr
.
getRawFd
(
fileName
).
then
(
value
=>
{
fileDescriptor
=
{
fd
:
value
.
fd
,
offset
:
value
.
offset
,
length
:
value
.
length
};
console
.
log
(
'
openResource success fileName:
'
+
fileName
);
}).
catch
(
error
=>
{
console
.
log
(
'
openResource err:
'
+
error
);
});
return
fileDescriptor
;
}
async
function
closeResource
(
fileName
)
{
let
mgr
=
await
resourceManager
.
getResourceManager
();
await
mgr
.
closeRawFd
(
fileName
).
then
(()
=>
{
console
.
log
(
'
closeResource success fileName:
'
+
fileName
);
}).
catch
(
error
=>
{
console
.
log
(
'
closeResource err:
'
+
error
);
});
}
// 获取振动文件资源描述符
// 获取振动文件资源描述符
let
rawFd
=
openResource
(
FILE_NAME
);
let
fileDescriptor
=
undefined
;
getContext
().
resourceManager
.
getRawFd
(
FILE_NAME
).
then
(
value
=>
{
fileDescriptor
=
{
fd
:
value
.
fd
,
offset
:
value
.
offset
,
length
:
value
.
length
};
console
.
info
(
'
Succeed in getting resource file descriptor
'
);
}).
catch
(
error
=>
{
console
.
error
(
`Failed to get resource file descriptor. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
});
// 使用startVibration、stopVibration需要添加ohos.permission.VIBRATE权限
// 使用startVibration、stopVibration需要添加ohos.permission.VIBRATE权限
try
{
try
{
// 启动自定义振动
// 启动自定义振动
vibrator
.
startVibration
({
vibrator
.
startVibration
({
type
:
"
file
"
,
type
:
"
file
"
,
hapticFd
:
{
fd
:
rawFd
.
fd
,
offset
:
rawFd
.
offset
,
length
:
rawFd
.
length
}
hapticFd
:
{
fd
:
fileDescriptor
.
fd
,
offset
:
fileDescriptor
.
offset
,
length
:
fileDescriptor
.
length
}
},
{
},
{
usage
:
"
alarm
"
usage
:
"
alarm
"
}).
then
(()
=>
{
}).
then
(()
=>
{
console
.
info
(
'
startVibration success
'
);
console
.
info
(
'
Succeed in starting vibration
'
);
},
(
error
)
=>
{
},
(
error
)
=>
{
console
.
info
(
'
startVibration error
'
);
console
.
error
(
`Failed to start vibration. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
});
});
// 停止所有类型的马达振动
// 停止所有类型的马达振动
vibrator
.
stopVibration
(
function
(
error
)
{
vibrator
.
stopVibration
(
function
(
error
)
{
if
(
error
)
{
if
(
error
)
{
console
.
log
(
'
error.code
'
+
error
.
code
+
'
error.message
'
+
error
.
message
);
console
.
error
(
`Failed to stop vibration. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
return
;
return
;
}
}
console
.
log
(
'
Callback returned to indicate successful.
'
);
console
.
info
(
'
Succeed in stopping vibration
'
);
})
})
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
info
(
'
errCode:
'
+
error
.
code
+
'
,msg:
'
+
error
.
message
);
console
.
error
(
`An unexpected error occurred. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
}
}
// 关闭振动文件资源
// 关闭振动文件资源
closeResource
(
FILE_NAME
);
getContext
().
resourceManager
.
closeRawFd
(
FILE_NAME
).
then
(()
=>
{
console
.
info
(
'
Succeed in closing resource file descriptor
'
);
}).
catch
(
error
=>
{
console
.
error
(
`Failed to close resource file descriptor. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
});
```
```
...
...
zh-cn/application-dev/reference/apis/js-apis-system-vibrate.md
浏览文件 @
a8424bab
...
@@ -36,17 +36,17 @@ import vibrator from '@system.vibrator';
...
@@ -36,17 +36,17 @@ import vibrator from '@system.vibrator';
**示例:**
**示例:**
```
j
s
```
t
s
vibrator
.
vibrate
({
vibrator
.
vibrate
({
mode
:
'
short
'
,
mode
:
'
short
'
,
success
:
function
()
{
success
:
function
()
{
console
.
log
(
'
vibrate is successful
'
);
console
.
info
(
'
Succeed in vibrating
'
);
},
},
fail
:
function
(
data
,
code
)
{
fail
:
function
(
data
,
code
)
{
console
.
log
(
"
vibrate is failed, data:
"
+
data
+
"
, code:
"
+
code
);
console
.
info
(
`Failed to vibrate. Data:
${
data
}
, code:
${
code
}
`
);
},
},
complete
:
function
()
{
complete
:
function
()
{
console
.
log
(
'
vibrate is completed
'
);
console
.
info
(
'
completed in vibrating
'
);
}
}
});
});
```
```
...
...
zh-cn/application-dev/reference/apis/js-apis-vibrator.md
浏览文件 @
a8424bab
...
@@ -9,7 +9,7 @@ vibrator模块提供控制马达振动启、停的能力。
...
@@ -9,7 +9,7 @@ vibrator模块提供控制马达振动启、停的能力。
## 导入模块
## 导入模块
```
j
s
```
t
s
import
vibrator
from
'
@ohos.vibrator
'
;
import
vibrator
from
'
@ohos.vibrator
'
;
```
```
...
@@ -41,24 +41,25 @@ startVibration(effect: VibrateEffect, attribute: VibrateAttribute, callback: Asy
...
@@ -41,24 +41,25 @@ startVibration(effect: VibrateEffect, attribute: VibrateAttribute, callback: Asy
示例:
示例:
```
j
s
```
t
s
import
vibrator
from
'
@ohos.vibrator
'
;
import
vibrator
from
'
@ohos.vibrator
'
;
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
.
error
(
'
Vibrate fail, error.code:
'
+
error
.
code
+
'
error.message:
'
,
+
error
.
message
);
console
.
error
(
`Failed to start vibration. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
return
;
return
;
}
}
console
.
log
(
'
Callback returned to indicate a successful vibration.
'
);
console
.
info
(
'
Succeed in starting vibration
'
);
});
});
}
catch
(
err
)
{
}
catch
(
err
)
{
console
.
error
(
'
errCode:
'
+
err
.
code
+
'
,msg:
'
+
err
.
message
);
console
.
error
(
`An unexpected error occurred. Code:
${
err
.
code
}
, message:
${
err
.
message
}
`
);
}
}
```
```
...
@@ -95,24 +96,25 @@ startVibration(effect: VibrateEffect, attribute: VibrateAttribute): Promise<v
...
@@ -95,24 +96,25 @@ startVibration(effect: VibrateEffect, attribute: VibrateAttribute): Promise<v
**示例:**
**示例:**
```
j
s
```
t
s
import
vibrator
from
'
@ohos.vibrator
'
;
import
vibrator
from
'
@ohos.vibrator
'
;
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
.
info
(
'
Succeed in starting
vibration
'
);
},
(
error
)
=>
{
},
(
error
)
=>
{
console
.
error
(
'
error.code
'
+
error
.
code
+
'
error.message
'
+
error
.
message
);
console
.
error
(
`Failed to start vibration. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
});
});
}
catch
(
err
)
{
}
catch
(
err
)
{
console
.
error
(
'
errCode:
'
+
err
.
code
+
'
,msg:
'
+
err
.
message
);
console
.
error
(
`An unexpected error occurred. Code:
${
err
.
code
}
, message:
${
err
.
message
}
`
);
}
}
```
```
## vibrator.stopVibration<sup>9+</sup>
## vibrator.stopVibration<sup>9+</sup>
...
@@ -133,40 +135,41 @@ stopVibration(stopMode: VibratorStopMode, callback: AsyncCallback<void>):
...
@@ -133,40 +135,41 @@ stopVibration(stopMode: VibratorStopMode, callback: AsyncCallback<void>):
**示例:**
**示例:**
```
j
s
```
t
s
import
vibrator
from
'
@ohos.vibrator
'
;
import
vibrator
from
'
@ohos.vibrator
'
;
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
.
error
(
'
Vibrate fail, error.code:
'
+
error
.
code
+
'
error.message:
'
,
+
error
.
message
);
console
.
error
(
`Failed to start vibration. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
return
;
return
;
}
}
console
.
log
(
'
Callback returned to indicate a successful vibration.
'
);
console
.
info
(
'
Succeed in starting vibration
'
);
});
});
}
catch
(
err
)
{
}
catch
(
err
)
{
console
.
error
(
'
errCode:
'
+
err
.
code
+
'
,msg:
'
+
err
.
message
);
console
.
error
(
`An unexpected error occurred. Code:
${
err
.
code
}
, message:
${
err
.
message
}
`
);
}
}
try
{
try
{
// 按照VIBRATOR_STOP_MODE_TIME模式停止振动
// 按照VIBRATOR_STOP_MODE_TIME模式停止振动
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
.
error
(
`Failed to stop vibration. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
return
;
return
;
}
}
console
.
log
(
'
Callback returned to indicate successful.
'
);
console
.
info
(
'
Succeed in stopping vibration
'
);
})
})
}
catch
(
err
)
{
}
catch
(
err
)
{
console
.
info
(
'
errCode:
'
+
err
.
code
+
'
,msg:
'
+
err
.
message
);
console
.
error
(
`An unexpected error occurred. Code:
${
err
.
code
}
, message:
${
err
.
message
}
`
);
}
}
```
```
## vibrator.stopVibration<sup>9+</sup>
## vibrator.stopVibration<sup>9+</sup>
...
@@ -192,36 +195,37 @@ stopVibration(stopMode: VibratorStopMode): Promise<void>
...
@@ -192,36 +195,37 @@ stopVibration(stopMode: VibratorStopMode): Promise<void>
**示例:**
**示例:**
```
j
s
```
t
s
import
vibrator
from
'
@ohos.vibrator
'
;
import
vibrator
from
'
@ohos.vibrator
'
;
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
.
info
(
'
Succeed in starting
vibration
'
);
},
(
error
)
=>
{
},
(
error
)
=>
{
console
.
error
(
'
error.code
'
+
error
.
code
+
'
error.message
'
+
error
.
message
);
console
.
error
(
`Failed to start vibration. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
});
});
}
catch
(
err
)
{
}
catch
(
err
)
{
console
.
error
(
'
errCode:
'
+
err
.
code
+
'
,msg:
'
+
err
.
message
);
console
.
error
(
`An unexpected error occurred. Code:
${
err
.
code
}
, message:
${
err
.
message
}
`
);
}
}
try
{
try
{
// 按照VIBRATOR_STOP_MODE_TIME模式停止振动
// 按照VIBRATOR_STOP_MODE_TIME模式停止振动
vibrator
.
stopVibration
(
vibrator
.
VibratorStopMode
.
VIBRATOR_STOP_MODE_PRESET
).
then
(()
=>
{
vibrator
.
stopVibration
(
vibrator
.
VibratorStopMode
.
VIBRATOR_STOP_MODE_PRESET
).
then
(()
=>
{
console
.
log
(
'
Promise returned to indicate a successful vibration.
'
);
console
.
info
(
'
Succeed in stopping vibration
'
);
},
(
error
)
=>
{
},
(
error
)
=>
{
console
.
log
(
'
error.code
'
+
error
.
code
+
'
error.message
'
+
error
.
message
);
console
.
error
(
`Failed to stop vibration. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
});
});
}
catch
(
err
)
{
}
catch
(
err
)
{
console
.
info
(
'
errCode:
'
+
err
.
code
+
'
,msg:
'
+
err
.
message
);
console
.
error
(
`An unexpected error occurred. Code:
${
err
.
code
}
, message:
${
err
.
message
}
`
);
}
}
```
```
## vibrator.stopVibration<sup>10+</sup>
## vibrator.stopVibration<sup>10+</sup>
...
@@ -241,40 +245,41 @@ stopVibration(callback: AsyncCallback<void>): void
...
@@ -241,40 +245,41 @@ stopVibration(callback: AsyncCallback<void>): void
**示例:**
**示例:**
```
j
s
```
t
s
import
vibrator
from
'
@ohos.vibrator
'
;
import
vibrator
from
'
@ohos.vibrator
'
;
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
.
error
(
'
Vibrate fail, error.code:
'
+
error
.
code
+
'
error.message:
'
,
+
error
.
message
);
console
.
error
(
`Failed to start vibration. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
return
;
return
;
}
}
console
.
log
(
'
Callback returned to indicate a successful vibration.
'
);
console
.
info
(
'
Succeed in starting vibration
'
);
});
});
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
error
(
'
errCode:
'
+
error
.
code
+
'
,msg:
'
+
error
.
message
);
console
.
error
(
`An unexpected error occurred. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
}
}
try
{
try
{
// 停止所有模式的马达振动
// 停止所有模式的马达振动
vibrator
.
stopVibration
(
function
(
error
)
{
vibrator
.
stopVibration
(
function
(
error
)
{
if
(
error
)
{
if
(
error
)
{
console
.
log
(
'
error.code
'
+
error
.
code
+
'
error.message
'
+
error
.
message
);
console
.
error
(
`Failed to stop vibration. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
return
;
return
;
}
}
console
.
log
(
'
Callback returned to indicate successful.
'
);
console
.
info
(
'
Succeed in stopping vibration
'
);
})
})
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
info
(
'
errCode:
'
+
error
.
code
+
'
,msg:
'
+
error
.
message
);
console
.
error
(
`An unexpected error occurred. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
}
}
```
```
## vibrator.stopVibration<sup>10+</sup>
## vibrator.stopVibration<sup>10+</sup>
...
@@ -294,36 +299,37 @@ stopVibration(): Promise<void>
...
@@ -294,36 +299,37 @@ stopVibration(): Promise<void>
**示例:**
**示例:**
```
j
s
```
t
s
import
vibrator
from
'
@ohos.vibrator
'
;
import
vibrator
from
'
@ohos.vibrator
'
;
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
.
info
(
'
Succeed in starting
vibration
'
);
},
(
error
)
=>
{
},
(
error
)
=>
{
console
.
error
(
'
error.code
'
+
error
.
code
+
'
error.message
'
+
error
.
message
);
console
.
error
(
`Failed to start vibration. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
});
});
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
error
(
'
errCode:
'
+
error
.
code
+
'
,msg:
'
+
error
.
message
);
console
.
error
(
`An unexpected error occurred. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
}
}
try
{
try
{
// 停止所有模式的马达振动
// 停止所有模式的马达振动
vibrator
.
stopVibration
().
then
(()
=>
{
vibrator
.
stopVibration
().
then
(()
=>
{
console
.
log
(
'
Promise returned to indicate a successful vibration.
'
);
console
.
info
(
'
Succeed in stopping vibration
'
);
},
(
error
)
=>
{
},
(
error
)
=>
{
console
.
log
(
'
error.code
'
+
error
.
code
+
'
error.message
'
+
error
.
message
);
console
.
error
(
`Failed to stop vibration. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
});
});
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
info
(
'
errCode:
'
+
error
.
code
+
'
,msg:
'
+
error
.
message
);
console
.
error
(
`An unexpected error occurred. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
}
}
```
```
## vibrator.isSupportEffect<sup>10+</sup>
## vibrator.isSupportEffect<sup>10+</sup>
...
@@ -342,40 +348,41 @@ isSupportEffect(effectId: string, callback: AsyncCallback<boolean>): void
...
@@ -342,40 +348,41 @@ isSupportEffect(effectId: string, callback: AsyncCallback<boolean>): void
**示例:**
**示例:**
```
j
s
```
t
s
import
vibrator
from
'
@ohos.vibrator
'
;
import
vibrator
from
'
@ohos.vibrator
'
;
try
{
try
{
// 查询是否支持'haptic.clock.timer'
// 查询是否支持'haptic.clock.timer'
vibrator
.
isSupportEffect
(
'
haptic.clock.timer
'
,
function
(
err
,
state
)
{
vibrator
.
isSupportEffect
(
'
haptic.clock.timer
'
,
function
(
err
,
state
)
{
if
(
err
)
{
if
(
err
)
{
console
.
error
(
'
isSupportEffect failed, error:
'
+
JSON
.
stringify
(
err
)
);
console
.
error
(
`Failed to query effect. Code:
${
err
.
code
}
, message:
${
err
.
message
}
`
);
return
;
return
;
}
}
console
.
log
(
'
The effectId is
'
+
(
state
?
'
supported
'
:
'
unsupported
'
)
);
console
.
info
(
'
Succeed in querying effect
'
);
if
(
state
)
{
if
(
state
)
{
try
{
try
{
vibrator
.
startVibration
({
// 使用startVibration需要添加ohos.permission.VIBRATE权限
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
.
error
(
'
haptic.clock.timer vibrator error:
'
+
JSON
.
stringify
(
error
)
);
console
.
error
(
`Failed to start vibration. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
}
else
{
}
else
{
console
.
log
(
'
haptic.clock.timer vibrator success
'
);
console
.
info
(
'
Succeed in starting vibration
'
);
}
}
});
});
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
error
(
'
Exception in, error:
'
+
JSON
.
stringify
(
error
)
);
console
.
error
(
`An unexpected error occurred. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
}
}
}
}
})
})
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
error
(
'
Exception in, error:
'
+
JSON
.
stringify
(
error
)
);
console
.
error
(
`An unexpected error occurred. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
}
}
```
```
## vibrator.isSupportEffect<sup>10+</sup>
## vibrator.isSupportEffect<sup>10+</sup>
...
@@ -399,36 +406,37 @@ isSupportEffect(effectId: string): Promise<boolean>
...
@@ -399,36 +406,37 @@ isSupportEffect(effectId: string): Promise<boolean>
**示例:**
**示例:**
```
j
s
```
t
s
import
vibrator
from
'
@ohos.vibrator
'
;
import
vibrator
from
'
@ohos.vibrator
'
;
try
{
try
{
// 查询是否支持'haptic.clock.timer'
// 查询是否支持'haptic.clock.timer'
vibrator
.
isSupportEffect
(
'
haptic.clock.timer
'
).
then
((
state
)
=>
{
vibrator
.
isSupportEffect
(
'
haptic.clock.timer
'
).
then
((
state
)
=>
{
console
.
log
(
'
The effectId is
'
+
(
state
?
'
supported
'
:
'
unsupported
'
)
);
console
.
info
(
`The query result is
${
state
}
`
);
if
(
state
)
{
if
(
state
)
{
try
{
try
{
vibrator
.
startVibration
({
vibrator
.
startVibration
({
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
.
info
(
'
Succeed in starting
vibration
'
);
}).
catch
((
error
)
=>
{
}).
catch
((
error
)
=>
{
console
.
error
(
'
Promise returned to indicate a failed vibration:
'
+
JSON
.
stringify
(
error
)
);
console
.
error
(
`Failed to start vibration. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
});
});
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
error
(
'
Exception in, error:
'
+
JSON
.
stringify
(
error
)
);
console
.
error
(
`An unexpected error occurred. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
}
}
}
}
},
(
error
)
=>
{
},
(
error
)
=>
{
console
.
error
(
'
isSupportEffect failed, error:
'
+
JSON
.
stringify
(
error
)
);
console
.
error
(
`Failed to query effect. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
})
})
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
error
(
'
Exception in, error:
'
+
JSON
.
stringify
(
error
)
);
console
.
error
(
`An unexpected error occurred. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
}
}
```
```
## EffectId
## EffectId
...
@@ -489,7 +497,7 @@ try {
...
@@ -489,7 +497,7 @@ try {
## VibrateFromFile<sup>10+</sup>
## VibrateFromFile<sup>10+</sup>
自定义振动类型,仅部分设备支持。
自定义振动类型,仅部分设备支持
,当设备不支持此振动类型时,返回
[
设备不支持错误码
](
../errorcodes/errorcode-universal.md
)
。
**系统能力**
:SystemCapability.Sensors.MiscDevice
**系统能力**
:SystemCapability.Sensors.MiscDevice
...
@@ -565,13 +573,13 @@ vibrate(duration: number): Promise<void>
...
@@ -565,13 +573,13 @@ vibrate(duration: number): Promise<void>
**示例:**
**示例:**
```
j
s
```
t
s
vibrator
.
vibrate
(
1000
).
then
(()
=>
{
vibrator
.
vibrate
(
1000
).
then
(()
=>
{
console
.
log
(
'
Promise returned to indicate a successful vibration.
'
);
console
.
info
(
'
Succeed in vibrating
'
);
},
(
error
)
=>
{
},
(
error
)
=>
{
console
.
log
(
'
error.code
'
+
error
.
code
+
'
error.message
'
+
error
.
message
);
console
.
error
(
`Failed to vibrate. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
});
});
```
```
## vibrator.vibrate<sup>(deprecated)</sup>
## vibrator.vibrate<sup>(deprecated)</sup>
...
@@ -594,15 +602,15 @@ vibrate(duration: number, callback?: AsyncCallback<void>): void
...
@@ -594,15 +602,15 @@ vibrate(duration: number, callback?: AsyncCallback<void>): void
**示例:**
**示例:**
```
j
s
```
t
s
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
.
error
(
`Failed to vibrate. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
}
else
{
}
else
{
console
.
log
(
'
Callback returned to indicate a successful vibration.
'
);
console
.
info
(
'
Succeed in vibrating
'
);
}
}
})
})
```
```
## vibrator.vibrate<sup>(deprecated)</sup>
## vibrator.vibrate<sup>(deprecated)</sup>
...
@@ -631,13 +639,13 @@ vibrate(effectId: EffectId): Promise<void>
...
@@ -631,13 +639,13 @@ vibrate(effectId: EffectId): Promise<void>
**示例:**
**示例:**
```
j
s
```
t
s
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
.
info
(
'
Succeed in vibrating
'
);
},
(
error
)
=>
{
},
(
error
)
=>
{
console
.
log
(
'
error.code
'
+
error
.
code
+
'
error.message
'
+
error
.
message
);
console
.
error
(
`Failed to vibrate. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
});
});
```
```
## vibrator.vibrate<sup>(deprecated)</sup>
## vibrator.vibrate<sup>(deprecated)</sup>
...
@@ -661,15 +669,15 @@ vibrate(effectId: EffectId, callback?: AsyncCallback<void>): void
...
@@ -661,15 +669,15 @@ vibrate(effectId: EffectId, callback?: AsyncCallback<void>): void
**示例:**
**示例:**
```
j
s
```
t
s
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
.
error
(
`Failed to vibrate. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
}
else
{
}
else
{
console
.
log
(
'
Callback returned to indicate a successful vibration.
'
);
console
.
info
(
'
Succeed in vibrating
'
);
}
}
})
})
```
```
## vibrator.stop<sup>(deprecated)</sup>
## vibrator.stop<sup>(deprecated)</sup>
...
@@ -697,22 +705,22 @@ stop(stopMode: VibratorStopMode): Promise<void>
...
@@ -697,22 +705,22 @@ stop(stopMode: VibratorStopMode): Promise<void>
**示例:**
**示例:**
```
j
s
```
t
s
// 按照effectId类型启动振动
// 按照effectId类型启动振动
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
.
error
(
`Failed to vibrate. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
}
else
{
}
else
{
console
.
log
(
'
Callback returned to indicate a successful vibration.
'
);
console
.
info
(
'
Succeed in vibrating
'
);
}
}
})
})
// 使用VIBRATOR_STOP_MODE_PRESET模式停止振动
// 使用VIBRATOR_STOP_MODE_PRESET模式停止振动
vibrator
.
stop
(
vibrator
.
VibratorStopMode
.
VIBRATOR_STOP_MODE_PRESET
).
then
(()
=>
{
vibrator
.
stop
(
vibrator
.
VibratorStopMode
.
VIBRATOR_STOP_MODE_PRESET
).
then
(()
=>
{
console
.
log
(
'
Promise returned to indicate a successful vibration.
'
);
console
.
info
(
'
Succeed in stopping
'
);
},
(
error
)
=>
{
},
(
error
)
=>
{
console
.
log
(
'
error.code
'
+
error
.
code
+
'
error.message
'
+
error
.
message
);
console
.
error
(
`Failed to stop. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
});
});
```
```
## vibrator.stop<sup>(deprecated)</sup>
## vibrator.stop<sup>(deprecated)</sup>
...
@@ -736,21 +744,21 @@ stop(stopMode: VibratorStopMode, callback?: AsyncCallback<void>): void
...
@@ -736,21 +744,21 @@ stop(stopMode: VibratorStopMode, callback?: AsyncCallback<void>): void
**示例:**
**示例:**
```
j
s
```
t
s
// 按照effectId类型启动振动
// 按照effectId类型启动振动
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
.
error
(
`Failed to vibrate. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
}
else
{
}
else
{
console
.
log
(
'
Callback returned to indicate a successful vibration.
'
);
console
.
info
(
'
Succeed in vibrating
'
);
}
}
})
})
// 使用VIBRATOR_STOP_MODE_PRESET模式停止振动
// 使用VIBRATOR_STOP_MODE_PRESET模式停止振动
vibrator
.
stop
(
vibrator
.
VibratorStopMode
.
VIBRATOR_STOP_MODE_PRESET
,
function
(
error
)
{
vibrator
.
stop
(
vibrator
.
VibratorStopMode
.
VIBRATOR_STOP_MODE_PRESET
,
function
(
error
)
{
if
(
error
)
{
if
(
error
)
{
console
.
log
(
'
error.code
'
+
error
.
code
+
'
error.message
'
+
error
.
message
);
console
.
error
(
`Failed to stop. Code:
${
error
.
code
}
, message:
${
error
.
message
}
`
);
}
else
{
}
else
{
console
.
log
(
'
Callback returned to indicate successful.
'
);
onsole
.
info
(
'
Succeed in stopping
'
);
}
}
})
})
```
```
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录