diff --git a/zh-cn/application-dev/device/sensor-guidelines.md b/zh-cn/application-dev/device/sensor-guidelines.md index 0e86df0246bef21951abdb179f916910c897f0fa..cf0cde22baf843011a03a379d7557e14e41cd5e1 100644 --- a/zh-cn/application-dev/device/sensor-guidelines.md +++ b/zh-cn/application-dev/device/sensor-guidelines.md @@ -25,13 +25,14 @@ 1. 导入模块。 ```ts - import sensor from "@ohos.sensor"; + import sensor from '@ohos.sensor'; + import { BusinessError } from '@ohos.base'; ``` 2. 查询设备支持的所有传感器的参数。 ```ts - sensor.getSensorList(function (error, data) { + sensor.getSensorList((error: BusinessError, data: Array) => { if (error) { console.info('getSensorList failed'); } else { @@ -54,19 +55,19 @@ - 通过on()接口,实现对传感器的持续监听,传感器上报周期interval设置为100000000纳秒。 ```ts - sensor.on(sensor.SensorId.ACCELEROMETER, function (data) { + sensor.on(sensor.SensorId.ACCELEROMETER, (data: sensor.AccelerometerResponse) => { console.info("Succeeded in obtaining data. x: " + data.x + " y: " + data.y + " z: " + data.z); - }, {'interval': 100000000}); + }, { interval: 100000000 }); ``` ![输入图片说明](figures/002.png) - 通过once()接口,实现对传感器的一次监听。 - ```ts - sensor.once(sensor.SensorId.ACCELEROMETER, function (data) { + ```ts + sensor.once(sensor.SensorId.ACCELEROMETER, (data: sensor.AccelerometerResponse) => { console.info("Succeeded in obtaining data. x: " + data.x + " y: " + data.y + " z: " + data.z); - }); + }); ``` ![输入图片说明](figures/003.png) diff --git a/zh-cn/application-dev/device/vibrator-guidelines.md b/zh-cn/application-dev/device/vibrator-guidelines.md index d04b5036ddaf67718f87098661c0d6ce4e4af524..6eae3be58a093f0e338ed6ccd99ca13d78b244d4 100644 --- a/zh-cn/application-dev/device/vibrator-guidelines.md +++ b/zh-cn/application-dev/device/vibrator-guidelines.md @@ -112,15 +112,17 @@ Json文件共包含2个属性。 ```ts import vibrator from '@ohos.vibrator'; +import { BusinessError } from '@ohos.base'; try { - vibrator.startVibration({ // 使用startVibration需要添加ohos.permission.VIBRATE权限 + // 使用startVibration需要添加ohos.permission.VIBRATE权限 + vibrator.startVibration({ type: 'time', duration: 1000, }, { id: 0, usage: 'alarm' - }, (error) => { + }, (error: BusinessError) => { if (error) { console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); return; @@ -128,7 +130,8 @@ try { console.info('Succeed in starting vibration.'); }); } catch (err) { - console.error(`An unexpected error occurred. Code: ${err.code}, message: ${err.message}`); + let e: BusinessError = err as BusinessError; + console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); } ``` @@ -136,10 +139,11 @@ try { ```ts import vibrator from '@ohos.vibrator'; +import { BusinessError } from '@ohos.base'; try { // 按照VIBRATOR_STOP_MODE_TIME模式停止振动, 使用stopVibration需要添加ohos.permission.VIBRATE权限 - vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME, function (error) { + vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME, (error: BusinessError) => { if (error) { console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); return; @@ -147,7 +151,8 @@ try { console.info('Succeeded in stopping vibration.'); }) } catch (err) { - console.error(`An unexpected error occurred. Code: ${err.code}, message: ${err.message}`); + let e: BusinessError = err as BusinessError; + console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); } ``` @@ -155,7 +160,8 @@ try { ```ts import vibrator from '@ohos.vibrator'; -// 使用startVibration、stopVibration需要添加ohos.permission.VIBRATE权限 +import { BusinessError } from '@ohos.base'; + try { vibrator.startVibration({ type: 'time', @@ -163,7 +169,7 @@ try { }, { id: 0, usage: 'alarm' - }, (error) => { + }, (error: BusinessError) => { if (error) { console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); return; @@ -171,7 +177,7 @@ try { console.info('Succeed in starting vibration'); }); // 停止所有类型的马达振动 - vibrator.stopVibration(function (error) { + vibrator.stopVibration((error: BusinessError) => { if (error) { console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); return; @@ -179,7 +185,8 @@ try { console.info('Succeed in stopping vibration'); }) } catch (error) { - console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`); + let e: BusinessError = error as BusinessError; + console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); } ``` @@ -187,10 +194,11 @@ try { ```ts import vibrator from '@ohos.vibrator'; +import { BusinessError } from '@ohos.base'; try { // 查询是否支持'haptic.clock.timer' - vibrator.isSupportEffect('haptic.clock.timer', function (err, state) { + vibrator.isSupportEffect('haptic.clock.timer', (err: BusinessError, state: boolean) => { if (err) { console.error(`Failed to query effect. Code: ${err.code}, message: ${err.message}`); return; @@ -198,13 +206,14 @@ try { console.info('Succeed in querying effect'); if (state) { try { - vibrator.startVibration({ // 使用startVibration需要添加ohos.permission.VIBRATE权限 + // 使用startVibration需要添加ohos.permission.VIBRATE权限 + vibrator.startVibration({ type: 'preset', effectId: 'haptic.clock.timer', count: 1, }, { usage: 'unknown' - }, (error) => { + }, (error: BusinessError) => { if (error) { console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); } else { @@ -212,12 +221,14 @@ try { } }); } catch (error) { - console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`); + let e: BusinessError = error as BusinessError; + console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); } } }) } catch (error) { - console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`); + let e: BusinessError = error as BusinessError; + console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); } ``` @@ -225,20 +236,22 @@ try { ```ts import vibrator from '@ohos.vibrator'; +import { BusinessError } from '@ohos.base'; +import resourceManager from '@ohos.resourceManager'; // 获取振动文件资源描述符 -async function getRawfileFd(fileName) { - let rawFd = await globalThis.getContext().resourceManager.getRawFd(fileName); +async function getRawfileFd(fileName: string): Promise { + let rawFd = await getContext().resourceManager.getRawFd(fileName); return rawFd; } // 关闭振动文件资源描述符 -async function closeRawfileFd(fileName) { - await globalThis.getContext().resourceManager.closeRawFd(fileName) +async function closeRawfileFd(fileName: string): Promise { + await getContext().resourceManager.closeRawFd(fileName) } // 播放自定义振动,使用startVibration、stopVibration需要添加ohos.permission.VIBRATE权限 -async function playCustomHaptic(fileName) { +async function playCustomHaptic(fileName: string): Promise { try { let rawFd = await getRawfileFd(fileName); vibrator.startVibration({ @@ -248,10 +261,10 @@ async function playCustomHaptic(fileName) { usage: "alarm" }).then(() => { console.info('Succeed in starting vibration'); - }, (error) => { + }, (error: BusinessError) => { console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); }); - vibrator.stopVibration(function (error) { + vibrator.stopVibration((error: BusinessError) => { if (error) { console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); return; @@ -260,7 +273,8 @@ async function playCustomHaptic(fileName) { }) await closeRawfileFd(fileName); } catch (error) { - console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`); + let e: BusinessError = error as BusinessError; + console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); } } ``` diff --git a/zh-cn/application-dev/reference/apis/js-apis-system-sensor.md b/zh-cn/application-dev/reference/apis/js-apis-system-sensor.md index cd5336f77bd2790f53e8901d2745cd2e423ba3a1..b2d267a33562302267cb96bca65a4ce38767c2a3 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-system-sensor.md +++ b/zh-cn/application-dev/reference/apis/js-apis-system-sensor.md @@ -38,17 +38,21 @@ import sensor from '@system.sensor'; **示例:** ```ts -sensor.subscribeAccelerometer({ +import sensor from '@system.sensor'; +import { AccelerometerResponse, subscribeAccelerometerOptions } from '@system.sensor'; + +let accelerometerOptions: subscribeAccelerometerOptions = { interval: 'normal', - success: function (ret) { + success: (ret: AccelerometerResponse) => { console.info('Succeeded in subscribing. X-axis data: ' + ret.x); console.info('Succeeded in subscribing. Y-axis data: ' + ret.y); console.info('Succeeded in subscribing. Z-axis data: ' + ret.z); }, - fail: function (data, code) { + fail: (data: string, code: number) => { console.error(`Failed to subscription. Code: ${code}, data: ${data}`); }, -}); +}; +sensor.subscribeAccelerometer(accelerometerOptions); ``` > **说明:** @@ -87,14 +91,18 @@ sensor.unsubscribeAccelerometer(); **示例:** ```ts -sensor.subscribeCompass({ - success: function (ret) { +import sensor from '@system.sensor'; +import { CompassResponse, SubscribeCompassOptions } from '@system.sensor'; + +let subscribeCompassOptions: SubscribeCompassOptions = { + success: (ret: CompassResponse) => { console.info('Succeeded in subscribing. Get data direction:' + ret.direction); }, - fail: function (data, code) { + fail: (data: string, code: number) => { console.error(`Failed to subscription. Code: ${code}, data: ${data}`); }, -}); +}; +sensor.subscribeCompass(subscribeCompassOptions); ``` > **说明:** @@ -131,15 +139,18 @@ sensor.unsubscribeCompass(); **示例:** ```ts -sensor.subscribeProximity({ - success: function (ret) { +import sensor from '@system.sensor'; +import { ProximityResponse, SubscribeProximityOptions } from '@system.sensor'; + +let subscribeProximityOptions: SubscribeProximityOptions = { + success: (ret: ProximityResponse) => { console.info('Succeeded in subscribing. Get data distance:' + ret.distance); }, - fail: function (data, code) { + fail: (data: string, code: number) => { console.error(`Failed to subscription. Code: ${code}, data: ${data}`); - }, -}); +}; +sensor.subscribeProximity(subscribeProximityOptions); ``` > **说明:** @@ -176,14 +187,18 @@ sensor.unsubscribeProximity(); **示例:** ```ts -sensor.subscribeLight({ - success: function (ret) { +import sensor from '@system.sensor'; +import { LightResponse, SubscribeLightOptions } from '@system.sensor'; + +let subscribeLightOptions: SubscribeLightOptions = { + success: (ret: LightResponse) => { console.info('Succeeded in subscribing. Get data intensity:' + ret.intensity); }, - fail: function (data, code) { + fail: (data: string, code: number) => { console.error(`Failed to subscription. Code: ${code}, data: ${data}`); }, -}); +}; +sensor.subscribeLight(subscribeLightOptions); ``` > **说明:** @@ -222,14 +237,18 @@ sensor.unsubscribeLight(); **示例:** ```ts -sensor.subscribeStepCounter({ - success: function (ret) { +import sensor from '@system.sensor'; +import { StepCounterResponse, SubscribeStepCounterOptions } from '@system.sensor'; + +let subscribeStepCounterOptions: SubscribeStepCounterOptions = { + success: (ret: StepCounterResponse) => { console.info('Succeeded in subscribing. Get step value:' + ret.steps); }, - fail: function (data, code) { + fail: (data: string, code: number) => { console.error(`Failed to subscription. Code: ${code}, data: ${data}`); }, -}); +}; +sensor.subscribeStepCounter(subscribeStepCounterOptions); ``` > **说明:** @@ -269,14 +288,18 @@ subscribeBarometer(options: SubscribeBarometerOptions): void **示例:** ```ts -sensor.subscribeBarometer({ - success: function (ret) { +import sensor from '@system.sensor'; +import { BarometerResponse, SubscribeBarometerOptions } from '@system.sensor'; + +let subscribeBarometerOptions: SubscribeBarometerOptions = { + success: (ret: BarometerResponse) => { console.info('Succeeded in subscribing. Get data value:' + ret.pressure); }, - fail: function (data, code) { + fail: (data: string, code: number) => { console.error(`Failed to subscription. Code: ${code}, data: ${data}`); }, -}); +}; +sensor.subscribeBarometer(subscribeBarometerOptions); ``` > **说明:** @@ -317,14 +340,18 @@ sensor.unsubscribeBarometer(); **示例:** ```ts -sensor.subscribeHeartRate({ - success: function (ret) { +import sensor from '@system.sensor'; +import { HeartRateResponse, SubscribeHeartRateOptions } from '@system.sensor'; + +let subscribeHeartRateOptions: SubscribeHeartRateOptions = { + success: (ret: HeartRateResponse) => { console.info('Succeeded in subscribing. Get heartrate value:' + ret.heartRate); }, - fail: function (data, code) { + fail: (data: string, code: number) => { console.error(`Failed to subscription. Code: ${code}, data: ${data}`); }, -}); +}; +sensor.subscribeHeartRate(subscribeHeartRateOptions); ``` > **说明:** @@ -364,14 +391,18 @@ sensor.unsubscribeHeartRate(); **示例:** ```ts -sensor.subscribeOnBodyState({ - success: function (ret) { +import sensor from '@system.sensor'; +import { OnBodyStateResponse, SubscribeOnBodyStateOptions } from '@system.sensor'; + +let subscribeOnBodyStateOptions: SubscribeOnBodyStateOptions = { + success: (ret: OnBodyStateResponse) => { console.info('Succeeded in subscribing. Get on-body state value:' + ret.value); }, - fail: function (data, code) { + fail: (data: string, code: number) => { console.error(`Failed to subscription. Code: ${code}, data: ${data}`); }, -}); +}; +sensor.subscribeOnBodyState(subscribeOnBodyStateOptions); ``` > **说明:** @@ -408,14 +439,18 @@ sensor.unsubscribeOnBodyState(); **示例:** ```ts -sensor.getOnBodyState({ - success: function (ret) { +import sensor from '@system.sensor'; +import { OnBodyStateResponse, GetOnBodyStateOptions } from '@system.sensor'; + +let getOnBodyStateOptions: GetOnBodyStateOptions = { + success: (ret: OnBodyStateResponse) => { console.info('Succeeded in subscribing. On body state: ' + ret.value); }, - fail: function (data, code) { + fail: (data: string, code: number) => { console.error(`Failed to subscription. Code: ${code}, data: ${data}`); }, -}); +}; +sensor.getOnBodyState(getOnBodyStateOptions); ``` ## sensor.subscribeDeviceOrientation6+ @@ -437,17 +472,21 @@ sensor.getOnBodyState({ **示例:** ```ts -sensor.subscribeDeviceOrientation({ +import sensor from '@system.sensor'; +import { DeviceOrientationResponse, SubscribeDeviceOrientationOptions } from '@system.sensor'; + +let subscribeDeviceOrientationOptions: SubscribeDeviceOrientationOptions = { interval: 'normal', - success: function (ret) { + success: (ret: DeviceOrientationResponse) => { console.info('Succeeded in subscribing. Alpha data: ' + ret.alpha); console.info('Succeeded in subscribing. Beta data: ' + ret.beta); console.info('Succeeded in subscribing. Gamma data: ' + ret.gamma); }, - fail: function (data, code) { + fail: (data: string, code: number) => { console.error(`Failed to subscription. Code: ${code}, data: ${data}`); - } -}); + } +}; +sensor.subscribeDeviceOrientation(subscribeDeviceOrientationOptions); ``` > **说明:** @@ -488,17 +527,21 @@ sensor.unsubscribeDeviceOrientation(); **示例:** ```ts -sensor.subscribeGyroscope({ +import sensor from '@system.sensor'; +import { GyroscopeResponse, SubscribeGyroscopeOptions } from '@system.sensor'; + +let subscribeGyroscopeOptions: SubscribeGyroscopeOptions = { interval: 'normal', - success: function (ret) { + success: (ret: GyroscopeResponse) => { console.info('Succeeded in subscribing. X-axis data: ' + ret.x); console.info('Succeeded in subscribing. Y-axis data: ' + ret.y); console.info('Succeeded in subscribing. Z-axis data: ' + ret.z); }, - fail: function (data, code) { + fail: (data: string, code: number) => { console.error(`Failed to subscription. Code: ${code}, data: ${data}`); } -}); +}; +sensor.subscribeGyroscope(subscribeGyroscopeOptions); ``` > **说明:** diff --git a/zh-cn/application-dev/reference/apis/js-apis-system-vibrate.md b/zh-cn/application-dev/reference/apis/js-apis-system-vibrate.md index 6eced16953a0283befe2206102d05316cf2703f8..133ce92544baa66cc6023f71068bfbbf76153d3e 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-system-vibrate.md +++ b/zh-cn/application-dev/reference/apis/js-apis-system-vibrate.md @@ -37,18 +37,22 @@ import vibrator from '@system.vibrator'; **示例:** ```ts -vibrator.vibrate({ +import vibrator from '@system.vibrator'; +import { VibrateOptions } from '@system.vibrator'; + +let vibrateOptions: VibrateOptions = { mode: 'short', - success: function() { + success: () => { console.info('Succeed in vibrating'); }, - fail: function(data, code) { + fail: (data: string, code: number) => { console.info(`Failed to vibrate. Data: ${data}, code: ${code}`); }, - complete: function() { + complete: () => { console.info('completed in vibrating'); } -}); +}; +vibrator.vibrate(vibrateOptions); ``` ## VibrateOptions diff --git a/zh-cn/application-dev/reference/apis/js-apis-vibrator.md b/zh-cn/application-dev/reference/apis/js-apis-vibrator.md index 247291bf59a7e2e236a2789c7d1f209e69b8fac8..348752c83570e0b4b4923dc6fd0d90c5e073cc5f 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-vibrator.md +++ b/zh-cn/application-dev/reference/apis/js-apis-vibrator.md @@ -43,6 +43,7 @@ startVibration(effect: VibrateEffect, attribute: VibrateAttribute, callback: Asy ```ts import vibrator from '@ohos.vibrator'; +import { BusinessError } from '@ohos.base'; try { vibrator.startVibration({ @@ -51,7 +52,7 @@ try { }, { id: 0, usage: 'alarm' - }, (error) => { + }, (error: BusinessError) => { if (error) { console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); return; @@ -59,7 +60,8 @@ try { console.info('Succeed in starting vibration'); }); } catch (err) { - console.error(`An unexpected error occurred. Code: ${err.code}, message: ${err.message}`); + let e: BusinessError = err as BusinessError; + console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); } ``` @@ -98,6 +100,7 @@ startVibration(effect: VibrateEffect, attribute: VibrateAttribute): Promise<v ```ts import vibrator from '@ohos.vibrator'; +import { BusinessError } from '@ohos.base'; try { vibrator.startVibration({ @@ -108,11 +111,12 @@ try { usage: 'alarm' }).then(() => { console.info('Succeed in starting vibration'); - }, (error) => { + }, (error: BusinessError) => { console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); }); } catch (err) { - console.error(`An unexpected error occurred. Code: ${err.code}, message: ${err.message}`); + let e: BusinessError = err as BusinessError; + console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); } ``` @@ -137,6 +141,7 @@ stopVibration(stopMode: VibratorStopMode, callback: AsyncCallback<void>): ```ts import vibrator from '@ohos.vibrator'; +import { BusinessError } from '@ohos.base'; try { // 按照固定时长振动 @@ -146,7 +151,7 @@ try { }, { id: 0, usage: 'alarm' - }, (error) => { + }, (error: BusinessError) => { if (error) { console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); return; @@ -154,12 +159,13 @@ try { console.info('Succeed in starting vibration'); }); } catch (err) { - console.error(`An unexpected error occurred. Code: ${err.code}, message: ${err.message}`); + let e: BusinessError = err as BusinessError; + console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); } try { // 按照VIBRATOR_STOP_MODE_TIME模式停止振动 - vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME, function (error) { + vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME, (error: BusinessError) => { if (error) { console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); return; @@ -167,7 +173,8 @@ try { console.info('Succeed in stopping vibration'); }) } catch (err) { - console.error(`An unexpected error occurred. Code: ${err.code}, message: ${err.message}`); + let e: BusinessError = err as BusinessError; + console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); } ``` @@ -197,33 +204,36 @@ stopVibration(stopMode: VibratorStopMode): Promise<void> ```ts import vibrator from '@ohos.vibrator'; +import { BusinessError } from '@ohos.base'; try { // 按照固定时长振动 vibrator.startVibration({ type: 'time', - duration: 1000 + duration: 1000, }, { id: 0, usage: 'alarm' }).then(() => { console.info('Succeed in starting vibration'); - }, (error) => { + }, (error: BusinessError) => { console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); }); } catch (err) { - console.error(`An unexpected error occurred. Code: ${err.code}, message: ${err.message}`); + let e: BusinessError = err as BusinessError; + console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); } try { // 按照VIBRATOR_STOP_MODE_TIME模式停止振动 vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(() => { console.info('Succeed in stopping vibration'); - }, (error) => { + }, (error: BusinessError) => { console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); }); } catch (err) { - console.error(`An unexpected error occurred. Code: ${err.code}, message: ${err.message}`); + let e: BusinessError = err as BusinessError; + console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); } ``` @@ -247,6 +257,7 @@ stopVibration(callback: AsyncCallback<void>): void ```ts import vibrator from '@ohos.vibrator'; +import { BusinessError } from '@ohos.base'; try { // 按照固定时长振动 @@ -256,7 +267,7 @@ try { }, { id: 0, usage: 'alarm' - }, (error) => { + }, (error: BusinessError) => { if (error) { console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); return; @@ -264,12 +275,13 @@ try { console.info('Succeed in starting vibration'); }); } catch (error) { - console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`); + let e: BusinessError = error as BusinessError; + console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); } try { // 停止所有模式的马达振动 - vibrator.stopVibration(function (error) { + vibrator.stopVibration((error: BusinessError) => { if (error) { console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); return; @@ -277,7 +289,8 @@ try { console.info('Succeed in stopping vibration'); }) } catch (error) { - console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`); + let e: BusinessError = error as BusinessError; + console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); } ``` @@ -301,33 +314,36 @@ stopVibration(): Promise<void> ```ts import vibrator from '@ohos.vibrator'; +import { BusinessError } from '@ohos.base'; try { // 按照固定时长振动 vibrator.startVibration({ type: 'time', - duration: 1000 + duration: 1000, }, { id: 0, usage: 'alarm' }).then(() => { console.info('Succeed in starting vibration'); - }, (error) => { + }, (error: BusinessError) => { 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}`); + let e: BusinessError = error as BusinessError; + console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); } try { // 停止所有模式的马达振动 vibrator.stopVibration().then(() => { console.info('Succeed in stopping vibration'); - }, (error) => { + }, (error: BusinessError) => { console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`); }); } catch (error) { - console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`); + let e: BusinessError = error as BusinessError; + console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); } ``` @@ -350,10 +366,11 @@ isSupportEffect(effectId: string, callback: AsyncCallback<boolean>): void ```ts import vibrator from '@ohos.vibrator'; +import { BusinessError } from '@ohos.base'; try { // 查询是否支持'haptic.clock.timer' - vibrator.isSupportEffect('haptic.clock.timer', function (err, state) { + vibrator.isSupportEffect('haptic.clock.timer', (err: BusinessError, state: boolean) => { if (err) { console.error(`Failed to query effect. Code: ${err.code}, message: ${err.message}`); return; @@ -361,13 +378,14 @@ try { console.info('Succeed in querying effect'); if (state) { try { - vibrator.startVibration({ // 使用startVibration需要添加ohos.permission.VIBRATE权限 + // 使用startVibration需要添加ohos.permission.VIBRATE权限 + vibrator.startVibration({ type: 'preset', effectId: 'haptic.clock.timer', count: 1, }, { usage: 'unknown' - }, (error) => { + }, (error: BusinessError) => { if (error) { console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`); } else { @@ -375,12 +393,14 @@ try { } }); } catch (error) { - console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`); + let e: BusinessError = error as BusinessError; + console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); } } }) } catch (error) { - console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`); + let e: BusinessError = error as BusinessError; + console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); } ``` @@ -408,10 +428,11 @@ isSupportEffect(effectId: string): Promise<boolean> ```ts import vibrator from '@ohos.vibrator'; +import { BusinessError } from '@ohos.base'; try { // 查询是否支持'haptic.clock.timer' - vibrator.isSupportEffect('haptic.clock.timer').then((state) => { + vibrator.isSupportEffect('haptic.clock.timer').then((state: boolean) => { console.info(`The query result is ${state}`); if (state) { try { @@ -423,18 +444,20 @@ try { usage: 'unknown' }).then(() => { console.info('Succeed in starting vibration'); - }).catch((error) => { + }).catch((error: BusinessError) => { 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}`); + let e: BusinessError = error as BusinessError; + console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); } } - }, (error) => { + }, (error: BusinessError) => { console.error(`Failed to query effect. Code: ${error.code}, message: ${error.message}`); }) } catch (error) { - console.error(`An unexpected error occurred. Code: ${error.code}, message: ${error.message}`); + let e: BusinessError = error as BusinessError; + console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`); } ``` @@ -574,9 +597,12 @@ vibrate(duration: number): Promise<void> **示例:** ```ts +import vibrator from '@ohos.vibrator'; +import { BusinessError } from '@ohos.base'; + vibrator.vibrate(1000).then(() => { console.info('Succeed in vibrating'); -}, (error) => { +}, (error: BusinessError) => { console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`); }); ``` @@ -603,7 +629,10 @@ vibrate(duration: number, callback?: AsyncCallback<void>): void **示例:** ```ts -vibrator.vibrate(1000, function (error) { +import vibrator from '@ohos.vibrator'; +import { BusinessError } from '@ohos.base'; + +vibrator.vibrate(1000, (error: BusinessError) => { if (error) { console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`); } else { @@ -640,9 +669,12 @@ vibrate(effectId: EffectId): Promise<void> **示例:** ```ts +import vibrator from '@ohos.vibrator'; +import { BusinessError } from '@ohos.base'; + vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER).then(() => { console.info('Succeed in vibrating'); -}, (error) => { +}, (error: BusinessError) => { console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`); }); ``` @@ -670,7 +702,10 @@ vibrate(effectId: EffectId, callback?: AsyncCallback<void>): void **示例:** ```ts -vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, function (error) { +import vibrator from '@ohos.vibrator'; +import { BusinessError } from '@ohos.base'; + +vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, (error: BusinessError) => { if (error) { console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`); } else { @@ -706,8 +741,11 @@ stop(stopMode: VibratorStopMode): Promise<void> **示例:** ```ts +import vibrator from '@ohos.vibrator'; +import { BusinessError } from '@ohos.base'; + // 按照effectId类型启动振动 -vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, function (error) { +vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, (error: BusinessError) => { if (error) { console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`); } else { @@ -717,7 +755,7 @@ vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, function (error) { // 使用VIBRATOR_STOP_MODE_PRESET模式停止振动 vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(() => { console.info('Succeed in stopping'); -}, (error) => { +}, (error: BusinessError) => { console.error(`Failed to stop. Code: ${error.code}, message: ${error.message}`); }); ``` @@ -745,8 +783,11 @@ stop(stopMode: VibratorStopMode, callback?: AsyncCallback<void>): void **示例:** ```ts +import vibrator from '@ohos.vibrator'; +import { BusinessError } from '@ohos.base'; + // 按照effectId类型启动振动 -vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, function (error) { +vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, (error: BusinessError) => { if (error) { console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`); } else { @@ -754,11 +795,11 @@ vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, function (error) { } }) // 使用VIBRATOR_STOP_MODE_PRESET模式停止振动 -vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, function (error) { +vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, (error: BusinessError) => { if (error) { console.error(`Failed to stop. Code: ${error.code}, message: ${error.message}`); } else { - onsole.info('Succeed in stopping'); + console.info('Succeed in stopping'); } }) ```