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

!20595 【泛Sensor】-- 根据规范修改代码示例格式

Merge pull request !20595 from lixiangpeng5/doc-r
...@@ -110,171 +110,162 @@ Json文件共包含2个属性。 ...@@ -110,171 +110,162 @@ Json文件共包含2个属性。
2. 根据指定振动效果和振动属性触发马达振动。 2. 根据指定振动效果和振动属性触发马达振动。
```js ```ts
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. 按照指定模式停止马达的振动。
```js ```ts
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. 停止所有模式的马达振动。
```js ```ts
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。
```js ```ts
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. 启动和停止自定义振动
```js ```ts
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}`);
});
``` ```
......
...@@ -36,17 +36,17 @@ import vibrator from '@system.vibrator'; ...@@ -36,17 +36,17 @@ import vibrator from '@system.vibrator';
**示例:** **示例:**
```js ```ts
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');
} }
}); });
``` ```
......
...@@ -9,7 +9,7 @@ vibrator模块提供控制马达振动启、停的能力。 ...@@ -9,7 +9,7 @@ vibrator模块提供控制马达振动启、停的能力。
## 导入模块 ## 导入模块
```js ```ts
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
示例: 示例:
```js ```ts
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
**示例:** **示例:**
```js ```ts
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&lt;void&gt;): ...@@ -133,40 +135,41 @@ stopVibration(stopMode: VibratorStopMode, callback: AsyncCallback&lt;void&gt;):
**示例:** **示例:**
```js ```ts
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&lt;void&gt; ...@@ -192,36 +195,37 @@ stopVibration(stopMode: VibratorStopMode): Promise&lt;void&gt;
**示例:** **示例:**
```js ```ts
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&lt;void&gt;): void ...@@ -241,40 +245,41 @@ stopVibration(callback: AsyncCallback&lt;void&gt;): void
**示例:** **示例:**
```js ```ts
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&lt;void&gt; ...@@ -294,36 +299,37 @@ stopVibration(): Promise&lt;void&gt;
**示例:** **示例:**
```js ```ts
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&lt;boolean&gt;): void ...@@ -342,40 +348,41 @@ isSupportEffect(effectId: string, callback: AsyncCallback&lt;boolean&gt;): void
**示例:** **示例:**
```js ```ts
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&lt;boolean&gt; ...@@ -399,36 +406,37 @@ isSupportEffect(effectId: string): Promise&lt;boolean&gt;
**示例:** **示例:**
```js ```ts
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&lt;void&gt; ...@@ -565,13 +573,13 @@ vibrate(duration: number): Promise&lt;void&gt;
**示例:** **示例:**
```js ```ts
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&lt;void&gt;): void ...@@ -594,15 +602,15 @@ vibrate(duration: number, callback?: AsyncCallback&lt;void&gt;): void
**示例:** **示例:**
```js ```ts
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&lt;void&gt; ...@@ -631,13 +639,13 @@ vibrate(effectId: EffectId): Promise&lt;void&gt;
**示例:** **示例:**
```js ```ts
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&lt;void&gt;): void ...@@ -661,15 +669,15 @@ vibrate(effectId: EffectId, callback?: AsyncCallback&lt;void&gt;): void
**示例:** **示例:**
```js ```ts
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&lt;void&gt; ...@@ -697,22 +705,22 @@ stop(stopMode: VibratorStopMode): Promise&lt;void&gt;
**示例:** **示例:**
```js ```ts
// 按照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&lt;void&gt;): void ...@@ -736,21 +744,21 @@ stop(stopMode: VibratorStopMode, callback?: AsyncCallback&lt;void&gt;): void
**示例:** **示例:**
```js ```ts
// 按照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.
先完成此消息的编辑!
想要评论请 注册