提交 d8adba08 编写于 作者: G Gloria

Update docs against 20233+20617+20595+20318+20668

Signed-off-by: wusongqing<wusongqing@huawei.com>
上级 67d5f551
...@@ -31,12 +31,13 @@ For details about the APIs, see [Sensor](../reference/apis/js-apis-sensor.md). ...@@ -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). 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"; 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). ...@@ -44,7 +45,7 @@ For details about the APIs, see [Sensor](../reference/apis/js-apis-sensor.md).
3. Unsubscribe from sensor data changes. 3. Unsubscribe from sensor data changes.
```js ```ts
import sensor from "@ohos.sensor"; import sensor from "@ohos.sensor";
sensor.off(sensor.SensorId.ACCELEROMETER); sensor.off(sensor.SensorId.ACCELEROMETER);
``` ```
...@@ -53,10 +54,11 @@ For details about the APIs, see [Sensor](../reference/apis/js-apis-sensor.md). ...@@ -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. 4. Subscribe to only one data change of a type of sensor.
```js ```ts
import sensor from "@ohos.sensor"; 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). ...@@ -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: 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"; import sensor from "@ohos.sensor";
try {
sensor.once(sensor.SensorId.ACCELEROMETER, function(data) { try {
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); } 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**. ...@@ -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**. - **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. - **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: - **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 - **continuous**: long vibration
The table below describes the parameters under **Event**. The table below describes the parameters under **Event**.
...@@ -105,170 +105,161 @@ The following requirements must be met: ...@@ -105,170 +105,161 @@ The following requirements must be met:
2. Start vibration with the specified effect and attribute. 2. Start vibration with the specified effect and attribute.
```js ```ts
import vibrator from '@ohos.vibrator'; import vibrator from '@ohos.vibrator';
try { try {
vibrator.startVibration({ // To use startVibration, you must configure the ohos.permission.VIBRATE permission. vibrator.startVibration({ // To use startVibration, you must configure the ohos.permission.VIBRATE permission.
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. Stop vibration in the specified mode. 3. Stop vibration in the specified mode.
```js ```ts
import vibrator from '@ohos.vibrator'; import vibrator from '@ohos.vibrator';
try { try {
// Stop vibration in VIBRATOR_STOP_MODE_TIME mode. To use stopVibration, you must configure the ohos.permission.VIBRATE permission. // 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) { 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. Stop vibration in all modes. 4. Stop vibration in all modes.
```js ```ts
import vibrator from '@ohos.vibrator'; import vibrator from '@ohos.vibrator';
// To use startVibration and stopVibration, you must configure the ohos.permission.VIBRATE permission. // To use startVibration and stopVibration, you must configure the ohos.permission.VIBRATE permission.
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');
}); });
// Stop vibration in all modes. // Stop vibration in all modes.
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. Check whether the passed effect ID is supported. 5. Check whether the passed effect ID is supported.
```js ```ts
import vibrator from '@ohos.vibrator'; import vibrator from '@ohos.vibrator';
try { try {
// Check whether 'haptic.clock.timer' is supported. // Check whether 'haptic.clock.timer' is supported.
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({ // To use startVibration, you must configure the ohos.permission.VIBRATE permission. vibrator.startVibration({ // To use startVibration, you must configure the ohos.permission.VIBRATE permission.
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. Start and stop custom vibration. 6. Start and stop custom vibration.
```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);
});
}
// Obtain the file descriptor of the vibration configuration file. // 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. // To use startVibration and stopVibration, you must configure the ohos.permission.VIBRATE permission.
try { try {
// Start custom vibration. // Start custom vibration.
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}`);
}); });
// Stop vibration in all modes. // Stop vibration in all modes.
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}`);
} }
// Close the vibration configuration file. // Close the vibration file.
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}`);
});
``` ```
因为 它太大了无法显示 source diff 。你可以改为 查看blob
...@@ -25,7 +25,7 @@ import sensor from '@system.sensor'; ...@@ -25,7 +25,7 @@ import sensor from '@system.sensor';
Subscribes to data changes of the acceleration sensor. If this API is called multiple times for the same application, the last call takes effect. Subscribes to data changes of the acceleration sensor. If this API is called multiple times for the same application, the last call takes effect.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
**Required permissions**: ohos.permission.ACCELEROMETER (a system permission) **Required permissions**: ohos.permission.ACCELEROMETER (a system permission)
...@@ -37,16 +37,16 @@ Subscribes to data changes of the acceleration sensor. If this API is called mul ...@@ -37,16 +37,16 @@ Subscribes to data changes of the acceleration sensor. If this API is called mul
**Example** **Example**
```js ```ts
sensor.subscribeAccelerometer({ sensor.subscribeAccelerometer({
interval: 'normal', interval: 'normal',
success: function(ret) { success: function (ret) {
console.log('X-axis data: ' + ret.x); console.info('Succeeded in subscribing. X-axis data: ' + ret.x);
console.log('Y-axis data: ' + ret.y); console.info('Succeeded in subscribing. Y-axis data: ' + ret.y);
console.log('Z-axis data: ' + ret.z); console.info('Succeeded in subscribing. Z-axis data: ' + ret.z);
}, },
fail: function(data, code) { fail: function (data, code) {
console.error('Subscription failed. Code: ' + code + '; Data: ' + data); console.error(`Failed to subscription. Code: ${code}, data: ${data}`);
}, },
}); });
``` ```
...@@ -60,13 +60,13 @@ unsubscribeAccelerometer(): void ...@@ -60,13 +60,13 @@ unsubscribeAccelerometer(): void
Unsubscribes from data changes of the acceleration sensor. Unsubscribes from data changes of the acceleration sensor.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
**Required permissions**: ohos.permission.ACCELEROMETER (a system permission) **Required permissions**: ohos.permission.ACCELEROMETER (a system permission)
**Example** **Example**
```js ```ts
sensor.unsubscribeAccelerometer(); sensor.unsubscribeAccelerometer();
``` ```
...@@ -76,7 +76,7 @@ sensor.unsubscribeAccelerometer(); ...@@ -76,7 +76,7 @@ sensor.unsubscribeAccelerometer();
Subscribes to data changes of the compass sensor. If this API is called multiple times for the same application, the last call takes effect. Subscribes to data changes of the compass sensor. If this API is called multiple times for the same application, the last call takes effect.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
**Parameters** **Parameters**
...@@ -86,13 +86,13 @@ Subscribes to data changes of the compass sensor. If this API is called multiple ...@@ -86,13 +86,13 @@ Subscribes to data changes of the compass sensor. If this API is called multiple
**Example** **Example**
```js ```ts
sensor.subscribeCompass({ sensor.subscribeCompass({
success: function(ret) { success: function (ret) {
console.log('Get data direction:' + ret.direction); console.info('Succeeded in subscribing. Get data direction:' + ret.direction);
}, },
fail: function(data, code) { fail: function (data, code) {
console.error('Subscription failed. Code: ' + code + '; Data: ' + data); console.error(`Failed to subscription. Code: ${code}, data: ${data}`);
}, },
}); });
``` ```
...@@ -106,11 +106,11 @@ unsubscribeCompass(): void ...@@ -106,11 +106,11 @@ unsubscribeCompass(): void
Unsubscribes from data changes of the compass sensor. Unsubscribes from data changes of the compass sensor.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
**Example** **Example**
```js ```ts
sensor.unsubscribeCompass(); sensor.unsubscribeCompass();
``` ```
...@@ -120,7 +120,7 @@ sensor.unsubscribeCompass(); ...@@ -120,7 +120,7 @@ sensor.unsubscribeCompass();
Subscribes to data changes of the proximity sensor. If this API is called multiple times for the same application, the last call takes effect. Subscribes to data changes of the proximity sensor. If this API is called multiple times for the same application, the last call takes effect.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
**Parameters** **Parameters**
...@@ -130,13 +130,14 @@ Subscribes to data changes of the proximity sensor. If this API is called multip ...@@ -130,13 +130,14 @@ Subscribes to data changes of the proximity sensor. If this API is called multip
**Example** **Example**
```js ```ts
sensor.subscribeProximity({ sensor.subscribeProximity({
success: function(ret) { success: function (ret) {
console.log('Get data distance:' + ret.distance); console.info('Succeeded in subscribing. Get data distance:' + ret.distance);
}, },
fail: function(data, code) { fail: function (data, code) {
console.error('Subscription failed. Code: ' + code + '; Data: ' + data); console.error(`Failed to subscription. Code: ${code}, data: ${data}`);
}, },
}); });
``` ```
...@@ -150,11 +151,11 @@ unsubscribeProximity(): void ...@@ -150,11 +151,11 @@ unsubscribeProximity(): void
Unsubscribes from data changes of the proximity sensor. Unsubscribes from data changes of the proximity sensor.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
**Example** **Example**
```js ```ts
sensor.unsubscribeProximity(); sensor.unsubscribeProximity();
``` ```
...@@ -164,7 +165,7 @@ sensor.unsubscribeProximity(); ...@@ -164,7 +165,7 @@ sensor.unsubscribeProximity();
Subscribes to data changes of the ambient light sensor. If this API is called multiple times, the last call takes effect. Subscribes to data changes of the ambient light sensor. If this API is called multiple times, the last call takes effect.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
**Parameters** **Parameters**
...@@ -174,13 +175,13 @@ Subscribes to data changes of the ambient light sensor. If this API is called mu ...@@ -174,13 +175,13 @@ Subscribes to data changes of the ambient light sensor. If this API is called mu
**Example** **Example**
```js ```ts
sensor.subscribeLight({ sensor.subscribeLight({
success: function(ret) { success: function (ret) {
console.log('Get data intensity:' + ret.intensity); console.info('Succeeded in subscribing. Get data intensity:' + ret.intensity);
}, },
fail: function(data, code) { fail: function (data, code) {
console.error('Subscription failed. Code: ' + code + '; Data: ' + data); console.error(`Failed to subscription. Code: ${code}, data: ${data}`);
}, },
}); });
``` ```
...@@ -194,11 +195,11 @@ unsubscribeLight(): void ...@@ -194,11 +195,11 @@ unsubscribeLight(): void
Unsubscribes from data changes of the ambient light sensor. Unsubscribes from data changes of the ambient light sensor.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
**Example** **Example**
```js ```ts
sensor.unsubscribeLight(); sensor.unsubscribeLight();
``` ```
...@@ -208,7 +209,7 @@ sensor.unsubscribeLight(); ...@@ -208,7 +209,7 @@ sensor.unsubscribeLight();
Subscribes to data changes of the step counter sensor. If this API is called multiple times for the same application, the last call takes effect. Subscribes to data changes of the step counter sensor. If this API is called multiple times for the same application, the last call takes effect.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
**Required permissions**: ohos.permission.ACTIVITY_MOTION **Required permissions**: ohos.permission.ACTIVITY_MOTION
...@@ -220,13 +221,13 @@ Subscribes to data changes of the step counter sensor. If this API is called mul ...@@ -220,13 +221,13 @@ Subscribes to data changes of the step counter sensor. If this API is called mul
**Example** **Example**
```js ```ts
sensor.subscribeStepCounter({ sensor.subscribeStepCounter({
success: function(ret) { success: function (ret) {
console.log('Get step value:' + ret.steps); console.info('Succeeded in subscribing. Get step value:' + ret.steps);
}, },
fail: function(data, code) { fail: function (data, code) {
console.log('Subscription failed. Code: ' + code + '; Data: ' + data); console.error(`Failed to subscription. Code: ${code}, data: ${data}`);
}, },
}); });
``` ```
...@@ -240,13 +241,13 @@ unsubscribeStepCounter(): void ...@@ -240,13 +241,13 @@ unsubscribeStepCounter(): void
Unsubscribes from data changes of the step counter sensor. Unsubscribes from data changes of the step counter sensor.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
**Required permissions**: ohos.permission.ACTIVITY_MOTION **Required permissions**: ohos.permission.ACTIVITY_MOTION
**Example** **Example**
```js ```ts
sensor.unsubscribeStepCounter(); sensor.unsubscribeStepCounter();
``` ```
...@@ -257,7 +258,7 @@ subscribeBarometer(options: SubscribeBarometerOptions): void ...@@ -257,7 +258,7 @@ subscribeBarometer(options: SubscribeBarometerOptions): void
Subscribes to data changes of the barometer sensor. If this API is called multiple times for the same application, the last call takes effect. Subscribes to data changes of the barometer sensor. If this API is called multiple times for the same application, the last call takes effect.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
**Parameters** **Parameters**
...@@ -267,13 +268,13 @@ Subscribes to data changes of the barometer sensor. If this API is called multip ...@@ -267,13 +268,13 @@ Subscribes to data changes of the barometer sensor. If this API is called multip
**Example** **Example**
```js ```ts
sensor.subscribeBarometer({ sensor.subscribeBarometer({
success: function(ret) { success: function (ret) {
console.log('Get data value:' + ret.pressure); console.info('Succeeded in subscribing. Get data value:' + ret.pressure);
}, },
fail: function(data, code) { fail: function (data, code) {
console.log('Subscription failed. Code: ' + code + '; Data: ' + data); console.error(`Failed to subscription. Code: ${code}, data: ${data}`);
}, },
}); });
``` ```
...@@ -288,11 +289,11 @@ unsubscribeBarometer(): void ...@@ -288,11 +289,11 @@ unsubscribeBarometer(): void
Unsubscribes from data changes of the barometer sensor. Unsubscribes from data changes of the barometer sensor.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
**Example** **Example**
```js ```ts
sensor.unsubscribeBarometer(); sensor.unsubscribeBarometer();
``` ```
...@@ -303,7 +304,7 @@ sensor.unsubscribeBarometer(); ...@@ -303,7 +304,7 @@ sensor.unsubscribeBarometer();
Subscribes to data changes of the heart rate sensor. If this API is called multiple times for the same application, the last call takes effect. Subscribes to data changes of the heart rate sensor. If this API is called multiple times for the same application, the last call takes effect.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
**Required permissions**: ohos.permission.READ_HEALTH_DATA **Required permissions**: ohos.permission.READ_HEALTH_DATA
...@@ -315,13 +316,13 @@ Subscribes to data changes of the heart rate sensor. If this API is called multi ...@@ -315,13 +316,13 @@ Subscribes to data changes of the heart rate sensor. If this API is called multi
**Example** **Example**
```js ```ts
sensor.subscribeHeartRate({ sensor.subscribeHeartRate({
success: function(ret) { success: function (ret) {
console.log('Get heartrate value:' + ret.heartRate); console.info('Succeeded in subscribing. Get heartrate value:' + ret.heartRate);
}, },
fail: function(data, code) { fail: function (data, code) {
console.log('Subscription failed. Code: ' + code + '; Data: ' + data); console.error(`Failed to subscription. Code: ${code}, data: ${data}`);
}, },
}); });
``` ```
...@@ -336,13 +337,13 @@ unsubscribeHeartRate(): void ...@@ -336,13 +337,13 @@ unsubscribeHeartRate(): void
Unsubscribes from data changes of the heart rate sensor. Unsubscribes from data changes of the heart rate sensor.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
**Required permissions**: ohos.permission.READ_HEALTH_DATA **Required permissions**: ohos.permission.READ_HEALTH_DATA
**Example** **Example**
```js ```ts
sensor.unsubscribeHeartRate(); sensor.unsubscribeHeartRate();
``` ```
...@@ -352,7 +353,7 @@ sensor.unsubscribeHeartRate(); ...@@ -352,7 +353,7 @@ sensor.unsubscribeHeartRate();
Subscribes to changes of the wearing state of a wearable device. If this API is called multiple times for the same application, the last call takes effect. Subscribes to changes of the wearing state of a wearable device. If this API is called multiple times for the same application, the last call takes effect.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
**Parameters** **Parameters**
...@@ -362,13 +363,13 @@ Subscribes to changes of the wearing state of a wearable device. If this API is ...@@ -362,13 +363,13 @@ Subscribes to changes of the wearing state of a wearable device. If this API is
**Example** **Example**
```js ```ts
sensor.subscribeOnBodyState({ sensor.subscribeOnBodyState({
success: function(ret) { success: function (ret) {
console.log('Get on-body state value:' + ret.value); console.info('Succeeded in subscribing. Get on-body state value:' + ret.value);
}, },
fail: function(data, code) { fail: function (data, code) {
console.log('Subscription failed. Code: ' + code + '; Data: ' + data); console.error(`Failed to subscription. Code: ${code}, data: ${data}`);
}, },
}); });
``` ```
...@@ -382,11 +383,11 @@ unsubscribeOnBodyState(): void ...@@ -382,11 +383,11 @@ unsubscribeOnBodyState(): void
Unsubscribes from changes of the wearing state of a wearable device. Unsubscribes from changes of the wearing state of a wearable device.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
**Example** **Example**
```js ```ts
sensor.unsubscribeOnBodyState(); sensor.unsubscribeOnBodyState();
``` ```
...@@ -396,7 +397,7 @@ sensor.unsubscribeOnBodyState(); ...@@ -396,7 +397,7 @@ sensor.unsubscribeOnBodyState();
Obtains the wearing state of a wearable device. Obtains the wearing state of a wearable device.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
**Parameters** **Parameters**
...@@ -406,13 +407,13 @@ Obtains the wearing state of a wearable device. ...@@ -406,13 +407,13 @@ Obtains the wearing state of a wearable device.
**Example** **Example**
```js ```ts
sensor.getOnBodyState({ sensor.getOnBodyState({
success: function(ret) { success: function (ret) {
console.log('On body state: ' + ret.value); console.info('Succeeded in subscribing. On body state: ' + ret.value);
}, },
fail: function(data, code) { fail: function (data, code) {
console.log('Subscription failed. Code: ' + code + '; Data: ' + data); console.error(`Failed to subscription. Code: ${code}, data: ${data}`);
}, },
}); });
``` ```
...@@ -425,7 +426,7 @@ Subscribes to data changes of the device orientation sensor. ...@@ -425,7 +426,7 @@ Subscribes to data changes of the device orientation sensor.
If this API is called multiple times for the same application, the last call takes effect. However, this API cannot be called multiple times in one click event. If this API is called multiple times for the same application, the last call takes effect. However, this API cannot be called multiple times in one click event.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
**Parameters** **Parameters**
...@@ -435,17 +436,17 @@ If this API is called multiple times for the same application, the last call tak ...@@ -435,17 +436,17 @@ If this API is called multiple times for the same application, the last call tak
**Example** **Example**
```js ```ts
sensor.subscribeDeviceOrientation({ sensor.subscribeDeviceOrientation({
interval: 'normal', interval: 'normal',
success: function(ret) { success: function (ret) {
console.log('Alpha data: ' + ret.alpha); console.info('Succeeded in subscribing. Alpha data: ' + ret.alpha);
console.log('Beta data: ' + ret.beta); console.info('Succeeded in subscribing. Beta data: ' + ret.beta);
console.log('Gamma data: ' + ret.gamma); console.info('Succeeded in subscribing. Gamma data: ' + ret.gamma);
}, },
fail: function(data, code) { fail: function (data, code) {
console.error('Subscription failed. Code: ' + code + '; Data: ' + data); console.error(`Failed to subscription. Code: ${code}, data: ${data}`);
} }
}); });
``` ```
...@@ -458,11 +459,11 @@ unsubscribeDeviceOrientation(): void ...@@ -458,11 +459,11 @@ unsubscribeDeviceOrientation(): void
Unsubscribes from data changes of the device orientation sensor. Unsubscribes from data changes of the device orientation sensor.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
**Example** **Example**
```js ```ts
sensor.unsubscribeDeviceOrientation(); sensor.unsubscribeDeviceOrientation();
``` ```
...@@ -474,7 +475,7 @@ Subscribes to data changes of the gyroscope sensor. ...@@ -474,7 +475,7 @@ Subscribes to data changes of the gyroscope sensor.
If this API is called multiple times for the same application, the last call takes effect. However, this API cannot be called multiple times in one click event. If this API is called multiple times for the same application, the last call takes effect. However, this API cannot be called multiple times in one click event.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
**Required permissions**: ohos.permission.GYROSCOPE (a system permission) **Required permissions**: ohos.permission.GYROSCOPE (a system permission)
...@@ -486,16 +487,16 @@ If this API is called multiple times for the same application, the last call tak ...@@ -486,16 +487,16 @@ If this API is called multiple times for the same application, the last call tak
**Example** **Example**
```js ```ts
sensor.subscribeGyroscope({ sensor.subscribeGyroscope({
interval: 'normal', interval: 'normal',
success: function(ret) { success: function (ret) {
console.log('X-axis data: ' + ret.x); console.info('Succeeded in subscribing. X-axis data: ' + ret.x);
console.log('Y-axis data: ' + ret.y); console.info('Succeeded in subscribing. Y-axis data: ' + ret.y);
console.log('Z-axis data: ' + ret.z); console.info('Succeeded in subscribing. Z-axis data: ' + ret.z);
}, },
fail: function(data, code) { fail: function (data, code) {
console.error('Subscription failed. Code: ' + code + '; data: ' + data); console.error(`Failed to subscription. Code: ${code}, data: ${data}`);
} }
}); });
``` ```
...@@ -509,13 +510,13 @@ unsubscribeGyroscope(): void ...@@ -509,13 +510,13 @@ unsubscribeGyroscope(): void
Unsubscribes from data changes of the gyroscope sensor. Unsubscribes from data changes of the gyroscope sensor.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
**Required permissions**: ohos.permission.GYROSCOPE (a system permission) **Required permissions**: ohos.permission.GYROSCOPE (a system permission)
**Example** **Example**
```js ```ts
sensor.unsubscribeGyroscope(); sensor.unsubscribeGyroscope();
``` ```
...@@ -525,7 +526,7 @@ Defines the type of data to return for a subscription to the acceleration sensor ...@@ -525,7 +526,7 @@ Defines the type of data to return for a subscription to the acceleration sensor
**Required permissions**: ohos.permission.ACCELEROMETER **Required permissions**: ohos.permission.ACCELEROMETER
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| -------- | ----------------------------------------------- | ---- | ------------------------------------------------------------ | | -------- | ----------------------------------------------- | ---- | ------------------------------------------------------------ |
...@@ -539,7 +540,7 @@ Defines the type of data to include in an **AccelerometerResponse** object. ...@@ -539,7 +540,7 @@ Defines the type of data to include in an **AccelerometerResponse** object.
**Required permissions**: ohos.permission.ACCELEROMETER **Required permissions**: ohos.permission.ACCELEROMETER
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
| Name| Type | Mandatory| Description | | Name| Type | Mandatory| Description |
| ---- | ------ | ---- | ------------- | | ---- | ------ | ---- | ------------- |
...@@ -551,7 +552,7 @@ Defines the type of data to include in an **AccelerometerResponse** object. ...@@ -551,7 +552,7 @@ Defines the type of data to include in an **AccelerometerResponse** object.
Defines the type of data to return for a subscription to the compass sensor data. Defines the type of data to return for a subscription to the compass sensor data.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| ------- | ----------------------------------- | ---- | ------------------------------ | | ------- | ----------------------------------- | ---- | ------------------------------ |
...@@ -562,7 +563,7 @@ Defines the type of data to return for a subscription to the compass sensor data ...@@ -562,7 +563,7 @@ Defines the type of data to return for a subscription to the compass sensor data
Defines the type of data to include in a **CompassResponse** object. Defines the type of data to include in a **CompassResponse** object.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| --------- | ------ | ---- | -------------------- | | --------- | ------ | ---- | -------------------- |
...@@ -572,7 +573,7 @@ Defines the type of data to include in a **CompassResponse** object. ...@@ -572,7 +573,7 @@ Defines the type of data to include in a **CompassResponse** object.
Defines the type of data to return for a subscription to the proximity sensor data. Defines the type of data to return for a subscription to the proximity sensor data.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| ------- | --------------------------------------- | ---- | ---------------------------------- | | ------- | --------------------------------------- | ---- | ---------------------------------- |
...@@ -583,7 +584,7 @@ Defines the type of data to return for a subscription to the proximity sensor da ...@@ -583,7 +584,7 @@ Defines the type of data to return for a subscription to the proximity sensor da
Defines the type of data to include in a **ProximityResponse** object. Defines the type of data to include in a **ProximityResponse** object.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| -------- | ------ | ---- | ------------------------------------------ | | -------- | ------ | ---- | ------------------------------------------ |
...@@ -593,18 +594,18 @@ Defines the type of data to include in a **ProximityResponse** object. ...@@ -593,18 +594,18 @@ Defines the type of data to include in a **ProximityResponse** object.
Defines the type of data to return for a subscription to the ambient light sensor data. Defines the type of data to return for a subscription to the ambient light sensor data.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| ------- | ------------------------------- | ---- | ------------------------------ | | ------- | ------------------------------- | ---- | ------------------------------ |
| success | [LightResponse](#lightresponse) | Yes | Called when the ambient light sensor data changes| | success | [LightResponse](#lightresponse) | Yes | Called when the ambient light sensor data changes.|
| fail | Function | No | Callback upon an API call failure. | | fail | Function | No | Callback upon an API call failure. |
## LightResponse ## LightResponse
Defines the type of data to include in a **LightResponse** object. Defines the type of data to include in a **LightResponse** object.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| --------- | ------ | ---- | --------------------- | | --------- | ------ | ---- | --------------------- |
...@@ -616,7 +617,7 @@ Defines the type of data to return for a subscription to the step counter sensor ...@@ -616,7 +617,7 @@ Defines the type of data to return for a subscription to the step counter sensor
**Required permissions**: ohos.permission.ACTIVITY_MOTION **Required permissions**: ohos.permission.ACTIVITY_MOTION
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| ------- | ------------------------------------------- | ---- | -------------------------------- | | ------- | ------------------------------------------- | ---- | -------------------------------- |
...@@ -629,7 +630,7 @@ Defines the type of data to include in a **StepCounterResponse** object. ...@@ -629,7 +630,7 @@ Defines the type of data to include in a **StepCounterResponse** object.
**Required permissions**: ohos.permission.ACTIVITY_MOTION **Required permissions**: ohos.permission.ACTIVITY_MOTION
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| ----- | ------ | ---- | -------------------------------- | | ----- | ------ | ---- | -------------------------------- |
...@@ -639,7 +640,7 @@ Defines the type of data to include in a **StepCounterResponse** object. ...@@ -639,7 +640,7 @@ Defines the type of data to include in a **StepCounterResponse** object.
Defines the type of data to return for a subscription to the barometer sensor data. Defines the type of data to return for a subscription to the barometer sensor data.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| ------- | --------------------------------------- | ---- | -------------------------------- | | ------- | --------------------------------------- | ---- | -------------------------------- |
...@@ -650,7 +651,7 @@ Defines the type of data to return for a subscription to the barometer sensor da ...@@ -650,7 +651,7 @@ Defines the type of data to return for a subscription to the barometer sensor da
Defines the type of data to include in a **BarometerResponse** object. Defines the type of data to include in a **BarometerResponse** object.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| -------- | ------ | ---- | ---------------------- | | -------- | ------ | ---- | ---------------------- |
...@@ -662,7 +663,7 @@ Defines the type of data to return for a subscription to the heart rate sensor d ...@@ -662,7 +663,7 @@ Defines the type of data to return for a subscription to the heart rate sensor d
**Required permissions**: ohos.permission.READ_HEALTH_DATA **Required permissions**: ohos.permission.READ_HEALTH_DATA
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| ------- | --------------------------------------- | ---- | ----------------------------------------------- | | ------- | --------------------------------------- | ---- | ----------------------------------------------- |
...@@ -675,7 +676,7 @@ Defines the type of data to include in a **HeartRateResponse** object. ...@@ -675,7 +676,7 @@ Defines the type of data to include in a **HeartRateResponse** object.
**Required permissions**: ohos.permission.READ_HEALTH_DATA **Required permissions**: ohos.permission.READ_HEALTH_DATA
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| --------- | ------ | ---- | -------- | | --------- | ------ | ---- | -------- |
...@@ -685,7 +686,7 @@ Defines the type of data to include in a **HeartRateResponse** object. ...@@ -685,7 +686,7 @@ Defines the type of data to include in a **HeartRateResponse** object.
Defines the type of data to return for a subscription to the wearing state changes. Defines the type of data to return for a subscription to the wearing state changes.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| ------- | ------------------------------------------- | ---- | -------------------------- | | ------- | ------------------------------------------- | ---- | -------------------------- |
...@@ -696,7 +697,7 @@ Defines the type of data to return for a subscription to the wearing state chang ...@@ -696,7 +697,7 @@ Defines the type of data to return for a subscription to the wearing state chang
Defines the wearing state. Defines the wearing state.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| ----- | ------- | ---- | ------------ | | ----- | ------- | ---- | ------------ |
...@@ -706,7 +707,7 @@ Defines the wearing state. ...@@ -706,7 +707,7 @@ Defines the wearing state.
Defines the type of data to return for obtaining the wearing state. Defines the type of data to return for obtaining the wearing state.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| -------- | ------------------------------------------- | ---- | ------------------------ | | -------- | ------------------------------------------- | ---- | ------------------------ |
...@@ -718,7 +719,7 @@ Defines the wearing state. ...@@ -718,7 +719,7 @@ Defines the wearing state.
Defines the type of data to return for a subscription to the device orientation sensor data. Defines the type of data to return for a subscription to the device orientation sensor data.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | | -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ |
...@@ -730,7 +731,7 @@ Defines the type of data to return for a subscription to the device orientation ...@@ -730,7 +731,7 @@ Defines the type of data to return for a subscription to the device orientation
Defines the type of data to include in a **DeviceOrientationResponse** object. Defines the type of data to include in a **DeviceOrientationResponse** object.
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| ----- | ------ | ---- | ------------------------------------------------------------ | | ----- | ------ | ---- | ------------------------------------------------------------ |
...@@ -744,7 +745,7 @@ Defines the type of data to return for a subscription to the gyroscope sensor da ...@@ -744,7 +745,7 @@ Defines the type of data to return for a subscription to the gyroscope sensor da
**Required permissions**: ohos.permission.GYROSCOPE **Required permissions**: ohos.permission.GYROSCOPE
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | | -------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
...@@ -758,7 +759,7 @@ Defines the type of data to include in a **GyroscopeResponse** object. ...@@ -758,7 +759,7 @@ Defines the type of data to include in a **GyroscopeResponse** object.
**Required permissions**: ohos.permission.GYROSCOPE **Required permissions**: ohos.permission.GYROSCOPE
**System capability**: SystemCapability.Sensors.Sensor **System capability**: SystemCapability.Sensors.Sensor.Lite
| Name| Type | Mandatory| Description | | Name| Type | Mandatory| Description |
| ---- | ------ | ---- | ----------------- | | ---- | ------ | ---- | ----------------- |
......
...@@ -26,7 +26,7 @@ Triggers device vibration. ...@@ -26,7 +26,7 @@ Triggers device vibration.
**Required permissions**: ohos.permission.VIBRATE **Required permissions**: ohos.permission.VIBRATE
**System capability**: SystemCapability.Sensors.MiscDevice **System capability**: SystemCapability.Sensors.MiscDevice.Lite
**Parameters** **Parameters**
...@@ -36,17 +36,17 @@ Triggers device vibration. ...@@ -36,17 +36,17 @@ Triggers device vibration.
**Example** **Example**
```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');
} }
}); });
``` ```
...@@ -57,7 +57,7 @@ Defines the vibration options. ...@@ -57,7 +57,7 @@ Defines the vibration options.
**Required permissions**: ohos.permission.VIBRATE **Required permissions**: ohos.permission.VIBRATE
**System capability**: SystemCapability.Sensors.MiscDevice **System capability**: SystemCapability.Sensors.MiscDevice.Lite
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| -------- | -------- | ---- | ------------------------------------------------------------ | | -------- | -------- | ---- | ------------------------------------------------------------ |
......
...@@ -9,7 +9,7 @@ The **vibrator** module provides APIs for starting or stopping vibration. ...@@ -9,7 +9,7 @@ The **vibrator** module provides APIs for starting or stopping vibration.
## Modules to Import ## Modules to Import
```js ```ts
import vibrator from '@ohos.vibrator'; import vibrator from '@ohos.vibrator';
``` ```
...@@ -41,24 +41,25 @@ For details about the error codes, see [Vibrator Error Codes](../errorcodes/erro ...@@ -41,24 +41,25 @@ For details about the error codes, see [Vibrator Error Codes](../errorcodes/erro
**Example** **Example**
```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 @@ For details about the error codes, see [Vibrator Error Codes](../errorcodes/erro ...@@ -95,24 +96,25 @@ For details about the error codes, see [Vibrator Error Codes](../errorcodes/erro
**Example** **Example**
```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 @@ Stops vibration in the specified mode. This API uses an asynchronous callback to ...@@ -133,40 +135,41 @@ Stops vibration in the specified mode. This API uses an asynchronous callback to
**Example** **Example**
```js ```ts
import vibrator from '@ohos.vibrator'; import vibrator from '@ohos.vibrator';
try { try {
// Start vibration at a fixed duration. // Start vibration at a fixed duration.
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 {
// Stop vibration in VIBRATOR_STOP_MODE_TIME mode. // Stop vibration in VIBRATOR_STOP_MODE_TIME mode.
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 @@ Stops vibration in the specified mode. This API uses a promise to return the res ...@@ -192,36 +195,37 @@ Stops vibration in the specified mode. This API uses a promise to return the res
**Example** **Example**
```js ```ts
import vibrator from '@ohos.vibrator'; import vibrator from '@ohos.vibrator';
try { try {
// Start vibration at a fixed duration. // Start vibration at a fixed duration.
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 {
// Stop vibration in VIBRATOR_STOP_MODE_TIME mode. // Stop vibration in VIBRATOR_STOP_MODE_TIME mode.
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 @@ Stops vibration in all modes. This API uses an asynchronous callback to return t ...@@ -241,40 +245,41 @@ Stops vibration in all modes. This API uses an asynchronous callback to return t
**Example** **Example**
```js ```ts
import vibrator from '@ohos.vibrator'; import vibrator from '@ohos.vibrator';
try { try {
// Start vibration at a fixed duration. // Start vibration at a fixed duration.
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 {
// Stop vibration in all modes. // Stop vibration in all modes.
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 @@ Stops vibration in all modes. This API uses a promise to return the result. ...@@ -294,36 +299,37 @@ Stops vibration in all modes. This API uses a promise to return the result.
**Example** **Example**
```js ```ts
import vibrator from '@ohos.vibrator'; import vibrator from '@ohos.vibrator';
try { try {
// Start vibration at a fixed duration. // Start vibration at a fixed duration.
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 {
// Stop vibration in all modes. // Stop vibration in all modes.
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 @@ Checks whether the passed effect ID is supported. This API uses an asynchronous ...@@ -342,40 +348,41 @@ Checks whether the passed effect ID is supported. This API uses an asynchronous
**Example** **Example**
```js ```ts
import vibrator from '@ohos.vibrator'; import vibrator from '@ohos.vibrator';
try { try {
// Check whether 'haptic.clock.timer' is supported. // Check whether 'haptic.clock.timer' is supported.
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({ // To use startVibration, you must configure the ohos.permission.VIBRATE permission. vibrator.startVibration({ // To use startVibration, you must configure the ohos.permission.VIBRATE permission.
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 @@ Checks whether the passed effect ID is supported. This API uses a promise to ret ...@@ -399,36 +406,37 @@ Checks whether the passed effect ID is supported. This API uses a promise to ret
**Example** **Example**
```js ```ts
import vibrator from '@ohos.vibrator'; import vibrator from '@ohos.vibrator';
try { try {
// Check whether 'haptic.clock.timer' is supported. // Check whether 'haptic.clock.timer' is supported.
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 @@ Describes the vibration with a preset effect. ...@@ -489,7 +497,7 @@ Describes the vibration with a preset effect.
## VibrateFromFile<sup>10+</sup> ## 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 **System capability**: SystemCapability.Sensors.MiscDevice
...@@ -565,13 +573,13 @@ This API is deprecated since API version 9. You are advised to use [vibrator.sta ...@@ -565,13 +573,13 @@ This API is deprecated since API version 9. You are advised to use [vibrator.sta
**Example** **Example**
```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 @@ This API is deprecated since API version 9. You are advised to use [vibrator.sta ...@@ -594,15 +602,15 @@ This API is deprecated since API version 9. You are advised to use [vibrator.sta
**Example** **Example**
```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 @@ This API is deprecated since API version 9. You are advised to use [vibrator.sta ...@@ -631,13 +639,13 @@ This API is deprecated since API version 9. You are advised to use [vibrator.sta
**Example** **Example**
```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 @@ This API is deprecated since API version 9. You are advised to use [vibrator.sta ...@@ -661,15 +669,15 @@ This API is deprecated since API version 9. You are advised to use [vibrator.sta
**Example** **Example**
```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 @@ This API is deprecated since API version 9. You are advised to use [vibrator.sto ...@@ -697,22 +705,22 @@ This API is deprecated since API version 9. You are advised to use [vibrator.sto
**Example** **Example**
```js ```ts
// Start vibration based on the specified effect ID. // Start vibration based on the specified effect ID.
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');
} }
}) })
// Stop vibration in VIBRATOR_STOP_MODE_PRESET mode. // Stop vibration in VIBRATOR_STOP_MODE_PRESET mode.
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 @@ This API is deprecated since API version 9. You are advised to use [vibrator.sto ...@@ -736,21 +744,21 @@ This API is deprecated since API version 9. You are advised to use [vibrator.sto
**Example** **Example**
```js ```ts
// Start vibration based on the specified effect ID. // Start vibration based on the specified effect ID.
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');
} }
}) })
// Stop vibration in VIBRATOR_STOP_MODE_PRESET mode. // Stop vibration in VIBRATOR_STOP_MODE_PRESET mode.
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.
先完成此消息的编辑!
想要评论请 注册