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

!21116 翻译完成:20233+20617+20595+20318+20668 sensor+vibrator更新

Merge pull request !21116 from wusongqing/TR20233
......@@ -31,12 +31,13 @@ For details about the APIs, see [Sensor](../reference/apis/js-apis-sensor.md).
For details about how to configure a permission, see [Declaring Permissions](../security/accesstoken-guidelines.md).
2. Subscribe to data changes of a type of sensor. The following uses the acceleration sensor as an example.
2. Subscribe to data changes of a type of sensor. The following uses the acceleration sensor as an example.
```js
```ts
import sensor from "@ohos.sensor";
sensor.on(sensor.SensorId.ACCELEROMETER, function(data){
console.info("Data obtained successfully. x: " + data.x + "y: " + data.y + "z: " + data.z); // Data is obtained.
sensor.on(sensor.SensorId.ACCELEROMETER, function (data) {
console.info("Succeeded in obtaining data. x: " + data.x + "y: " + data.y + "z: " + data.z); // Data is obtained.
});
```
......@@ -44,7 +45,7 @@ For details about the APIs, see [Sensor](../reference/apis/js-apis-sensor.md).
3. Unsubscribe from sensor data changes.
```js
```ts
import sensor from "@ohos.sensor";
sensor.off(sensor.SensorId.ACCELEROMETER);
```
......@@ -53,10 +54,11 @@ For details about the APIs, see [Sensor](../reference/apis/js-apis-sensor.md).
4. Subscribe to only one data change of a type of sensor.
```js
```ts
import sensor from "@ohos.sensor";
sensor.once(sensor.SensorId.ACCELEROMETER, function(data) {
console.info("Data obtained successfully. x: " + data.x + "y: " + data.y + "z: " + data.z); // Data is obtained.
sensor.once(sensor.SensorId.ACCELEROMETER, function (data) {
console.info("Succeeded in obtaining data. x: " + data.x + "y: " + data.y + "z: " + data.z); // Data is obtained.
});
```
......@@ -64,13 +66,14 @@ For details about the APIs, see [Sensor](../reference/apis/js-apis-sensor.md).
If the API fails to be called, you are advised to use the **try/catch** statement to capture error information that may occur in the code. Example:
```js
```ts
import sensor from "@ohos.sensor";
try {
sensor.once(sensor.SensorId.ACCELEROMETER, function(data) {
console.info("Data obtained successfully. x: " + data.x + "y: " + data.y + "z: " + data.z); // Data is obtained.
});
} catch (error) {
console.error("Get sensor data error. data:" + error.data, " msg:", error.message);
}
try {
sensor.once(sensor.SensorId.ACCELEROMETER, function (data) {
console.info("Succeeded in obtaining data. x: " + data.x + "y: " + data.y + "z: " + data.z); // Data is obtained.
});
} catch (error) {
console.error(`Failed to get sensor data. Code: ${error.code}, message: ${error.message}`);
}
```
......@@ -78,8 +78,8 @@ This JSON file contains two attributes: **MetaData** and **Channels**.
- **Channels** provides information about the vibration channel. It is a JSON array that holds information about each channel. It contains two attributes: **Parameters** and **Pattern**.
- **Parameters** provides parameters related to the channel. Under it, **Index** indicates the channel ID. The value is fixed at **1** for a single channel. This parameter is mandatory.
- **Pattern** indicates the vibration sequence. It is a JSON array. Under it, **Event** indicates a vibration event, which can be either of the following types:
- **transient**: short vibration
- **continuous**: long vibration
- **transient**: short vibration
- **continuous**: long vibration
The table below describes the parameters under **Event**.
......@@ -105,170 +105,161 @@ The following requirements must be met:
2. Start vibration with the specified effect and attribute.
```js
```ts
import vibrator from '@ohos.vibrator';
try {
vibrator.startVibration({ // To use startVibration, you must configure the ohos.permission.VIBRATE permission.
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.startVibration({ // To use startVibration, you must configure the ohos.permission.VIBRATE permission.
type: 'time',
duration: 1000,
}, {
id: 0,
usage: 'alarm'
}, (error) => {
if (error) {
console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
return;
}
console.info('Succeed in starting vibration.');
});
} catch (err) {
console.error('errCode: ' + err.code + ' ,msg: ' + err.message);
console.error(`An unexpected error occurred. Code: ${err.code}, message: ${err.message}`);
}
```
3. Stop vibration in the specified mode.
```js
```ts
import vibrator from '@ohos.vibrator';
try {
// Stop vibration in VIBRATOR_STOP_MODE_TIME mode. To use stopVibration, you must configure the ohos.permission.VIBRATE permission.
vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME, function (error) {
if (error) {
console.log('error.code' + error.code + 'error.message' + error.message);
return;
}
console.log('Callback returned to indicate successful.');
})
// Stop vibration in VIBRATOR_STOP_MODE_TIME mode. To use stopVibration, you must configure the ohos.permission.VIBRATE permission.
vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME, function (error) {
if (error) {
console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
return;
}
console.info('Succeeded in stopping vibration.');
})
} catch (err) {
console.info('errCode: ' + err.code + ' ,msg: ' + err.message);
console.error(`An unexpected error occurred. Code: ${err.code}, message: ${err.message}`);
}
```
4. Stop vibration in all modes.
```js
```ts
import vibrator from '@ohos.vibrator';
// To use startVibration and stopVibration, you must configure the ohos.permission.VIBRATE permission.
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.');
});
// Stop vibration in all modes.
vibrator.stopVibration(function (error) {
if (error) {
console.log('error.code' + error.code + 'error.message' + error.message);
return;
}
console.log('Callback returned to indicate successful.');
})
vibrator.startVibration({
type: 'time',
duration: 1000,
}, {
id: 0,
usage: 'alarm'
}, (error) => {
if (error) {
console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
return;
}
console.info('Succeed in starting vibration');
});
// Stop vibration in all modes.
vibrator.stopVibration(function (error) {
if (error) {
console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
return;
}
console.info('Succeed in stopping vibration');
})
} catch (error) {
console.info('errCode: ' + error.code + ' ,msg: ' + error.message);
console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`);
}
```
5. Check whether the passed effect ID is supported.
```js
```ts
import vibrator from '@ohos.vibrator';
try {
// Check whether 'haptic.clock.timer' is supported.
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({ // To use startVibration, you must configure the ohos.permission.VIBRATE permission.
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));
}
}
})
// Check whether 'haptic.clock.timer' is supported.
vibrator.isSupportEffect('haptic.clock.timer', function (err, state) {
if (err) {
console.error(`Failed to query effect. Code: ${err.code}, message: ${err.message}`);
return;
}
console.info('Succeed in querying effect');
if (state) {
try {
vibrator.startVibration({ // To use startVibration, you must configure the ohos.permission.VIBRATE permission.
type: 'preset',
effectId: 'haptic.clock.timer',
count: 1,
}, {
usage: 'unknown'
}, (error) => {
if (error) {
console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
} else {
console.info('Succeed in starting vibration');
}
});
} catch (error) {
console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`);
}
}
})
} catch (error) {
console.error('Exception in, error:' + JSON.stringify(error));
console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`);
}
```
6. Start and stop custom vibration.
```js
```ts
import vibrator from '@ohos.vibrator';
import resourceManager from '@ohos.resourceManager';
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);
});
}
// Obtain the file descriptor of the vibration configuration file.
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}`);
});
// To use startVibration and stopVibration, you must configure the ohos.permission.VIBRATE permission.
try {
// Start custom vibration.
vibrator.startVibration({
type: "file",
hapticFd: { fd: rawFd.fd, offset: rawFd.offset, length: rawFd.length }
}, {
usage: "alarm"
}).then(() => {
console.info('startVibration success');
}, (error) => {
console.info('startVibration error');
});
// Stop vibration in all modes.
vibrator.stopVibration(function (error) {
if (error) {
console.log('error.code' + error.code + 'error.message' + error.message);
return;
}
console.log('Callback returned to indicate successful.');
})
// Start custom vibration.
vibrator.startVibration({
type: "file",
hapticFd: { fd: fileDescriptor.fd, offset: fileDescriptor.offset, length: fileDescriptor.length }
}, {
usage: "alarm"
}).then(() => {
console.info('Succeed in starting vibration');
}, (error) => {
console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
});
// Stop vibration in all modes.
vibrator.stopVibration(function (error) {
if (error) {
console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
return;
}
console.info('Succeed in stopping vibration');
})
} catch (error) {
console.info('errCode: ' + error.code + ' ,msg: ' + error.message);
console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`);
}
// Close the vibration configuration file.
closeResource(FILE_NAME);
// Close the vibration file.
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}`);
});
```
因为 它太大了无法显示 source diff 。你可以改为 查看blob
......@@ -26,7 +26,7 @@ Triggers device vibration.
**Required permissions**: ohos.permission.VIBRATE
**System capability**: SystemCapability.Sensors.MiscDevice
**System capability**: SystemCapability.Sensors.MiscDevice.Lite
**Parameters**
......@@ -36,17 +36,17 @@ Triggers device vibration.
**Example**
```js
```ts
vibrator.vibrate({
mode: 'short',
success: function() {
console.log('vibrate is successful');
console.info('Succeed in vibrating');
},
fail: function(data, code) {
console.log("vibrate is failed, data: " + data + ", code: " + code);
console.info(`Failed to vibrate. Data: ${data}, code: ${code}`);
},
complete: function() {
console.log('vibrate is completed');
console.info('completed in vibrating');
}
});
```
......@@ -57,7 +57,7 @@ Defines the vibration options.
**Required permissions**: ohos.permission.VIBRATE
**System capability**: SystemCapability.Sensors.MiscDevice
**System capability**: SystemCapability.Sensors.MiscDevice.Lite
| Name | Type | Mandatory| Description |
| -------- | -------- | ---- | ------------------------------------------------------------ |
......
......@@ -9,7 +9,7 @@ The **vibrator** module provides APIs for starting or stopping vibration.
## Modules to Import
```js
```ts
import vibrator from '@ohos.vibrator';
```
......@@ -41,24 +41,25 @@ For details about the error codes, see [Vibrator Error Codes](../errorcodes/erro
**Example**
```js
```ts
import vibrator from '@ohos.vibrator';
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.startVibration({
type: 'time',
duration: 1000,
}, {
id: 0,
usage: 'alarm'
}, (error) => {
if (error) {
console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
return;
}
console.info('Succeed in starting vibration');
});
} 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 @@ For details about the error codes, see [Vibrator Error Codes](../errorcodes/erro
**Example**
```js
```ts
import vibrator from '@ohos.vibrator';
try {
vibrator.startVibration({
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);
});
vibrator.startVibration({
type: 'time',
duration: 1000
}, {
id: 0,
usage: 'alarm'
}).then(() => {
console.info('Succeed in starting vibration');
}, (error) => {
console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
});
} 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>
......@@ -133,40 +135,41 @@ Stops vibration in the specified mode. This API uses an asynchronous callback to
**Example**
```js
```ts
import vibrator from '@ohos.vibrator';
try {
// Start vibration at a fixed duration.
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.');
});
// Start vibration at a fixed duration.
vibrator.startVibration({
type: 'time',
duration: 1000,
}, {
id: 0,
usage: 'alarm'
}, (error) => {
if (error) {
console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
return;
}
console.info('Succeed in starting vibration');
});
} catch (err) {
console.error('errCode: ' + err.code + ' ,msg: ' + err.message);
console.error(`An unexpected error occurred. Code: ${err.code}, message: ${err.message}`);
}
try {
// Stop vibration in VIBRATOR_STOP_MODE_TIME mode.
vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME, function (error) {
if (error) {
console.log('error.code' + error.code + 'error.message' + error.message);
return;
}
console.log('Callback returned to indicate successful.');
})
// Stop vibration in VIBRATOR_STOP_MODE_TIME mode.
vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME, function (error) {
if (error) {
console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
return;
}
console.info('Succeed in stopping vibration');
})
} 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>
......@@ -192,36 +195,37 @@ Stops vibration in the specified mode. This API uses a promise to return the res
**Example**
```js
```ts
import vibrator from '@ohos.vibrator';
try {
// Start vibration at a fixed duration.
vibrator.startVibration({
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);
});
// Start vibration at a fixed duration.
vibrator.startVibration({
type: 'time',
duration: 1000
}, {
id: 0,
usage: 'alarm'
}).then(() => {
console.info('Succeed in starting vibration');
}, (error) => {
console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
});
} catch (err) {
console.error('errCode: ' + err.code + ' ,msg: ' + err.message);
console.error(`An unexpected error occurred. Code: ${err.code}, message: ${err.message}`);
}
try {
// Stop vibration in VIBRATOR_STOP_MODE_TIME mode.
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);
});
// Stop vibration in VIBRATOR_STOP_MODE_TIME mode.
vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(() => {
console.info('Succeed in stopping vibration');
}, (error) => {
console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
});
} 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>
......@@ -241,40 +245,41 @@ Stops vibration in all modes. This API uses an asynchronous callback to return t
**Example**
```js
```ts
import vibrator from '@ohos.vibrator';
try {
// Start vibration at a fixed duration.
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.');
});
// Start vibration at a fixed duration.
vibrator.startVibration({
type: 'time',
duration: 1000,
}, {
id: 0,
usage: 'alarm'
}, (error) => {
if (error) {
console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
return;
}
console.info('Succeed in starting vibration');
});
} catch (error) {
console.error('errCode: ' + error.code + ' ,msg: ' + error.message);
console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`);
}
try {
// Stop vibration in all modes.
vibrator.stopVibration(function (error) {
if (error) {
console.log('error.code' + error.code + 'error.message' + error.message);
return;
}
console.log('Callback returned to indicate successful.');
})
// Stop vibration in all modes.
vibrator.stopVibration(function (error) {
if (error) {
console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
return;
}
console.info('Succeed in stopping vibration');
})
} 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>
......@@ -294,36 +299,37 @@ Stops vibration in all modes. This API uses a promise to return the result.
**Example**
```js
```ts
import vibrator from '@ohos.vibrator';
try {
// Start vibration at a fixed duration.
vibrator.startVibration({
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);
});
// Start vibration at a fixed duration.
vibrator.startVibration({
type: 'time',
duration: 1000
}, {
id: 0,
usage: 'alarm'
}).then(() => {
console.info('Succeed in starting vibration');
}, (error) => {
console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
});
} catch (error) {
console.error('errCode: ' + error.code + ' ,msg: ' + error.message);
console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`);
}
try {
// Stop vibration in all modes.
vibrator.stopVibration().then(() => {
console.log('Promise returned to indicate a successful vibration.');
}, (error) => {
console.log('error.code' + error.code + 'error.message' + error.message);
});
// Stop vibration in all modes.
vibrator.stopVibration().then(() => {
console.info('Succeed in stopping vibration');
}, (error) => {
console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
});
} 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>
......@@ -342,40 +348,41 @@ Checks whether the passed effect ID is supported. This API uses an asynchronous
**Example**
```js
```ts
import vibrator from '@ohos.vibrator';
try {
// Check whether 'haptic.clock.timer' is supported.
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({ // To use startVibration, you must configure the ohos.permission.VIBRATE permission.
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));
}
}
})
// Check whether 'haptic.clock.timer' is supported.
vibrator.isSupportEffect('haptic.clock.timer', function (err, state) {
if (err) {
console.error(`Failed to query effect. Code: ${err.code}, message: ${err.message}`);
return;
}
console.info('Succeed in querying effect');
if (state) {
try {
vibrator.startVibration({ // To use startVibration, you must configure the ohos.permission.VIBRATE permission.
type: 'preset',
effectId: 'haptic.clock.timer',
count: 1,
}, {
usage: 'unknown'
}, (error) => {
if (error) {
console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
} else {
console.info('Succeed in starting vibration');
}
});
} catch (error) {
console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`);
}
}
})
} 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>
......@@ -399,36 +406,37 @@ Checks whether the passed effect ID is supported. This API uses a promise to ret
**Example**
```js
```ts
import vibrator from '@ohos.vibrator';
try {
// Check whether 'haptic.clock.timer' is supported.
vibrator.isSupportEffect('haptic.clock.timer').then((state) => {
console.log('The effectId is ' + (state ? 'supported' : 'unsupported'));
if (state) {
try {
vibrator.startVibration({
type: 'preset',
effectId: 'haptic.clock.timer',
count: 1,
}, {
usage: 'unknown'
}).then(()=>{
console.log('Promise returned to indicate a successful vibration');
}).catch((error)=>{
console.error('Promise returned to indicate a failed vibration:' + JSON.stringify(error));
});
} catch (error) {
console.error('Exception in, error:' + JSON.stringify(error));
}
}
}, (error) => {
console.error('isSupportEffect failed, error:' + JSON.stringify(error));
})
// Check whether 'haptic.clock.timer' is supported.
vibrator.isSupportEffect('haptic.clock.timer').then((state) => {
console.info(`The query result is ${state}`);
if (state) {
try {
vibrator.startVibration({
type: 'preset',
effectId: 'haptic.clock.timer',
count: 1,
}, {
usage: 'unknown'
}).then(() => {
console.info('Succeed in starting vibration');
}).catch((error) => {
console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
});
} catch (error) {
console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`);
}
}
}, (error) => {
console.error(`Failed to query effect. Code: ${error.code}, message: ${error.message}`);
})
} catch (error) {
console.error('Exception in, error:' + JSON.stringify(error));
console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`);
}
```
```
## EffectId
......@@ -489,7 +497,7 @@ Describes the vibration with a preset effect.
## VibrateFromFile<sup>10+</sup>
Describes the custom vibration type, which is supported only by certain devices.
Describes the custom vibration type, which is supported only by certain devices. If a device does not support this vibration type, [an error code indicating unsupported device](../errorcodes/errorcode-universal.md) is returned.
**System capability**: SystemCapability.Sensors.MiscDevice
......@@ -565,13 +573,13 @@ This API is deprecated since API version 9. You are advised to use [vibrator.sta
**Example**
```js
```ts
vibrator.vibrate(1000).then(() => {
console.log('Promise returned to indicate a successful vibration.');
console.info('Succeed in vibrating');
}, (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>
......@@ -594,15 +602,15 @@ This API is deprecated since API version 9. You are advised to use [vibrator.sta
**Example**
```js
```ts
vibrator.vibrate(1000, function (error) {
if (error) {
console.log('error.code' + error.code + 'error.message' + error.message);
} else {
console.log('Callback returned to indicate a successful vibration.');
}
if (error) {
console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`);
} else {
console.info('Succeed in vibrating');
}
})
```
```
## vibrator.vibrate<sup>(deprecated)</sup>
......@@ -631,13 +639,13 @@ This API is deprecated since API version 9. You are advised to use [vibrator.sta
**Example**
```js
```ts
vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER).then(() => {
console.log('Promise returned to indicate a successful vibration.');
console.info('Succeed in vibrating');
}, (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>
......@@ -661,15 +669,15 @@ This API is deprecated since API version 9. You are advised to use [vibrator.sta
**Example**
```js
```ts
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.');
}
if (error) {
console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`);
} else {
console.info('Succeed in vibrating');
}
})
```
```
## vibrator.stop<sup>(deprecated)</sup>
......@@ -697,22 +705,22 @@ This API is deprecated since API version 9. You are advised to use [vibrator.sto
**Example**
```js
```ts
// Start vibration based on the specified effect ID.
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.');
}
if (error) {
console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`);
} else {
console.info('Succeed in vibrating');
}
})
// Stop vibration in VIBRATOR_STOP_MODE_PRESET mode.
vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(() => {
console.log('Promise returned to indicate a successful vibration.');
console.info('Succeed in stopping');
}, (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>
......@@ -736,21 +744,21 @@ This API is deprecated since API version 9. You are advised to use [vibrator.sto
**Example**
```js
```ts
// Start vibration based on the specified effect ID.
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.');
}
if (error) {
console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`);
} else {
console.info('Succeed in vibrating');
}
})
// Stop vibration in VIBRATOR_STOP_MODE_PRESET mode.
vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, function (error) {
if (error) {
console.log('error.code' + error.code + 'error.message' + error.message);
} else {
console.log('Callback returned to indicate successful.');
}
if (error) {
console.error(`Failed to stop. Code: ${error.code}, message: ${error.message}`);
} else {
onsole.info('Succeed in stopping');
}
})
```
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册