HarmonyOS API:设备管理

joytrian
发布于 2023-4-10 18:07
浏览
0收藏

版本:v3.1 Beta

sensor.getDeviceAltitude9+

getDeviceAltitude(seaPressure: number, currentPressure: number): Promise<number>

根据气压值获取海拔高度。

系统能力:SystemCapability.Sensors.Sensor

参数:

参数名

类型

必填

说明

seaPressure

number

海平面气压值,单位为hPa。

currentPressure

number

指定的气压值,单位为hPa。

返回值:

类型

说明

Promise<number>

Promise对象,返回指定的气压值对应的海拔高度,单位为米。

错误码

以下错误码的详细介绍请参见​​ohos.sensor(传感器)错误码​​。

错误码ID

错误信息

14500101

Service exception.

示例:

try {
    let seaPressure = 1013.2;
    let currentPressure = 1500.0;
    const promise = sensor.getDeviceAltitude(seaPressure, currentPressure);
    promise.then((data) => {
        console.info('sensor_getDeviceAltitude_Promise success', data);
    }, (err) => {
        console.error('Get altitude failed. Error code: ' + err.code + '; message: ' + err.message);
    });
} catch (err) {
    console.error('Get altitude failed. Error code: ' + err.code + '; message: ' + err.message);
}

sensor.getInclination9+

getInclination(inclinationMatrix: Array<number>, callback: AsyncCallback<number>): void

根据倾斜矩阵计算地磁倾角。

系统能力:SystemCapability.Sensors.Sensor

参数:

参数名

类型

必填

说明

inclinationMatrix

Array<number>

倾斜矩阵。

callback

AsyncCallback<number>

回调函数,返回地磁倾角,单位为弧度。

错误码

以下错误码的详细介绍请参见​​ohos.sensor(传感器)错误码​​。

错误码ID

错误信息

14500101

Service exception.

示例:

try {
    // inclinationMatrix可以为3*3,或者4*4
    let inclinationMatrix = [
        1, 0, 0,
        0, 1, 0,
        0, 0, 1
    ]
    sensor.getInclination(inclinationMatrix, function (err, data) {
        if (err) {
            console.error('Get inclination failed. Error code: ' + err.code + '; message: ' + err.message);
            return;
        }
        console.info('Inclination: ' + data);
    })
} catch (err) {
    console.error('Get inclination failed. Error code: ' + err.code + '; message: ' + err.message);
}

sensor.getInclination9+

getInclination(inclinationMatrix: Array<number>): Promise<number>

根据倾斜矩阵计算地磁倾角。

系统能力:SystemCapability.Sensors.Sensor

参数:

参数名

类型

必填

说明

inclinationMatrix

Array<number>

倾斜矩阵。

返回值:

类型

说明

Promise<number>

Promise对象,返回地磁倾斜角,单位为弧度。

错误码

以下错误码的详细介绍请参见​​ohos.sensor(传感器)错误码​​。

错误码ID

错误信息

14500101

Service exception.

示例:

try {
    // inclinationMatrix可以为3*3,或者4*4
    let inclinationMatrix = [
        1, 0, 0,
        0, 1, 0,
        0, 0, 1
    ]
    const promise = sensor.getInclination(inclinationMatrix);
    promise.then((data) => {
        console.info('Inclination: ' + data);
    }, (err) => {
        console.error('Get inclination failed. Error code: ' + err.code + '; message: ' + err.message);
    });
} catch (err) {
    console.error('Get inclination failed. Error code: ' + err.code + '; message: ' + err.message);
}

sensor.getAngleVariation9+

getAngleVariation(currentRotationMatrix: Array<number>, preRotationMatrix: Array<number>,

callback: AsyncCallback<Array<number>>): void

计算两个旋转矩阵之间的角度变化。

系统能力:SystemCapability.Sensors.Sensor

参数:

参数名

类型

必填

说明

currentRotationMatrix

Array<number>

当前旋转矩阵。

preRotationMatrix

Array<number>

相对旋转矩阵。

callback

AsyncCallback<Array<number>>

回调函数,返回绕z、x、y轴方向的旋转角度。

错误码

以下错误码的详细介绍请参见​​ohos.sensor(传感器)错误码​​。

错误码ID

错误信息

14500101

Service exception.

示例:

try {
    // 旋转矩阵可以为3*3,或者4*4
    let currentRotationMatrix = [
        1, 0, 0,
        0, 1, 0,
        0, 0, 1
    ];
    let preRotationMatrix = [
        1, 0, 0,
        0, 0.87, -0.50,
        0, 0.50, 0.87
    ];
    sensor.getAngleVariation(currentRotationMatrix, preRotationMatrix, function (err, data) {
        if (err) {
            console.error('Get angle variation failed. Error code: ' + err.code + '; message: ' + err.message);
            return;
        }
        if (data.length < 3) {
            console.error("Get angle variation failed, length" + data.length);
        }
        console.info("Z: " + data[0]);
        console.info("X: " + data[1]);
        console.info("Y  : " + data[2]);
    })
} catch (err) {
    console.error('Get angle variation failed. Error code: ' + err.code + '; message: ' + err.message);
}

sensor.getAngleVariation9+

getAngleVariation(currentRotationMatrix: Array<number>, preRotationMatrix: Array<number>): Promise<Array<number>>

得到两个旋转矩阵之间的角度变化。

系统能力:SystemCapability.Sensors.Sensor

参数:

参数名

类型

必填

说明

currentRotationMatrix

Array<number>

