智能客服
你问我答,随时在线为你解决问题

























您当前正在浏览HarmonyOS开发者3.0版本配套的开发者文档,对应API能力级别为API 8 Release。此版本文档已归档不再维护,推荐您使用HarmonyOS NEXT版本。
| |---ohos.sensor.agent.SensorAgent<CategoryMotion,CategoryMotionData,ICategoryMotionDataCallback> |---|---ohos.sensor.agent.CategoryMotionAgent 收起 自动换行 深色代码主题 复制 |
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:
1See Also:
SensorBase, SensorAgent, CategoryMotion, CategoryMotionData, ICategoryMotionDataCallbackConstructor | Description |
|---|---|
Default constructor used to create a CategoryMotionAgent instance. |
Modifier and Type | Method | Description |
|---|---|---|
Obtains all motion sensors. | ||
getAllSensors(int sensorType) | Obtains a specified type of sensors in the motion category. | |
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 ohos.sensor.agent.SensorAgent |
|---|
getSensorMinSampleInterval, runCommand, subscribeParamsCheck |
public CategoryMotionAgent()
Default constructor used to create a CategoryMotionAgent instance.Since:
1
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:
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:
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:
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
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:
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
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
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:
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:
智能客服
你问我答,随时在线为你解决问题
合作咨询
我们的专家服务团队将竭诚为您提供专业的合作咨询服务
解决方案
精准高效的一站式服务支持,助力开发者商业成功