传感器
说明: 从API Version 7 开始支持。
导入模块
import sensor from '@ohos.sensor';
权限列表
计步器:ohos.permission.ACTIVITY_MOTION
心率:ohos.permission.READ_HEALTH_DATA
加速度:ohos.permission.ACCELEROMETER
sensor.on
on(type: SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback: AsyncCallback<AccelerometerResponse>,options?: Options): void
监听加速度传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。
参数:
AsyncCallback<AccelerometerResponse> |
|||
示例:
sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的x,y,z轴坐标的分量
console.info('X-coordinate component: ' + data.x);
console.info('Y-coordinate component: ' + data.y);
console.info('Z-coordinate component: ' + data.z);
},
{interval: 10000000}
);
sensor.on
on(type:SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION,callback:AsyncCallback<LinearAccelerometerResponse>, options?: Options): void
监听线性加速度传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。
参数:
AsyncCallback<LinearAccelerometerResponse> |
|||
示例:
sensor.on(sensor.SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的x,y,z轴坐标的分量
console.info('X-coordinate component: ' + data.x);
console.info('Y-coordinate component: ' + data.y);
console.info('Z-coordinate component: ' + data.z);
}
{interval: 10000000}
);
sensor.on
on(type:SensorType.SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED,callback:AsyncCallback<AccelerometerUncalibratedResponse>, options?: Options): void
监听未校准加速度计传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。
参数:
AsyncCallback<AccelerometerUncalibratedResponse> |
注册未校准加速度计传感器的回调函数,上报的数据类型为AccelerometerUncalibratedResponse。 |
||
示例:
sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的x,y,z轴坐标的分量
console.info('X-coordinate component: ' + data.x);
console.info('Y-coordinate component: ' + data.y);
console.info('Z-coordinate component: ' + data.z);
//打印data在x,y,z轴坐标的偏移量
console.info('X-coordinate bias: ' + data.biasX);
console.info('Y-coordinate bias: ' + data.biasY);
console.info('Z-coordinate bias: ' + data.biasZ);
}
{interval: 10000000}
);
sensor.on
on(type: SensorType.SENSOR_TYPE_ID_GRAVITY, callback: AsyncCallback<GravityResponse>,options?: Options): void
监听重力传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。
参数:
AsyncCallback<GravityResponse> |
|||
示例:
sensor.on(sensor.SensorType.SENSOR_TYPE_ID_GRAVITY, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的x,y,z轴坐标的分量
console.info('X-coordinate component: ' + data.x);
console.info('Y-coordinate component: ' + data.y);
console.info('Z-coordinate component: ' + data.z);
}
{interval: 10000000}
);
sensor.on
on(type: SensorType.SENSOR_TYPE_ID_GYROSCOPE, callback: AsyncCallback<GyroscopeResponse>, options?: Options): void
监听陀螺仪传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。
参数:
AsyncCallback<GyroscopeResponse> |
|||
示例:
sensor.on(sensor.SensorType.SENSOR_TYPE_ID_GYROSCOPE, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的x,y,z轴坐标的分量
console.info('X-coordinate component: ' + data.x);
console.info('Y-coordinate component: ' + data.y);
console.info('Z-coordinate component: ' + data.z);
}
{interval: 10000000}
);
sensor.on
on(type:SensorType.SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED,callback:AsyncCallback<GyroscopeUncalibratedResponse>, options?: Options): void
监听未校准陀螺仪传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。
参数:
AsyncCallback<GyroscopeUncalibratedResponse> |
|||
示例:
sensor.on(sensor.SensorType.SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的x,y,z轴坐标的分量
console.info('X-coordinate component: ' + data.x);
console.info('Y-coordinate component: ' + data.y);
console.info('Z-coordinate component: ' + data.z);
//打印data的x,y,z轴坐标的偏移量
console.info('X-coordinate bias: ' + data.biasX);
console.info('Y-coordinate bias: ' + data.biasY);
console.info('Z-coordinate bias: ' + data.biasZ);
}
{interval: 10000000}
);
sensor.on
on(type: SensorType.SENSOR_TYPE_ID_SIGNIFICANT_MOTION, callback: AsyncCallback<SignificantMotionResponse>, options?: Options): void
监听有效运动传感器数据变化。如果多次调用该接口,仅最后一次调用生效。
参数:
AsyncCallback<SignificantMotionResponse> |
|||
示例:
sensor.on(sensor.SensorType.SENSOR_TYPE_ID_SIGNIFICANT_MOTION, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
console.info('Scalar data: ' + data.scalar);
}
{interval: 10000000}
);
sensor.on
on(type: SensorType.SENSOR_TYPE_ID_PEDOMETER_DETECTION, callback: AsyncCallback<PedometerDetectResponse>, options?: Options): void
监听计步检测传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。
参数:
AsyncCallback<PedometerDetectResponse> |
|||
示例:
sensor.on(sensor.SensorType.SENSOR_TYPE_ID_PEDOMETER_DETECTION, function(error, data) {
if (error) {
console.error(""Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
console.info('Scalar data: ' + data.scalar);
}
{interval: 10000000}
);
sensor.on
on(type: SensorType.SENSOR_TYPE_ID_PEDOMETER, callback: AsyncCallback<PedometerResponse>, options?: Options): void
监听计步传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。
参数:
AsyncCallback<PedometerResponse> |
|||
示例:
sensor.on(sensor.SensorType.SENSOR_TYPE_ID_PEDOMETER, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的步数
console.info('Steps: ' + data.steps);
}
{interval: 10000000}
);
sensor.on
on(type:SensorType.SENSOR_TYPE_ID_AMBIENT_TEMPERATURE,callback:AsyncCallback<AmbientTemperatureResponse>, options?: Options): void
监听环境温度传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。
参数:
AsyncCallback<AmbientTemperatureResponse> |
|||
示例:
sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_TEMPERATURE, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的温度值
console.info('Temperature: ' + data.temperature);
}
{interval: 10000000}
);
sensor.on
on(type: SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD, callback: AsyncCallback<MagneticFieldResponse>,options?: Options): void
监听磁场传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。
参数:
AsyncCallback<MagneticFieldResponse> |
|||
示例:
sensor.on(sensor.SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的x,y,z轴坐标的分量
console.info('X-coordinate component: ' + data.x);
console.info('Y-coordinate component: ' + data.y);
console.info('Z-coordinate component: ' + data.z);
}
{interval: 10000000}
);
sensor.on
on(type:SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED,callback:AsyncCallback<MagneticFieldUncalibratedResponse>, options: Options): void
监听未校准磁场传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。
参数:
AsyncCallback<MagneticFieldUncalibratedResponse> |
|||
示例:
sensor.on(sensor.SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的x,y,z轴坐标的分量
console.info('X-coordinate component: ' + data.x);
console.info('Y-coordinate component: ' + data.y);
console.info('Z-coordinate component: ' + data.z);
//打印data的x,y,z轴坐标的偏移量
console.info('X-coordinate bias: ' + data.biasX);
console.info('Y-coordinate bias: ' + data.biasY);
console.info('Z-coordinate bias: ' + data.biasZ);
}
{interval: 10000000} //设置数据的上报频率。
);
sensor.on
on(type: SensorType.SENSOR_TYPE_ID_PROXIMITY, callback: AsyncCallback<ProximityResponse>,options?: Options): void
监听接近光传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。
参数:
AsyncCallback<ProximityResponse> |
|||
示例:
sensor.on(sensor.SensorType.SENSOR_TYPE_ID_PROXIMITY, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的距离值
console.info('Distance: ' + data.distance);
}
{interval: 10000000}
);
sensor.on
on(type: SensorType.SENSOR_TYPE_ID_HUMIDITY, callback: AsyncCallback<HumidityResponse>,options?: Options): void
监听湿度传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。
参数:
AsyncCallback<HumidityResponse> |
|||
示例:
sensor.on(sensor.SensorType.SENSOR_TYPE_ID_HUMIDITY, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的湿度值
console.info('Humidity: ' + data.humidity);
}
{interval: 10000000}
);
sensor.on
on(type: SensorType.SENSOR_TYPE_ID_BAROMETER, callback: AsyncCallback<BarometerResponse>,options?: Options): void
监听气压计传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。
参数:
AsyncCallback<BarometerResponse> |
|||
示例:
sensor.on(sensor.SensorType.SENSOR_TYPE_ID_BAROMETER, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的压强值
console.info('Atmospheric pressure: ' + data.pressure);
}
{interval: 10000000}
);
sensor.on
on(type: SensorType.SENSOR_TYPE_ID_HALL, callback: AsyncCallback<HallResponse>, options?: Options): void
监听霍尔传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。
参数:
AsyncCallback<HallResponse> |
|||
示例:
sensor.on(sensor.SensorType.SENSOR_TYPE_ID_HALL, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的状态值
console.info('Status: ' + data.status);
}
{interval: 10000000}
);
sensor.on
on(type: SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback: AsyncCallback<LightResponse>, options?: Options): void
监听环境光传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。
参数:
AsyncCallback<LightResponse> |
|||
示例:
sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的光强值
console.info(''Illumination: ' + data.intensity);
}
{interval: 10000000}
);
sensor.on
on(type: SensorType.SENSOR_TYPE_ID_ORIENTATION, callback: AsyncCallback<OrientationResponse>, options?: Options): void
监听方向传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。
参数:
AsyncCallback<OrientationResponse> |
|||
示例:
sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ORIENTATION, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的x,y,z轴坐标的分量
console.info('X-coordinate component: ' + data.x);
console.info('Y-coordinate component: ' + data.y);
console.info('Z-coordinate component: ' + data.z);
}
{interval: 10000000}
);
sensor.on
on(type:SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR,callback:AsyncCallback<RotationVectorResponse>,options?: Options): void
监听旋转矢量传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。
参数:
AsyncCallback<RotationVectorResponse> |
|||
示例:
sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的x,y,z轴坐标的分量
console.info('X-coordinate component: ' + data.x);
console.info('Y-coordinate component: ' + data.y);
console.info('Z-coordinate component: ' + data.z);
}
{interval: 10000000}
);
sensor.on
on(type: SensorType.SENSOR_TYPE_ID_WEAR_DETECTION, callback: AsyncCallback<WearDetectionResponse>,options?: Options): void
监听佩戴检测传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。
参数:
AsyncCallback<WearDetectionResponse> |
|||
示例:
sensor.on(sensor.SensorType.SENSOR_TYPE_ID_WEAR_DETECTION, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的佩戴状态
console.info('Wear status: ' + data.value);
}
{interval: 10000000}
);
sensor.once
once(type: SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback: AsyncCallback<AccelerometerResponse>): void
监听加速度传感器的数据变化一次。
参数:
AsyncCallback<AccelerometerResponse> |
示例:
sensor.once(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的x,y,z轴坐标的分量
console.info('X-coordinate component: ' + data.x);
console.info('Y-coordinate component: ' + data.y);
console.info('Z-coordinate component: ' + data.z);
}
);
sensor.once
once(type:SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION,callback:AsyncCallback<LinearAccelerometerResponse>): void
监听线性加速度传感器数据变化一次。
参数:
AsyncCallback<LinearAccelerometerResponse> |
示例:
sensor.once(sensor.SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的x,y,z轴坐标的分量
console.info('X-coordinate component: ' + data.x);
console.info('Y-coordinate component: ' + data.y);
console.info('Z-coordinate component: ' + data.z);
}
);
sensor.once
once(type:SensorType.SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED,callback:AsyncCallback<AccelerometerUncalibratedResponse>): void
监听未校准加速度传感器的数据变化一次。
参数:
AsyncCallback<AccelerometerUncalibratedResponse> |
注册一次未校准加速度传感器的回调函数,上报的数据类型为AccelerometerUncalibratedResponse。 |
示例:
sensor.once(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的x,y,z轴坐标的分量
console.info('X-coordinate component: ' + data.x);
console.info('Y-coordinate component: ' + data.y);
console.info('Z-coordinate component: ' + data.z);
//打印data的x,y,z轴坐标的偏移量
console.info('X-coordinate bias: ' + data.biasX);
console.info('Y-coordinate bias: ' + data.biasY);
console.info('Z-coordinate bias: ' + data.biasZ);
}
);
sensor.once
once(type: SensorType.SENSOR_TYPE_ID_GRAVITY, callback: AsyncCallback<GravityResponse>): void
监听重力传感器的数据变化一次。
参数:
AsyncCallback<GravityResponse> |
示例:
sensor.once(sensor.SensorType.SENSOR_TYPE_ID_GRAVITY, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的x,y,z轴坐标的分量
console.info('X-coordinate component: ' + data.x);
console.info('Y-coordinate component: ' + data.y);
console.info('Z-coordinate component: ' + data.z);
}
);
sensor.once
once(type: SensorType.SENSOR_TYPE_ID_GYROSCOPE, callback: AsyncCallback<GyroscopeResponse>): void
监听陀螺仪传感器的数据变化一次。
参数:
AsyncCallback<GyroscopeResponse> |
示例:
sensor.once(sensor.SensorType.SENSOR_TYPE_ID_GYROSCOPE, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的x,y,z轴坐标的分量
console.info('X-coordinate component: ' + data.x);
console.info('Y-coordinate component: ' + data.y);
console.info('Z-coordinate component: ' + data.z);
}
);
sensor.once
once(type:SensorType.SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED,callback:AsyncCallback<GyroscopeUncalibratedResponse>): void
监听未校准陀螺仪传感器的数据变化一次。
参数:
AsyncCallback<GyroscopeUncalibratedResponse> |
示例:
sensor.once(sensor.SensorType.SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的x,y,z轴坐标的分量
console.info('X-coordinate component: ' + data.x);
console.info('Y-coordinate component: ' + data.y);
console.info('Z-coordinate component: ' + data.z);
//打印data的x,y,z轴坐标的偏移量
console.info('X-coordinate bias: ' + data.biasX);
console.info('Y-coordinate bias: ' + data.biasY);
console.info('Z-coordinate bias: ' + data.biasZ);
}
);
sensor.once
once(type: SensorType.SENSOR_TYPE_ID_SIGNIFICANT_MOTION,callback:AsyncCallback<SignificantMotionResponse>): void
监听有效运动传感器的数据变化一次。
参数:
AsyncCallback<SignificantMotionResponse> |
示例:
sensor.once(sensor.SensorType.SENSOR_TYPE_ID_SIGNIFICANT_MOTION, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
console.info('Scalar data: ' + data.scalar);
}
);
sensor.once
once(type:SensorType.SENSOR_TYPE_ID_PEDOMETER_DETECTION,callback:AsyncCallback<PedometerDetectResponse>): void
监听计步检测传感器数据变化一次。
参数:
AsyncCallback<PedometerDetectResponse> |
示例:
sensor.once(sensor.SensorType.SENSOR_TYPE_ID_PEDOMETER_DETECTION, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
console.info('Scalar data: ' + data.scalar);
}
);
sensor.once
once(type: SensorType.SENSOR_TYPE_ID_PEDOMETER, callback: AsyncCallback<PedometerResponse>): void
监听计步器传感器数据变化一次。
参数:
AsyncCallback<PedometerResponse> |
示例:
sensor.once(sensor.SensorType.SENSOR_TYPE_ID_PEDOMETER, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的步数
console.info('Steps: ' + data.steps);
}
);
sensor.once
once(type:SensorType.SENSOR_TYPE_ID_AMBIENT_TEMPERATURE,callback:AsyncCallback<AmbientTemperatureResponse>): void
监听环境温度传感器数据变化一次。
参数:
AsyncCallback<AmbientTemperatureResponse> |
示例:
sensor.once(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_TEMPERATURE, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的温度值
console.info('Temperature: ' + data.temperature);
}
);
sensor.once
once(type: SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD, callback: AsyncCallback<MagneticFieldResponse>): void
监听磁场传感器数据变化一次。
参数:
AsyncCallback<MagneticFieldResponse> |
示例:
sensor.once(sensor.SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的x,y,z轴坐标的分量
console.info('X-coordinate component: ' + data.x);
console.info('Y-coordinate component: ' + data.y);
console.info('Z-coordinate component: ' + data.z);
}
);
sensor.once
once(type:SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED,callback:AsyncCallback<MagneticFieldUncalibratedResponse>): void
监听未校准磁场传感器数据变化一次。
参数:
AsyncCallback<MagneticFieldUncalibratedResponse> |
注册一次未校准磁场传感器的回调函数,上报的数据类型为MagneticFieldUncalibratedResponse。 |
示例:
sensor.once(sensor.SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的x,y,z轴坐标的分量
console.info('X-coordinate component: ' + data.x);
console.info('Y-coordinate component: ' + data.y);
console.info('Z-coordinate component: ' + data.z);
//打印data的x,y,z轴坐标的偏移量
console.info('X-coordinate bias: ' + data.biasX);
console.info('Y-coordinate bias: ' + data.biasY);
console.info('Z-coordinate bias: ' + data.biasZ);
}
);
sensor.once
once(type: SensorType.SENSOR_TYPE_ID_PROXIMITY, callback: AsyncCallback<ProximityResponse>): void
监听接近光传感器数据变化一次。
参数:
AsyncCallback<ProximityResponse> |
示例:
sensor.once(sensor.SensorType.SENSOR_TYPE_ID_PROXIMITY, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的距离值
console.info('Distance: ' + data.distance);
}
);
senor.once
once(type: SensorType.SENSOR_TYPE_ID_HUMIDITY, callback: AsyncCallback<HumidityResponse>): void
监听湿度传感器数据变化一次。
参数:
AsyncCallback<HumidityResponse> |
示例:
sensor.once(sensor.SensorType.SENSOR_TYPE_ID_HUMIDITY, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的湿度值
console.info('Humidity: ' + data.humidity);
}
);
sensor.once
once(type: SensorType.SENSOR_TYPE_ID_BAROMETER, callback: AsyncCallback<BarometerResponse>): void
监听气压计传感器数据变化一次。
参数:
AsyncCallback<BarometerResponse> |
示例:
sensor.once(sensor.SensorType.SENSOR_TYPE_ID_BAROMETER, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的压强值
console.info('Atmospheric pressure: ' + data.pressure);
}
);
sensor.once
once(type: SensorType.SENSOR_TYPE_ID_HALL, callback: AsyncCallback<HallResponse>): void
监听霍尔传感器数据变化一次。
参数:
AsyncCallback<HallResponse> |
示例:
sensor.once(sensor.SensorType.SENSOR_TYPE_ID_HALL, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的状态值
console.info('Status: ' + data.status);
}
);
sensor.once
once(type: SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback: AsyncCallback<LightResponse>): void
监听环境光传感器数据变化一次。
参数:
AsyncCallback<LightResponse> |
示例:
sensor.once(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的光强值
console.info(''Illumination: ' + data.intensity);
}
);
sensor.once
once(type: SensorType.SENSOR_TYPE_ID_ORIENTATION, callback: AsyncCallback<OrientationResponse>): void
监听方向传感器数据变化一次。
参数:
AsyncCallback<OrientationResponse> |
示例:
sensor.once(sensor.SensorType.SENSOR_TYPE_ID_ORIENTATION, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的x,y,z轴坐标的分量
console.info('X-coordinate component: ' + data.x);
console.info('Y-coordinate component: ' + data.y);
console.info('Z-coordinate component: ' + data.z);
}
);
sensor.once
once(type: SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, callback: AsyncCallback<RotationVectorResponse>): void
监听旋转矢量传感器数据变化一次。
参数:
AsyncCallback<RotationVectorResponse> |
示例:
sensor.once(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的x,y,z轴坐标的分量
console.info('X-coordinate component: ' + data.x);
console.info('Y-coordinate component: ' + data.y);
console.info('Z-coordinate component: ' + data.z);
}
);
sensor.once
once(type: SensorType.SENSOR_TYPE_ID_HEART_RATE, callback: AsyncCallback<HeartRateResponse>): void
监听心率传感器数据变化一次。
参数:
AsyncCallback<HeartRateResponse> |
示例:
sensor.once(sensor.SensorType.SENSOR_TYPE_ID_HEART_RATE, function(error, data) {
if (error) {
console.error("Subscription failed. Error code: " + error.code + "; message: " + error.message);
return;
}
//打印data的心率值
console.info("Heart rate: " + data.heartRate);
}
);
sensor.once
once(type: SensorType.SENSOR_TYPE_ID_WEAR_DETECTION, callback: AsyncCallback<WearDetectionResponse>): void
监听佩戴检测传感器数据变化一次。
参数:
AsyncCallback<WearDetectionResponse> |
示例:
sensor.once(sensor.SensorType.SENSOR_TYPE_ID_WEAR_DETECTION, function(error, data) {
if (error) {
console.error("Failed to register data, error code is" + error.code + ", message: " + error.message);
return;
}
//打印data的佩戴状态
console.info("Wear status: "+ data.value);
}
);
sensor.off
off(type: SensorType, callback?: AsyncCallback<void>): void
取消订阅传感器数据。
参数:
示例:
sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, function(error) {
if (error) {
console.error("Failed to unsubscribe from acceleration sensor data. Error code: " + error.code + "; message: " + error.message);
return;
}
console.info("Succeeded in unsubscribing from acceleration sensor data.");
}
);
SensorType
表示要订阅或取消订阅的传感器类型。
AccelerometerResponse
加速度传感器数据。
属性
LinearAccelerometerResponse
线性加速度传感器数据。
属性
AccelerometerUncalibratedResponse
未校准加速度计传感器数据。
属性
GravityResponse
重力传感器数据。
属性
OrientationResponse
方向传感器数据。
属性
RotationVectorResponse
旋转矢量传感器数据。
属性
GyroscopeResponse
陀螺仪传感器数据。
属性
GyroscopeUncalibratedResponse
未校准陀螺仪传感器数据。
属性
SignificantMotionResponse
有效运动传感器数据。
属性
表示剧烈运动程度。测量三个物理轴(x、y 和 z)上,设备是否存在大幅度运动;如果取值为1则代表存在大幅度运动,取值为0则代表没有大幅度运动。 |
ProximityResponse
接近光传感器数据。
属性
LightResponse
环境光传感器数据。
属性
HallResponse
霍尔传感器数据。
属性
MagneticFieldResponse
磁场传感器数据。
属性
MagneticFieldUncalibratedResponse
未校准磁场传感器数据。
属性
PedometerResponse
计步传感器数据。
属性
HumidityResponse
湿度传感器数据。
属性
PedometerDetectResponse
计步检测传感器数据。
属性
AmbientTemperatureResponse
温度传感器数据。
属性
BarometerResponse
气压计传感器数据。
属性
HeartRateResponse
心率传感器数据。
属性
WearDetectionResponse
佩戴检测传感器数据。