当前旋转矩阵。

preRotationMatrix

Array<number>

相对旋转矩阵

返回值:

类型

说明

Promise<Array<number>>

Promise对象,返回绕z、x、y轴方向的旋转角度。

错误码

以下错误码的详细介绍请参见​​ohos.sensor(传感器)错误码​​。

错误码ID

错误信息

14500101

Service exception.

示例:

try {
    // 旋转矩阵可以为3*3,或者4*4
    let currentRotationMatrix = [
        1, 0, 0,
        0, 1, 0,
        0, 0, 1
    ];
    let preRotationMatrix = [
        1, 0, 0,
        0, 0.87, -0.50,
        0, 0.50, 0.87
    ];
    const promise = sensor.getAngleVariation(currentRotationMatrix, preRotationMatrix);
    promise.then((data) => {
        if (data.length < 3) {
            console.error("Get angle variation failed, length" + data.length);
        }
        console.info("Z: " + data[0]);
        console.info("X: " + data[1]);
        console.info("Y  : " + data[2]);
    }, (err) => {
        console.error('Get angle variation failed. Error code: ' + err.code + '; message: ' + err.message);
    });
} catch (err) {
    console.error('Get angle variation failed. Error code: ' + err.code + '; message: ' + err.message);
}

sensor.getRotationMatrix9+

getRotationMatrix(rotationVector: Array<number>, callback: AsyncCallback<Array<number>>): void

根据旋转矢量获取旋转矩阵。

系统能力:SystemCapability.Sensors.Sensor

参数:

参数名

类型

必填

说明

rotationVector

Array<number>

旋转矢量。

callback

AsyncCallback<Array<number>>

回调函数,返回3*3旋转矩阵。

错误码

以下错误码的详细介绍请参见​​ohos.sensor(传感器)错误码​​。

错误码ID

错误信息

14500101

Service exception.

示例:

try {
    let rotationVector = [0.20046076, 0.21907, 0.73978853, 0.60376877];
    sensor.getRotationMatrix(rotationVector, function (err, data) {
        if (err) {
            console.error('Get rotationMatrix failed. Error code: ' + err.code + '; message: ' + err.message);
            return;
        }
        for (var i = 0; i < data.length; i++) {
            console.info('data[' + i + ']: ' + data[i]);
        }
    })
} catch (err) {
    console.error('Get rotationMatrix failed. Error code: ' + err.code + '; message: ' + err.message);
}

sensor.getRotationMatrix9+

getRotationMatrix(rotationVector: Array<number>): Promise<Array<number>>

根据旋转矢量获取旋转矩阵。

系统能力:SystemCapability.Sensors.Sensor

参数:

参数名

类型

必填

说明

rotationVector

Array<number>

旋转矢量。

返回值:

类型

说明

Promise<Array<number>>

Promise对象,返回旋转矩阵。

错误码

以下错误码的详细介绍请参见​​ohos.sensor(传感器)错误码​​。

错误码ID

错误信息

14500101

Service exception.

示例:

try {
    let rotationVector = [0.20046076, 0.21907, 0.73978853, 0.60376877];
    const promise = sensor.getRotationMatrix(rotationVector);
    promise.then((data) => {
        for (var i = 0; i < data.length; i++) {
            console.info('data[' + i + ']: ' + data[i]);
        }
    }, (err) => {
        console.error('Get rotationMatrix failed. Error code: ' + err.code + '; message: ' + err.message);
    });
} catch (err) {
    console.error('Get rotationMatrix failed. Error code: ' + err.code + '; message: ' + err.message);
}

sensor.transformRotationMatrix9+

transformRotationMatrix(inRotationVector: Array<number>, coordinates: CoordinatesOptions,

callback: AsyncCallback<Array<number>>): void

根据指定坐标系映射旋转矩阵。

系统能力:SystemCapability.Sensors.Sensor

参数:

参数名

类型

必填

说明

inRotationVector

Array<number>

旋转矩阵。

coordinates

​CoordinatesOptions​

指定坐标系方向。

callback

AsyncCallback<Array<number>>

回调函数,返回映射后的旋转矩阵。

错误码

以下错误码的详细介绍请参见​​ohos.sensor(传感器)错误码​​。

错误码ID

错误信息

14500101

Service exception.

示例:

try {
    let rotationMatrix = [
        1, 0, 0,
        0, 0.87, -0.50,
        0, 0.50, 0.87
    ];
    sensor.transformRotationMatrix(rotationMatrix, { x: 1, y: 3 }, function (err, data) {
        if (err) {
            console.error('Transform rotationMatrix failed. Error code: ' + err.code + '; message: ' + err.message);
            return;
        }
        for (var i = 0; i < data.length; i++) {
            console.info('data[' + i + '] = ' + data[i]);
        }
    })
} catch (err) {
    console.error('Transform rotationMatrix failed. Error code: ' + err.code + '; message: ' + err.message);
}

sensor.transformRotationMatrix9+

transformRotationMatrix(inRotationVector: Array<number>, coordinates: CoordinatesOptions): Promise<Array<number>>

根据指定坐标系映射旋转矩阵。

系统能力:SystemCapability.Sensors.Sensor

参数:

参数名

类型

必填

说明

inRotationVector

Array<number>

旋转矩阵。

coordinates

​CoordinatesOptions​

指定坐标系方向。

返回值:

类型

说明

Promise<Array<number>>

Promise对象,返回转换后的旋转矩阵。

错误码

以下错误码的详细介绍请参见​​ohos.sensor(传感器)错误码​​。

