文档管理中心

您当前正在浏览HarmonyOS开发者3.0版本配套的开发者文档,对应API能力级别为API 8 Release。此版本文档已归档不再维护,推荐您使用HarmonyOS NEXT版本

API参考Java API参考ohos.sensor.agentClassCategoryMotionAgent

CategoryMotionAgent

java.lang.Object

|---ohos.sensor.agent.SensorAgent<CategoryMotion,CategoryMotionData,ICategoryMotionDataCallback>

|---|---ohos.sensor.agent.CategoryMotionAgent

收起
自动换行
深色代码主题
复制
Inherits from SensorAgent and manages motion sensors on hardware devices.

This class is used to obtain all motion sensors, a type of sensors in the motion sensor category, and the first sensor of a specific type, and set or release the sensor data callback.

When you need to obtain information such as the acceleration, gravity, and gyroscope, you can call this class to obtain a specific sensor and set the sensor data callback. You can also release the sensor data callback when the sensor data is unneeded.

Since:

1

See Also:

SensorBase, SensorAgent, CategoryMotion, CategoryMotionData, ICategoryMotionDataCallback

Constructor Summary

展开

Constructor

Description

CategoryMotionAgent​()

Default constructor used to create a CategoryMotionAgent instance.

Method Summary

展开

Modifier and Type

Method

Description

List<CategoryMotion>

getAllSensors​()

Obtains all motion sensors.

List<CategoryMotion>

getAllSensors​(int sensorType)

Obtains a specified type of sensors in the motion category.

CategoryMotion

getSingleSensor​(int sensorType)

Obtains the default sensor of a specified type in the motion category.

boolean

releaseSensorDataCallback​(ICategoryMotionDataCallback callback)

Releases the callback for all motion sensors.

boolean

releaseSensorDataCallback​(ICategoryMotionDataCallback callback, CategoryMotion sensor)

Releases the callback for a motion sensor.

boolean

setSensorDataCallback​(ICategoryMotionDataCallback callback, CategoryMotion sensor, int mode)

Sets a callback for a motion sensor to receive sensor data in a specified sampling mode.

boolean

setSensorDataCallback​(ICategoryMotionDataCallback callback, CategoryMotion sensor, int mode, long maxDelay)

Sets a callback for a motion sensor to receive sensor data in a specified sampling mode based on the maximum event reporting latency.

boolean

setSensorDataCallback​(ICategoryMotionDataCallback callback, CategoryMotion sensor, long interval)

Sets a callback for a motion sensor to receive the sensor data at a specified sampling interval.

boolean

setSensorDataCallback​(ICategoryMotionDataCallback callback, CategoryMotion sensor, long interval, long maxDelay)

Sets a callback for a motion sensor to receive the sensor data at a specified sampling interval based on the maximum event reporting latency.

展开

Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

展开

Methods inherited from class ohos.sensor.agent.SensorAgent

getSensorMinSampleInterval, runCommand, subscribeParamsCheck

Constructor Detail

CategoryMotionAgent

public CategoryMotionAgent​()

Default constructor used to create a CategoryMotionAgent instance.

Since:

1

Method Detail

getAllSensors

public List<CategoryMotion> getAllSensors​()

Obtains all motion sensors.

Motion sensors include acceleration sensors, uncalibrated acceleration sensors, linear acceleration sensors, gravity sensors, gyroscope sensors, uncalibrated gyroscope sensors, significant motion sensors, and drop detection sensors.

Specified by:

getAllSensors in class SensorAgent<CategoryMotion,CategoryMotionData,ICategoryMotionDataCallback>

Returns:

Returns a list of motion sensors.

Since:

1

See Also:

getAllSensors(int), getSingleSensor(int), CategoryMotion

getAllSensors

public List<CategoryMotion> getAllSensors​(int sensorType)

Obtains a specified type of sensors in the motion category.

Specified by:

getAllSensors in class SensorAgent<CategoryMotion,CategoryMotionData,ICategoryMotionDataCallback>

