diff --git a/README.md b/README.md index 480a59f71a3174007264ec0979c9718696354e92..fe90d5a778fc6d42d16ee02cde223641d6ec269d 100755 --- a/README.md +++ b/README.md @@ -120,17 +120,17 @@ The sensor JS APIs listen for sensor data changes. If an API is called multiple -

on(type: SensorType, callback: AsyncCallback<Response>, options?: Options)

+

on(type: SensorType, callback: Callback<Response>, options?: Options)

Subscribes to a type of sensor that listens for changes of sensor data. SensorType indicates the type of the sensor that can be subscribed to. callback specifies whether the subscription is successful. options indicates the interval for reporting sensor data.

-

once(type: SensorType, callback: AsyncCallback<Response>)

+

once(type: SensorType, callback: Callback<Response>)

Subscribes to a type of sensor that listens for the sensor data change once. SensorType indicates the type of the sensor that can be subscribed to. callback specifies whether the subscription is successful.

-

off(type: SensorType, callback: AsyncCallback<void>)

+

off(type: SensorType, callback: Callback<Response>)

Unsubscribes from a type of sensor that listens for data changes. SensorType indicates the type of the sensor that can be unsubscribed from. callback specifies whether the unsubscription is successful.

@@ -138,6 +138,8 @@ The sensor JS APIs listen for sensor data changes. If an API is called multiple + + ### How to Use 1. Import the sensor package. @@ -153,29 +155,13 @@ import sensor from '@ohos.sensor'; export default { onCreate() { // Step 2 Subscribe to and listen for data changes of a type of sensor. - sensor.on(sensor.SENSOR_TYPE_ID_ACCELEROMETER, (error, data) => { - if (error) { - console.error("Failed to subscribe to acceleration data. Error code: " + error.code + "; message: " + error.message); - return; - } + sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, (data) => { console.info("Acceleration data obtained. x: " + data.x + "; y: " + data.y + "; z: " + data.z); }, {'interval':200000000}); // Step 3 Unsubscribe from data changes of the sensor 10 seconds later. - setTimeout(function(){ - sensor.off(sensor.SENSOR_TYPE_ID_ACCELEROMETER, function(error) { - if (error) { - console.error("Failed to unsubscribe from acceleration data. Error code: " + error.code + "; message: " + error.message); - return; - } - console.info("Succeeded in unsubscribe from sensor data"); - }); - } ,10000); + sensor.off(sensor.SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION); // Step 4 Subscribe to and listen for a data change of a type of sensor. - sensor.once(sensor.SENSOR_TYPE_ID_ACCELEROMETER, (error, data) => { - if (error) { - console.error("Failed to subscribe to gravity data. Error code: " + error.code + "; message: " + error.message); - return; - } + sensor.once(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, (data) => { console.info("Acceleration data obtained. x: " + data.x + "; y: " + data.y + "; z: " + data.z); }); } diff --git a/README_zh.md b/README_zh.md index 62323e6ee009230d8e3078c0ddf32720fdcb014d..b9006123dbe2c981e7dc7cf3fb8c5852ea7673a3 100755 --- a/README_zh.md +++ b/README_zh.md @@ -120,17 +120,17 @@ sensor导入模块的示例代码如下: -

on(type: SensorType, callback: AsyncCallback<Response>, options?: Options)

+

on(type: SensorType, callback: Callback<Response>, options?: Options)

监听传感器数据变化。SensorType为支持订阅的传感器类型,callback表示订阅传感器的回调函数,options为设置传感器数据上报的时间间隔。

-

once(type: SensorType, callback: AsyncCallback<Response>)

+

once(type: SensorType, callback: Callback<Response>)

监听传感器数据变化一次。SensorType为支持订阅的传感器类型,callback表示订阅传感器的回调函数。

-

off(type: SensorType, callback: AsyncCallback<void>)

+

off(type: SensorType, callback: Callback<Response>)

取消订阅传感器数据。SensorType为支持的取消订阅的传感器类型,callback表示取消订阅传感器是否成功。

@@ -138,6 +138,8 @@ sensor导入模块的示例代码如下: + + ### 使用说明 1. 导包。 @@ -153,29 +155,13 @@ import sensor from '@ohos.sensor'; export default { onCreate() { //步骤2 监听传感器数据变化,并注册传感器类型 - sensor.on(sensor.SENSOR_TYPE_ID_ACCELEROMETER, (error, data) => { - if (error) { - console.error("Failed to subscribe to acceleration data. Error code: " + error.code + "; message: " + error.message); - return; - } + sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, (data) => { console.info("Acceleration data obtained. x: " + data.x + "; y: " + data.y + "; z: " + data.z); }, {'interval':200000000}); //步骤3 设置10秒后取消订阅传感器数据 - setTimeout(function(){ - sensor.off(sensor.SENSOR_TYPE_ID_ACCELEROMETER, function(error) { - if (error) { - console.error("Failed to unsubscribe from acceleration data. Error code: " + error.code + "; message: " + error.message); - return; - } - console.info("Succeeded in unsubscribe from sensor data"); - }); - } ,10000); + sensor.off(sensor.SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION); //步骤4 监听传感器数据变化一次,并注册传感器类型 - sensor.once(sensor.SENSOR_TYPE_ID_ACCELEROMETER, (error, data) => { - if (error) { - console.error("Failed to subscribe to gravity data. Error code: " + error.code + "; message: " + error.message); - return; - } + sensor.once(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, (data) => { console.info("Acceleration data obtained. x: " + data.x + "; y: " + data.y + "; z: " + data.z); }); }