错误码ID

错误信息

14500101

Service exception.

示例:

try {
    let rotationMatrix = [
        1, 0, 0,
        0, 0.87, -0.50,
        0, 0.50, 0.87
    ];
    const promise = sensor.transformRotationMatrix(rotationMatrix, { x: 1, y: 3 });
    promise.then((data) => {
        for (var i = 0; i < data.length; i++) {
            console.info('data[' + i + ']: ' + data[i]);
        }
    }, (err) => {
        console.error('Transform rotationMatrix failed. Error code: ' + err.code + '; message: ' + err.message);
    });
} catch (err) {
    console.error('Transform rotationMatrix failed. Error code: ' + err.code + '; message: ' + err.message);
}

sensor.getQuaternion9+

getQuaternion(rotationVector: Array<number>, callback: AsyncCallback<Array<number>>): void

根据旋转向量计算归一化四元数。

系统能力:SystemCapability.Sensors.Sensor

参数:

参数名

类型

必填

说明

rotationVector

Array<number>

旋转矢量。

callback

AsyncCallback<Array<number>>

回调函数,返回归一化四元数。

错误码

以下错误码的详细介绍请参见​​ohos.sensor(传感器)错误码​​。

错误码ID

错误信息

14500101

Service exception.

示例:

try {
    let rotationVector = [0.20046076, 0.21907, 0.73978853, 0.60376877];
    sensor.getQuaternion(rotationVector, function (err, data) {
        if (err) {
            console.error('Get quaternion failed. Error code: ' + err.code + '; message: ' + err.message);
            return;
        }
        for (var i = 0; i < data.length; i++) {
            console.info('data[' + i + ']: ' + data[i]);
        }
    })
} catch (err) {
    console.error('Get quaternion failed. Error code: ' + err.code + '; message: ' + err.message);
}

sensor.getQuaternion9+

getQuaternion(rotationVector: Array<number>): Promise<Array<number>>

根据旋转向量计算归一化四元数。

系统能力:SystemCapability.Sensors.Sensor

参数:

参数名

类型

必填

说明

rotationVector

Array<number>

旋转矢量。

返回值:

类型

说明

Promise<Array<number>>

Promise,对象返归一化回四元数。

错误码

以下错误码的详细介绍请参见​​ohos.sensor(传感器)错误码​​。

错误码ID

错误信息

14500101

Service exception.

示例:

try {
    let rotationVector = [0.20046076, 0.21907, 0.73978853, 0.60376877];
    const promise = sensor.getQuaternion(rotationVector);
    promise.then((data) => {
        for (var i = 0; i < data.length; i++) {
            console.info('data[' + i + ']: ' + data[i]);
        }
    }, (err) => {
        console.error('Get quaternion failed. Error code: ' + err.code + '; message: ' + err.message);
    });
} catch (err) {
    console.error('Get quaternion failed. Error code: ' + err.code + '; message: ' + err.message);
}

sensor.getOrientation9+

getOrientation(rotationMatrix: Array<number>, callback: AsyncCallback<Array<number>>): void

根据旋转矩阵计算设备方向。

系统能力:SystemCapability.Sensors.Sensor

参数:

参数名

类型

必填

说明

rotationMatrix

Array<number>

旋转矩阵。

callback

AsyncCallback<Array<number>>

回调函数,返回围绕z、x、y轴方向的旋转角度。

错误码

以下错误码的详细介绍请参见​​ohos.sensor(传感器)错误码​​。

错误码ID

错误信息

14500101

Service exception.

示例:

try {
    let preRotationMatrix = [
        1, 0, 0,
        0, 0.87, -0.50,
        0, 0.50, 0.87
    ];
    sensor.getOrientation(preRotationMatrix, function (err, data) {
        if (err) {
            console.error('Get orientation failed. Error code: ' + err.code + '; message: ' + err.message);
            return;
        }
        if (data.length < 3) {
            console.error("Get orientation failed, length" + data.length);
        }
        console.info("Z: " + data[0]);
        console.info("X: " + data[1]);
        console.info("Y  : " + data[2]);
    })
} catch (err) {
    console.error('Get orientation failed. Error code: ' + err.code + '; message: ' + err.message);
}

sensor.getOrientation9+

getOrientation(rotationMatrix: Array<number>): Promise<Array<number>>

根据旋转矩阵计算设备的方向。

系统能力:SystemCapability.Sensors.Sensor

参数:

参数名

类型

必填

说明

rotationMatrix

Array<number>

旋转矩阵。

返回值:

类型

说明

Promise<Array<number>>

Promise对象,返回围绕z、x、y轴方向的旋转角度。

错误码

以下错误码的详细介绍请参见​​ohos.sensor(传感器)错误码​​。

错误码ID

错误信息

14500101

Service exception.

示例:

try {
    let preRotationMatrix = [
        1, 0, 0,
        0, 0.87, -0.50,
        0, 0.50, 0.87
    ];
    const promise = sensor.getOrientation(preRotationMatrix);
    promise.then((data) => {
        for (var i = 0; i < data.length; i++) {
            console.info('data[' + i + ']: ' + data[i]);
        }
    }, (err) => {
        console.error('getOrientation failed. Error code: ' + err.code + '; message: ' + err.message);
    });
} catch (err) {
    console.error('getOrientation failed. Error code: ' + err.code + '; message: ' + err.message);
}

sensor.getRotationMatrix9+

getRotationMatrix(gravity: Array<number>, geomagnetic: Array<number>, callback: AsyncCallback<RotationMatrixResponse>): void