Parameters:

展开

Parameter Name

Parameter Description

sensorType

Indicates a sensor type in the motion category. CategoryMotion defines the sensor types using integer constants. For example, you can set CategoryMotion.SENSOR_TYPE_ACCELEROMETER for this parameter to obtain acceleration sensors.

Returns:

Returns a list of sensors of the specified type; returns an empty list if the specified sensor type does not exist.

Since:

1

See Also:

getAllSensors(), getSingleSensor(int), CategoryMotion

getSingleSensor

public CategoryMotion getSingleSensor​(int sensorType)

Obtains the default sensor of a specified type in the motion category.

The default sensor is the first sensor of a specified type in the motion category.

Specified by:

getSingleSensor in class SensorAgent<CategoryMotion,CategoryMotionData,ICategoryMotionDataCallback>

Parameters:

展开

Parameter Name

Parameter Description

sensorType

Indicates a sensor type in the motion category. CategoryMotion defines the sensor types using integer constants. For example, you can set CategoryMotion.SENSOR_TYPE_ACCELEROMETER for this parameter to obtain acceleration sensors.

Returns:

Returns the first sensor of the specified type in the motion category if multiple sensors of this type are available; returns null if the specified sensor type does not exist.

Since:

1

See Also:

getAllSensors(), getAllSensors(int), CategoryMotion

setSensorDataCallback

public boolean setSensorDataCallback​(ICategoryMotionDataCallback callback, CategoryMotion sensor, long interval)

Sets a callback for a motion sensor to receive the sensor data at a specified sampling interval.

Parameters:

展开

Parameter Name

Parameter Description

callback

Indicates the callback ICategoryMotionDataCallback to set for receiving motion sensor data. If this parameter is null, setting the callback fails.

sensor

Indicates the sensor for which the callback is to be set. If this parameter is null, setting the callback fails.

interval

Indicates the sampling interval, in nanoseconds. The sampling interval may vary depending on the sensor type. You can use the SensorBase.getMinInterval() and SensorBase.getMaxInterval() methods to obtain the minimum sampling interval and maximum sampling interval of the sensor, respectively, and then set the value of this parameter in between. If the configured parameter value is smaller than the minimum sampling interval of the sensor, the minimum sampling interval is used. If the configured parameter value is larger than the maximum sampling interval of the sensor, the maximum sampling interval is used.

Returns:

Returns true if the callback is set successfully; returns false otherwise.

Since:

1

See Also:

setSensorDataCallback(ICategoryMotionDataCallback, CategoryMotion, long, long), ICategoryMotionDataCallback, CategoryMotion

setSensorDataCallback

public boolean setSensorDataCallback​(ICategoryMotionDataCallback callback, CategoryMotion sensor, long interval, long maxDelay)

Sets a callback for a motion sensor to receive the sensor data at a specified sampling interval based on the maximum event reporting latency.

Specified by:

setSensorDataCallback in class SensorAgent<CategoryMotion,CategoryMotionData,ICategoryMotionDataCallback>

Parameters:

展开

Parameter Name

Parameter Description

callback

Indicates the callback ICategoryMotionDataCallback to set for receiving motion sensor data. If this parameter is null, setting the callback fails.

sensor

Indicates the sensor for which the callback is to be set. If this parameter is null, setting the callback fails.

interval

Indicates the sampling interval, in nanoseconds. The sampling interval may vary depending on the sensor type. You can use the SensorBase.getMinInterval() and SensorBase.getMaxInterval() methods to obtain the minimum sampling interval and maximum sampling interval of the sensor, respectively, and then set the value of this parameter in between. If the configured parameter value is smaller than the minimum sampling interval of the sensor, the minimum sampling interval is used. If the configured parameter value is larger than the maximum sampling interval of the sensor, the maximum sampling interval is used.

maxDelay

Indicates the maximum latency for the sensor to report events to the application in nanoseconds, based on the first in first out (FIFO) policy of the hardware. The maximum latency may vary depending on the sensor.

