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

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

Merge pull request !21116 from wusongqing/TR20233
......@@ -33,10 +33,11 @@ For details about the APIs, see [Sensor](../reference/apis/js-apis-sensor.md).
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.
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("Get sensor data error. data:" + error.data, " msg:", error.message);
console.error(`Failed to get sensor data. Code: ${error.code}, message: ${error.message}`);
}
```
......@@ -78,7 +78,7 @@ 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
- **transient**: short vibration
- **continuous**: long vibration
The table below describes the parameters under **Event**.
......@@ -105,8 +105,9 @@ 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',
......@@ -116,37 +117,38 @@ The following requirements must be met:
usage: 'alarm'
}, (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;
}
console.log('Callback returned to indicate a successful vibration.');
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);
console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
return;
}
console.log('Callback returned to indicate successful.');
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 {
......@@ -158,36 +160,37 @@ The following requirements must be met:
usage: 'alarm'
}, (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;
}
console.log('Callback returned to indicate a successful vibration.');
console.info('Succeed in starting vibration');
});
// Stop vibration in all modes.
vibrator.stopVibration(function (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;
}
console.log('Callback returned to indicate successful.');
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));
console.error(`Failed to query effect. Code: ${err.code}, message: ${err.message}`);
return;
}
console.log('The effectId is ' + (state ? 'supported' : 'unsupported'));
console.info('Succeed in querying effect');
if (state) {
try {
vibrator.startVibration({ // To use startVibration, you must configure the ohos.permission.VIBRATE permission.
......@@ -197,78 +200,66 @@ The following requirements must be met:
}, {
usage: 'unknown'
}, (error) => {
if(error) {
console.error('haptic.clock.timer vibrator error:' + JSON.stringify(error));
if (error) {
console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
} else {
console.log('haptic.clock.timer vibrator success');
console.info('Succeed in starting vibration');
}
});
} catch (error) {
console.error('Exception in, error:' + JSON.stringify(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) {
// Obtain the file descriptor of the vibration configuration file.
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);
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.log('closeResource err: ' + error);
console.error(`Failed to get resource file descriptor. Code: ${error.code}, message: ${error.message}`);
});
}
// Obtain the file descriptor of the vibration configuration file.
let rawFd = openResource(FILE_NAME);
// 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 }
hapticFd: { fd: fileDescriptor.fd, offset: fileDescriptor.offset, length: fileDescriptor.length }
}, {
usage: "alarm"
}).then(() => {
console.info('startVibration success');
console.info('Succeed in starting vibration');
}, (error) => {
console.info('startVibration 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.log('error.code' + error.code + 'error.message' + error.message);
console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
return;
}
console.log('Callback returned to indicate successful.');
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}`);
});
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册