根据重力矢量和地磁矢量计算旋转矩阵。

系统能力:SystemCapability.Sensors.Sensor

参数:

参数名

类型

必填

说明

gravity

Array<number>

重力矢量。

geomagnetic

Array<number>

地磁矢量。

callback

AsyncCallback<​​RotationMatrixResponse​​>

回调函数,返回旋转矩阵。

错误码

以下错误码的详细介绍请参见​​ohos.sensor(传感器)错误码​​。

错误码ID

错误信息

14500101

Service exception.

示例:

try {
    let gravity = [-0.27775216, 0.5351276, 9.788099];
    let geomagnetic = [210.87253, -78.6096, -111.44444];
    sensor.getRotationMatrix(gravity, geomagnetic, function (err, data) {
        if (err) {
            console.error('Get rotationMatrix failed. Error code: ' + err.code + '; message: ' + err.message);
            return;
        }
        console.info('RotationMatrix' + JSON.stringify(data));
    })
} catch (err) {
    console.error('Get rotationMatrix failed. Error code: ' + err.code + '; message: ' + err.message);
}

sensor.getRotationMatrix9+

getRotationMatrix(gravity: Array<number>, geomagnetic: Array<number>,): Promise<RotationMatrixResponse>

根据重力矢量和地磁矢量计算旋转矩阵。

系统能力:SystemCapability.Sensors.Sensor

参数:

参数名

类型

必填

说明

gravity

Array<number>

重力向量。

geomagnetic

Array<number>

地磁矢量。

返回值:

类型

说明

Promise<​​RotationMatrixResponse​​>

Promise对象,返回旋转矩阵。

错误码

以下错误码的详细介绍请参见​​ohos.sensor(传感器)错误码​​。

错误码ID

错误信息

14500101

Service exception.

示例:

try {
    let gravity = [-0.27775216, 0.5351276, 9.788099];
    let geomagnetic = [210.87253, -78.6096, -111.44444];
    const promise = sensor.getRotationMatrix(gravity, geomagnetic);
    promise.then((data) => {
        console.info('RotationMatrix' + JSON.stringify(data));
    }, (err) => {
        console.error('Get rotationMatrix failed. Error code: ' + err.code + '; message: ' + err.message);
    });
} catch (err) {
    console.error('Get rotationMatrix failed. Error code: ' + err.code + '; message: ' + err.message);
}

SensorId9+

表示要订阅或取消订阅的传感器类型。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor

名称

说明

ACCELEROMETER

1

加速度传感器。

GYROSCOPE

2

陀螺仪传感器。

AMBIENT_LIGHT

5

环境光传感器。

MAGNETIC_FIELD

6

磁场传感器。

BAROMETER

8

气压计传感器。

HALL

10

霍尔传感器。

PROXIMITY

12

接近光传感器。

HUMIDITY

13

湿度传感器。

ORIENTATION

256

方向传感器。

GRAVITY

257

重力传感器。

LINEAR_ACCELEROMETER

258

线性加速度传感器。

ROTATION_VECTOR

259

旋转矢量传感器。

AMBIENT_TEMPERATURE

260

环境温度传感器。

MAGNETIC_FIELD_UNCALIBRATED

261

未校准磁场传感器。

GYROSCOPE_UNCALIBRATED

263

未校准陀螺仪传感器。

SIGNIFICANT_MOTION

264

有效运动传感器。

PEDOMETER_DETECTION

265

计步检测传感器。

PEDOMETER

266

计步传感器。

HEART_RATE

278

心率传感器。

WEAR_DETECTION

280

佩戴检测传感器。

ACCELEROMETER_UNCALIBRATED

281

未校准加速度计传感器。

SensorType(deprecated)

表示要订阅或取消订阅的传感器类型。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor

名称

说明

SENSOR_TYPE_ID_ACCELEROMETER

1

加速度传感器。

SENSOR_TYPE_ID_GYROSCOPE

2

陀螺仪传感器。

SENSOR_TYPE_ID_AMBIENT_LIGHT

5

环境光传感器。

SENSOR_TYPE_ID_MAGNETIC_FIELD

6

磁场传感器。

SENSOR_TYPE_ID_BAROMETER

8

气压计传感器。

SENSOR_TYPE_ID_HALL

10

霍尔传感器。

SENSOR_TYPE_ID_PROXIMITY

12

接近光传感器。

SENSOR_TYPE_ID_HUMIDITY

13

湿度传感器。

SENSOR_TYPE_ID_ORIENTATION

256

方向传感器。

SENSOR_TYPE_ID_GRAVITY

257

重力传感器。

SENSOR_TYPE_ID_LINEAR_ACCELERATION

258

线性加速度传感器。

SENSOR_TYPE_ID_ROTATION_VECTOR

259

旋转矢量传感器。

SENSOR_TYPE_ID_AMBIENT_TEMPERATURE

260

环境温度传感器。

SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED

261

未校准磁场传感器。

SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED

263

未校准陀螺仪传感器。

SENSOR_TYPE_ID_SIGNIFICANT_MOTION

264

有效运动传感器。

SENSOR_TYPE_ID_PEDOMETER_DETECTION

265

计步检测传感器。

SENSOR_TYPE_ID_PEDOMETER

266

计步传感器。

SENSOR_TYPE_ID_HEART_RATE

278

心率传感器。

SENSOR_TYPE_ID_WEAR_DETECTION

280

佩戴检测传感器。

SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED

281