Returns:

Returns true if the callback is set successfully; returns false otherwise.

Since:

1

See Also:

ICategoryMotionDataCallback, CategoryMotion

setSensorDataCallback

public boolean setSensorDataCallback​(ICategoryMotionDataCallback callback, CategoryMotion sensor, int mode)

Sets a callback for a motion sensor to receive sensor data in a specified sampling mode.

Specified by:

setSensorDataCallback in class SensorAgent<CategoryMotion,CategoryMotionData,ICategoryMotionDataCallback>

Parameters:

展开

Parameter Name

Parameter Description

callback

Indicates the callback ICategoryMotionDataCallback to set for receiving motion sensor data. If this parameter is null, setting the callback fails.

sensor

Indicates the sensor for which the callback is to be set. If this parameter is null, setting the callback fails.

mode

Indicates the sampling mode. Each sampling mode represents a sampling rate, including the fastest sampling rate, sampling rate in gaming scenarios, sampling rate on UIs, and default sampling rate. For example, you can set the fastest sampling rate of the sensor via SensorAgent#SENSOR_SAMPLING_RATE_FASTEST.

Returns:

Returns true if the callback is set successfully; returns false otherwise.

Since:

4

setSensorDataCallback

public boolean setSensorDataCallback​(ICategoryMotionDataCallback callback, CategoryMotion sensor, int mode, long maxDelay)

Sets a callback for a motion sensor to receive sensor data in a specified sampling mode based on the maximum event reporting latency.

Specified by:

setSensorDataCallback in class SensorAgent<CategoryMotion,CategoryMotionData,ICategoryMotionDataCallback>

Parameters:

展开

Parameter Name

Parameter Description

callback

Indicates the callback ICategoryMotionDataCallback to set for receiving motion sensor data. If this parameter is null, setting the callback fails.

sensor

Indicates the sensor for which the callback is to be set. If this parameter is null, setting the callback fails.

mode

Indicates the sampling mode. Each sampling mode represents a sampling rate, including the fastest sampling rate, sampling rate in gaming scenarios, sampling rate on UIs, and default sampling rate. For example, you can set the fastest sampling rate of the sensor via SensorAgent#SENSOR_SAMPLING_RATE_FASTEST.

maxDelay

Indicates the maximum latency (in nanoseconds) for the sensor to report events to the application, based on the first in first out (FIFO) policy of the hardware. The maximum latency may vary depending on the sensor.

Returns:

Returns true if the callback is set successfully; returns false otherwise.

Since:

4

releaseSensorDataCallback

public boolean releaseSensorDataCallback​(ICategoryMotionDataCallback callback, CategoryMotion sensor)

Releases the callback for a motion sensor.

Specified by:

releaseSensorDataCallback in class SensorAgent<CategoryMotion,CategoryMotionData,ICategoryMotionDataCallback>

Parameters:

展开

Parameter Name

Parameter Description

callback

Indicates the callback ICategoryMotionDataCallback set for receiving motion sensor data. If this parameter is null, releasing the callback fails.

sensor

Indicates the sensor for which the callback is to be released. If this parameter is null, the callback of all motion sensors will be released.

Returns:

Returns true if the callback is released successfully; returns false otherwise.

Since:

1

See Also:

ICategoryMotionDataCallback, CategoryMotion

releaseSensorDataCallback

public boolean releaseSensorDataCallback​(ICategoryMotionDataCallback callback)

Releases the callback for all motion sensors.

Specified by:

releaseSensorDataCallback in class SensorAgent<CategoryMotion,CategoryMotionData,ICategoryMotionDataCallback>

Parameters:

展开

Parameter Name

Parameter Description

callback

Indicates the callback ICategoryMotionDataCallback set for receiving motion sensor data. If this parameter is null, releasing the callback fails.

Returns:

Returns true if the callback is released successfully; returns false otherwise.

Since:

1

See Also:

ICategoryMotionDataCallback

搜索
请输入您想要搜索的关键词