未校准加速度计传感器。

Response

传感器数据的时间戳。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor

名称

类型

可读

可写

说明

timestamp

number

传感器数据上报的时间戳。

Sensor9+

指示传感器信息。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor

名称

类型

可读

可写

说明

sensorName

string

传感器名称。

venderName

string

传感器供应商。

firmwareVersion

string

传感器固件版本。

hardwareVersion

string

传感器硬件版本。

sensorId

number

传感器类型id。

maxRange

number

传感器测量范围的最大值。

  

  

  

  

  

  

  

  

  

  

precision

number

传感器精度。

power

number

传感器功率。

AccelerometerResponse

加速度传感器数据,继承于​​Response​​。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor

名称

类型

可读

可写

说明

x

number

施加在设备x轴的加速度,单位 : m/s2。

y

number

施加在设备y轴的加速度,单位 : m/s2。

z

number

施加在设备z轴的加速度,单位 : m/s2。

LinearAccelerometerResponse

线性加速度传感器数据,继承于​​Response​​。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor

名称

类型

可读

可写

说明

x

number

施加在设备x轴的线性加速度,单位 : m/s2。

y

number

施加在设备y轴的线性加速度,单位 : m/s2。

z

number

施加在设备z轴的线性加速度,单位 : m/s2。

AccelerometerUncalibratedResponse

未校准加速度计传感器数据,继承于​​Response​​。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor

名称

类型

可读

可写

说明

x

number

施加在设备x轴未校准的加速度,单位 : m/s2。

y

number

施加在设备y轴未校准的加速度,单位 : m/s2。

z

number

施加在设备z轴未校准的加速度,单位 : m/s2。

biasX

number

施加在设备x轴未校准的加速度偏量,单位 : m/s2。

biasY

number

施加在设备上y轴未校准的加速度偏量,单位 : m/s2。

biasZ

number

施加在设备z轴未校准的加速度偏量,单位 : m/s2。

GravityResponse

重力传感器数据,继承于​​Response​​。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor

名称

类型

可读

可写

说明

x

number

施加在设备x轴的重力加速度,单位 : m/s2。

y

number

施加在设备y轴的重力加速度,单位 : m/s2。

z

number

施加在设备z轴的重力加速度,单位 : m/s2。

OrientationResponse

方向传感器数据,继承于​​Response​​。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor

名称

类型

可读

可写

说明

alpha

number

设备围绕Z轴的旋转角度,单位:度。

beta

number

设备围绕X轴的旋转角度,单位:度。

gamma

number

设备围绕Y轴的旋转角度,单位:度。

RotationVectorResponse

旋转矢量传感器数据,继承于​​Response​​。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor

名称

类型

可读

可写

说明

x

number

旋转矢量x轴分量。

y

number

旋转矢量y轴分量。

z

number

旋转矢量z轴分量。

w

number

标量。

GyroscopeResponse

陀螺仪传感器数据,继承于​​Response​​。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor

名称

类型

可读

可写

说明

x

number

设备x轴的旋转角速度,单位rad/s。

y

number

设备y轴的旋转角速度,单位rad/s。

z

number

设备z轴的旋转角速度,单位rad/s。

GyroscopeUncalibratedResponse

未校准陀螺仪传感器数据,继承于​​Response​​。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor

名称

类型

可读

可写

说明

x

number

设备x轴未校准的旋转角速度,单位rad/s。

y

number

设备y轴未校准的旋转角速度,单位rad/s。

z

number

设备z轴未校准的旋转角速度,单位rad/s。

biasX

number

设备x轴未校准的旋转角速度偏量,单位rad/s。

biasY

number

设备y轴未校准的旋转角速度偏量,单位rad/s。

biasZ

number

设备z轴未校准的旋转角速度偏量,单位rad/s。

SignificantMotionResponse

有效运动传感器数据,继承于​​Response​​。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor

名称

类型

可读

可写

说明

scalar

number

表示剧烈运动程度。测量三个物理轴(x、y 和 z)上,设备是否存在大幅度运动;如果取值为1则代表存在大幅度运动,取值为0则代表没有大幅度运动。

ProximityResponse

接近光传感器数据,继承于​​Response​​。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor

名称

类型

可读

可写

说明

distance

number

可见物体与设备显示器的接近程度。0表示接近,1表示远离。

LightResponse

环境光传感器数据,继承于​​Response​​。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor

名称

类型

可读

可写

说明

intensity

number

光强(单位:勒克斯)。

HallResponse

霍尔传感器数据,继承于​​Response​​。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor

名称

类型

可读

可写

说明

status

number

显示霍尔状态。测量设备周围是否存在磁力吸引,0表示没有,大于0表示有。

MagneticFieldResponse

磁场传感器数据,继承于​​Response​​。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor

名称

类型

可读

可写

说明

x

number

x轴环境磁场强度,单位 : μT。

y

number

y轴环境磁场强度,单位 : μT。

z

number

z轴环境磁场强度,单位 : μT。

MagneticFieldUncalibratedResponse

未校准磁场传感器数据,继承于​​Response​​。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor

名称

类型

可读

可写

说明

x

number

x轴未校准环境磁场强度,单位 : μT。

y

number

y轴未校准环境磁场强度,单位 : μT。

z

number

z轴未校准环境磁场强度,单位 : μT。

biasX

number

x轴未校准环境磁场强度偏量,单位 : μT。

biasY

number

y轴未校准环境磁场强度偏量,单位 : μT。

biasZ

number

z轴未校准环境磁场强度偏量,单位 : μT。

PedometerResponse

计步传感器数据,继承于​​Response​​。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor

名称

类型

可读

可写

说明

steps

number

用户的行走步数。

HumidityResponse

湿度传感器数据,继承于​​Response​​。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor

名称

类型

可读

可写

说明

humidity

number

湿度值。测量环境的相对湿度,以百分比 (%) 表示。

PedometerDetectionResponse

计步检测传感器数据,继承于​​Response​​。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor

名称

类型

可读

可写

说明

scalar

number

计步器检测。检测用户的计步动作,如果取值为1则代表用户产生了计步行走的动作,取值为0则代表用户没有发生运动。

AmbientTemperatureResponse

温度传感器数据,继承于​​Response​​。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor

名称

类型

可读

可写

说明

temperature

number

环境温度(单位:摄氏度)。

BarometerResponse

气压计传感器数据,继承于​​Response​​。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor

名称

类型

可读

可写

说明

pressure

number

压力值(单位:帕斯卡)。

HeartRateResponse

心率传感器数据,继承于​​Response​​。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor

名称

类型

可读

可写

说明

heartRate

number

心率值。测量用户的心率数值,单位:bpm。

WearDetectionResponse

佩戴检测传感器数据,继承于​​Response​​。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor

名称

类型

可读

可写

说明

value

number

表示设备是否被穿戴(1表示已穿戴,0表示未穿戴)。

Options

设置传感器上报频率。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor

名称

类型

可读

可写

说明

interval

number

表示传感器的上报频率,默认值为200000000ns。

RotationMatrixResponse

设置旋转矩阵响应对象。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor

名称

类型

可读

可写

说明

rotation

Array<number>

旋转矩阵。

inclination

Array<number>

倾斜矩阵。

CoordinatesOptions

设置坐标选项对象。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor

名称

类型

可读

可写

说明

x

number

x坐标方向。

y

number

y坐标方向。

GeomagneticResponse

设置地磁响应对象,继承于​​Response​​。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor

名称

类型

可读

可写

说明

x

number

地磁场的北分量。

y

number

地磁场的东分量。

z

number

地磁场的垂直分量。

geomagneticDip

number

地磁倾角,即地球磁场线与水平面的夹角。

deflectionAngle

number

地磁偏角,即地磁北方向与正北方向在水平面上的角度。

levelIntensity

number

地磁场的水平强度。

totalIntensity

number

地磁场的总强度。

LocationOptions

指示地理位置。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.Sensor

名称

类型

可读

可写

说明

latitude

number

纬度。

longitude

number

经度。

altitude

number

海拔高度。

sensor.on(deprecated)

ACCELEROMETER(deprecated)

on(type: SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback: Callback<AccelerometerResponse>,options?: Options): void

监听加速度传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。

从API version 9 开始不再维护,建议使用​​sensor.on.ACCELEROMETER​​代替。

需要权限:ohos.permission.ACCELEROMETER

系统能力:SystemCapability.Sensors.Sensor

参数:

参数名

类型

必填

说明

type

​SensorType​​.SENSOR_TYPE_ID_ACCELEROMETER

要订阅的加速度传感器类型为SENSOR_TYPE_ID_ACCELEROMETER。

callback

Callback<​​AccelerometerResponse​​>

注册加速度传感器的回调函数,上报的数据类型为AccelerometerResponse。

options

​Options​

可选参数列表,设置上报频率,默认值为200000000ns。

示例:

sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER,function(data){
    console.info('X-coordinate component: ' + data.x);
    console.info('Y-coordinate component: ' + data.y);
    console.info('Z-coordinate component: ' + data.z);
},
    {interval: 10000000}
);

LINEAR_ACCELERATION(deprecated)

on(type: SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION,callback:Callback<LinearAccelerometerResponse>, options?: Options): void

监听线性加速度传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。

从API version 9 开始不再维护,建议使用​​sensor.on.LINEAR_ACCELEROMETER​​代替。

需要权限:ohos.permission.ACCELEROMETER

系统能力:SystemCapability.Sensors.Sensor

参数:

参数名

类型

必填

说明

type

​SensorType​​.SENSOR_TYPE_ID_LINEAR_ACCELERATION

要订阅的线性加速度传感器类型为SENSOR_TYPE_ID_LINEAR_ACCELERATION。

callback

Callback<​​LinearAccelerometerResponse​​>

注册线性加速度传感器的回调函数,上报的数据类型为LinearAccelerometerResponse。

options

​Options​

可选参数列表,设置上报频率,默认值为200000000ns。

ACCELEROMETER_UNCALIBRATED(deprecated)

on(type: SensorType.SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED,callback: Callback<AccelerometerUncalibratedResponse>, options?: Options): void

监听未校准加速度计传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。

从API version 9 开始不再维护,建议使用​​sensor.on.ACCELEROMETER_UNCALIBRATED​​代替。

需要权限:ohos.permission.ACCELEROMETER

系统能力:SystemCapability.Sensors.Sensor

参数:

参数名

类型

必填

说明

type

​SensorType​​.SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED

要订阅的未校准加速度计传感器类型为SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED。

callback

Callback<​​AccelerometerUncalibratedResponse​​>

注册未校准加速度计传感器的回调函数,上报的数据类型为AccelerometerUncalibratedResponse。

options

​Options​

可选参数列表,设置上报频率,默认值为200000000ns。

示例:

sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED,function(data){
    console.info('X-coordinate component: ' + data.x);
    console.info('Y-coordinate component: ' + data.y);
    console.info('Z-coordinate component: ' + data.z);
    console.info('X-coordinate bias: ' + data.biasX);
    console.info('Y-coordinate bias: ' + data.biasY);
    console.info('Z-coordinate bias: ' + data.biasZ);
},
    {interval: 10000000}
);

GRAVITY(deprecated)

on(type: SensorType.SENSOR_TYPE_ID_GRAVITY, callback: Callback<GravityResponse>,options?: Options): void

监听重力传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。

从API version 9 开始不再维护,建议使用​​sensor.on.GRAVITY​​代替。

系统能力:SystemCapability.Sensors.Sensor

参数:

参数名

类型

必填

说明

type

​SensorType​​.SENSOR_TYPE_ID_GRAVITY

要订阅的重力传感器类型为SENSOR_TYPE_ID_GRAVITY。

callback

Callback<​​GravityResponse​​>

注册重力传感器的回调函数,上报的数据类型为GravityResponse。

options

​Options​

可选参数列表,设置上报频率,默认值为200000000ns。

示例:

sensor.on(sensor.SensorType.SENSOR_TYPE_ID_GRAVITY,function(data){
    console.info('X-coordinate component: ' + data.x);
    console.info('Y-coordinate component: ' + data.y);
    console.info('Z-coordinate component: ' + data.z);
},
    {interval: 10000000}
);

GYROSCOPE(deprecated)

on(type: SensorType.SENSOR_TYPE_ID_GYROSCOPE, callback: Callback<GyroscopeResponse>, options?: Options): void

监听陀螺仪传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。

从API version 9 开始不再维护,建议使用​​sensor.on.GYROSCOPE​​代替。

需要权限:ohos.permission.GYROSCOPE

系统能力:SystemCapability.Sensors.Sensor

参数:

参数名

类型

必填

说明

type

​SensorType​​.SENSOR_TYPE_ID_GYROSCOPE

要订阅的陀螺仪传感器类型为SENSOR_TYPE_ID_GYROSCOPE。

callback

Callback<​​GyroscopeResponse​​>

注册陀螺仪传感器的回调函数,上报的数据类型为GyroscopeResponse。

options

​Options​

可选参数列表,设置上报频率,默认值为200000000ns。

示例:

sensor.on(sensor.SensorType.SENSOR_TYPE_ID_GYROSCOPE,function(data){
    console.info('X-coordinate component: ' + data.x);
    console.info('Y-coordinate component: ' + data.y);
    console.info('Z-coordinate component: ' + data.z);
},
    {interval: 10000000}
);

GYROSCOPE_UNCALIBRATED(deprecated)

on(type: SensorType.SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED,callback:Callback<GyroscopeUncalibratedResponse>, options?: Options): void

监听未校准陀螺仪传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。

从API version 9 开始不再维护,建议使用​​sensor.on.GYROSCOPE_UNCALIBRATED​​代替。

需要权限:ohos.permission.GYROSCOPE

系统能力:SystemCapability.Sensors.Sensor

参数:

参数名

类型

必填

说明

type

​SensorType​​.SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED

要订阅的未校准陀螺仪传感器类型为SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED。

callback

Callback<​​GyroscopeUncalibratedResponse​​>

注册未校准陀螺仪传感器的回调函数,上报的数据类型为GyroscopeUncalibratedResponse。

options

​Options​

可选参数列表,设置上报频率。

示例:

sensor.on(sensor.SensorType.SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED,function(data){
    console.info('X-coordinate component: ' + data.x);
    console.info('Y-coordinate component: ' + data.y);
    console.info('Z-coordinate component: ' + data.z);
    console.info('X-coordinate bias: ' + data.biasX);
    console.info('Y-coordinate bias: ' + data.biasY);
    console.info('Z-coordinate bias: ' + data.biasZ);
},
    {interval: 10000000}
);

SIGNIFICANT_MOTION(deprecated)

on(type: SensorType.SENSOR_TYPE_ID_SIGNIFICANT_MOTION, callback: Callback<SignificantMotionResponse>, options?: Options): void

监听大幅动作传感器数据变化。如果多次调用该接口,仅最后一次调用生效。

从API version 9 开始不再维护,建议使用​​sensor.on.SIGNIFICANT_MOTION​​ 代替。

系统能力:SystemCapability.Sensors.Sensor

参数:

参数名

类型

必填

说明

type

​SensorType​​.SENSOR_TYPE_ID_SIGNIFICANT_MOTION

要订阅的大幅动作传感器类型为SENSOR_TYPE_ID_SIGNIFICANT_MOTION。

callback

Callback<​​SignificantMotionResponse​​>

注册有效运动传感器的回调函数,上报的数据类型为SignificantMotionResponse。

options

​Options​

可选参数列表,设置上报频率,默认值为200000000ns。

示例:

sensor.on(sensor.SensorType.SENSOR_TYPE_ID_SIGNIFICANT_MOTION,function(data){
    console.info('Scalar data: ' + data.scalar);
},
    {interval: 10000000}
);

PEDOMETER_DETECTION(deprecated)

on(type: SensorType.SENSOR_TYPE_ID_PEDOMETER_DETECTION, callback: Callback<PedometerDetectionResponse>, options?: Options): void

监听计步检测传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。

从API version 9 开始不再维护,建议使用​​sensor.on.PEDOMETER_DETECTION​​代替。

需要权限:ohos.permission.ACTIVITY_MOTION

系统能力:SystemCapability.Sensors.Sensor

参数:

参数名

类型

必填

说明

type

​SensorType​​.SENSOR_TYPE_ID_PEDOMETER_DETECTION

要订阅的计步检测传感器类型为SENSOR_TYPE_ID_PEDOMETER_DETECTION。

callback

Callback<​​PedometerDetectionResponse​​>

注册计步检测传感器的回调函数,上报的数据类型为PedometerDetectionResponse。

options

​Options​

可选参数列表,设置上报频率,默认值为200000000ns。

示例:

sensor.on(sensor.SensorType.SENSOR_TYPE_ID_PEDOMETER_DETECTION,function(data){
    console.info('Scalar data: ' + data.scalar);
},
    {interval: 10000000}
);

PEDOMETER(deprecated)

on(type: SensorType.SENSOR_TYPE_ID_PEDOMETER, callback: Callback<PedometerResponse>, options?: Options): void

监听计步传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。

从API version 9 开始不再维护,建议使用​​sensor.on.PEDOMETER​​代替。

需要权限:ohos.permission.ACTIVITY_MOTION

系统能力:SystemCapability.Sensors.Sensor

参数:

参数名

类型

必填

说明

type

​SensorType​​.SENSOR_TYPE_ID_PEDOMETER

要订阅的计步传感器类型为SENSOR_TYPE_ID_PEDOMETER。

callback

Callback<​​PedometerResponse​​>

注册计步传感器的回调函数,上报的数据类型为PedometerResponse。

options

​Options​

可选参数列表,设置上报频率,默认值为200000000ns。

示例:

sensor.on(sensor.SensorType.SENSOR_TYPE_ID_PEDOMETER,function(data){
    console.info('Steps: ' + data.steps);
},
    {interval: 10000000}
);

AMBIENT_TEMPERATURE(deprecated)

on(type: SensorType.SENSOR_TYPE_ID_AMBIENT_TEMPERATURE,callback:Callback<AmbientTemperatureResponse>, options?: Options): void

监听环境温度传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。

从API version 9 开始不再维护,建议使用​​sensor.on.AMBIENT_TEMPERATURE​​代替。

系统能力:SystemCapability.Sensors.Sensor

参数:

参数名

类型

必填

说明

type

​SensorType​​.SENSOR_TYPE_ID_AMBIENT_TEMPERATURE

要订阅的环境温度传感器类型为SENSOR_TYPE_ID_AMBIENT_TEMPERATURE。

callback

Callback<​​AmbientTemperatureResponse​​>

注册环境温度传感器的回调函数,上报的数据类型为AmbientTemperatureResponse。

options

​Options​

可选参数列表,设置上报频率,默认值为200000000ns。

示例:

sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_TEMPERATURE,function(data){
    console.info('Temperature: ' + data.temperature);
},
    {interval: 10000000}
);

MAGNETIC_FIELD(deprecated)

on(type: SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD, callback: Callback<MagneticFieldResponse>,options?: Options): void

监听磁场传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。

从API version 9 开始不再维护,建议使用​​sensor.on.MAGNETIC_FIELD​​代替。

系统能力:SystemCapability.Sensors.Sensor

参数:

参数名

类型

必填

说明

type

​SensorType​​.SENSOR_TYPE_ID_MAGNETIC_FIELD

要订阅的磁场传感器类型为SENSOR_TYPE_ID_MAGNETIC_FIELD。

callback

Callback<​​MagneticFieldResponse​​>

注册磁场传感器的回调函数,上报的数据类型为MagneticFieldResponse。

options

​Options​

可选参数列表,设置上报频率,默认值为200000000ns。

示例:

sensor.on(sensor.SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD,function(data){
    console.info('X-coordinate component: ' + data.x);
    console.info('Y-coordinate component: ' + data.y);
    console.info('Z-coordinate component: ' + data.z);
},
    {interval: 10000000}
);

MAGNETIC_FIELD_UNCALIBRATED(deprecated)

on(type: SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED,callback: Callback<MagneticFieldUncalibratedResponse>, options?: Options): void

监听未校准磁场传感器的数据变化。如果多次调用该接口,仅最后一次调用生效。

从API version 9 开始不再维护,建议使用​​sensor.on.MAGNETIC_FIELD_UNCALIBRATED​​代替。

系统能力:SystemCapability.Sensors.Sensor

参数:

参数名

类型

必填

说明

type

​SensorType​​.SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED

要订阅的未校准磁场传感器类型为SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED。

callback

Callback<​​MagneticFieldUncalibratedResponse​​>

注册未校准磁场传感器的回调函数,上报的数据类型为MagneticFieldUncalibratedResponse。

options

​Options​

可选参数列表,设置上报频率,默认值为200000000ns。

示例:

sensor.on(sensor.SensorType.SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED,function(data){
    console.info('X-coordinate component: ' + data.x);
    console.info('Y-coordinate component: ' + data.y);
    console.info('Z-coordinate component: ' + data.z);
    console.info('X-coordinate bias: ' + data.biasX);
    console.info('Y-coordinate bias: ' + data.biasY);
    console.info('Z-coordinate bias: ' + data.biasZ);
},
    {interval: 10000000}
);


文章转载自:​​https://developer.harmonyos.com/cn/docs/documentation/doc-references-V3/js-apis-sensor-0000001500954573-V3?catalogVersion=V3#ZH-CN_TOPIC_0000001500954573__sensorgetinclination9​​​

已于2023-4-10 18:07:43修改
收藏
回复
举报
回复
    相关推荐