S - Indicates a sensor.D - Indicates sensor data.L - Indicates the callback for receiving sensor data.CategoryBodyAgent, CategoryEnvironmentAgent, CategoryLightAgent, CategoryMotionAgent, CategoryOrientationAgent, CategoryOtherAgentpublic abstract class SensorAgent<S extends SensorBase,D extends SensorData<S>,L extends ISensorDataCallback<D,S>> extends Object
This class abstracts sensors on hardware devices and provides abstract methods.
SystemCapability.Sensors#SENSOR| 限定符和类型 | 字段 | 说明 |
|---|---|---|
static int |
SENSOR_CATEGORY_BODY |
Describes body sensors.
|
static int |
SENSOR_CATEGORY_DEVICEMOTION |
Describes motion sensors of devices.
|
static int |
SENSOR_CATEGORY_ENVIRONMENT |
Describes environment sensors.
|
static int |
SENSOR_CATEGORY_LIGHT |
Describes light sensors.
|
static int |
SENSOR_CATEGORY_ORIENTATION |
Describes orientation sensors.
|
static int |
SENSOR_CATEGORY_OTHER |
Describes other sensors.
|
static int |
SENSOR_SAMPLING_RATE_FASTEST |
Indicates the fastest sampling rate for obtaining sensor data.
|
static int |
SENSOR_SAMPLING_RATE_GAME |
Indicates the sampling rate for obtaining sensor data in gaming scenarios.
|
static int |
SENSOR_SAMPLING_RATE_NORMAL |
Indicates the default sampling rate for obtaining sensor data, for example, in screen
rotation scenarios.
|
static int |
SENSOR_SAMPLING_RATE_UI |
Indicates the sampling rate for obtaining sensor data on UIs.
|
static String |
STRING_SENSOR_CATEGORY_BODY |
Describes body sensors using a string of characters.
|
static String |
STRING_SENSOR_CATEGORY_DEVICEMOTION |
Describes motion sensors using a string of characters.
|
static String |
STRING_SENSOR_CATEGORY_ENVIRONMENT |
Describes environment sensors using a string of characters.
|
static String |
STRING_SENSOR_CATEGORY_LIGHT |
Describes light sensors using a string of characters.
|
static String |
STRING_SENSOR_CATEGORY_ORIENTATION |
Describes orientation sensors using a string of characters.
|
static String |
STRING_SENSOR_CATEGORY_OTHER |
Describes sensors in the other category (a category other than motion, environment,
orientation, light, and body) using a string of characters.
|
| 构造器 | 说明 |
|---|---|
SensorAgent() |
Default constructor used to create a
SensorAgent instance. |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
abstract List<S> |
getAllSensors() |
Obtains a specified category of sensors.
|
abstract List<S> |
getAllSensors(int sensorType) |
Obtains a specified type of sensors in a sensor category.
|
long |
getSensorMinSampleInterval(int sensorId) |
Queries the minimum sampling interval of a sensor.
|
abstract S |
getSingleSensor(int sensorType) |
Obtains the default sensor of a specified type in a sensor category.
|
abstract boolean |
releaseSensorDataCallback(L callback) |
Releases the callback for all sensors.
|
abstract boolean |
releaseSensorDataCallback(L callback,
S sensor) |
Releases the callback for a sensor.
|
int |
runCommand(int sensorId,
int commandType,
int commandParameter) |
Executes the specified command for a sensor based on the given parameter.
|
abstract boolean |
setSensorDataCallback(L callback,
S sensor,
int mode) |
Sets a callback for a sensor to receive sensor data in a specified sampling mode.
|
abstract boolean |
setSensorDataCallback(L callback,
S sensor,
int mode,
long maxDelay) |
Sets a callback for a sensor to receive sensor data in a specified sampling mode based on
the maximum event reporting latency.
|
abstract boolean |
setSensorDataCallback(L callback,
S sensor,
long interval,
long maxDelay) |
Sets a callback for a sensor to receive the sensor data at a specified sampling interval
based on the maximum event reporting latency.
|
protected boolean |
subscribeParamsCheck(L callback,
S sensor,
long interval,
long maxDelay) |
Checks the validity of subscription parameters.
|
public static final int SENSOR_CATEGORY_BODY
public static final int SENSOR_CATEGORY_DEVICEMOTION
public static final int SENSOR_CATEGORY_ENVIRONMENT
public static final int SENSOR_CATEGORY_LIGHT
public static final int SENSOR_CATEGORY_ORIENTATION
public static final int SENSOR_CATEGORY_OTHER
public static final int SENSOR_SAMPLING_RATE_FASTEST
public static final int SENSOR_SAMPLING_RATE_GAME
public static final int SENSOR_SAMPLING_RATE_NORMAL
public static final int SENSOR_SAMPLING_RATE_UI
public static final String STRING_SENSOR_CATEGORY_BODY
public static final String STRING_SENSOR_CATEGORY_DEVICEMOTION
public static final String STRING_SENSOR_CATEGORY_ENVIRONMENT
public static final String STRING_SENSOR_CATEGORY_LIGHT
public static final String STRING_SENSOR_CATEGORY_ORIENTATION
public SensorAgent()
SensorAgent instance.protected boolean subscribeParamsCheck(L callback, S sensor, long interval, long maxDelay)
callback - Indicates the callback for receiving the sensor data.sensor - Indicates the sensor for which the callback is to be set.interval - Indicates the sampling interval, in nanoseconds.maxDelay - Indicates the maximum latency for the sensor to report events to the
application, in nanoseconds.true if the input parameters are valid; returns false
otherwise.public abstract List<S> getAllSensors()
getAllSensors(int),
getSingleSensor(int)public abstract List<S> getAllSensors(int sensorType)
sensorType - Indicates a type in the sensor category.getAllSensors(),
getSingleSensor(int)public abstract S getSingleSensor(int sensorType)
The default sensor is the first sensor of a specified type in a sensor category.
sensorType - Indicates a type in the sensor category.getAllSensors(),
getAllSensors(int)public abstract boolean setSensorDataCallback(L callback, S sensor, long interval, long maxDelay)
callback - Indicates the callback to set for receiving 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.true if the callback is set successfully; returns false
otherwise.public abstract boolean setSensorDataCallback(L callback, S sensor, int mode)
callback - Indicates the callback to set for receiving 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.true if the callback is set successfully; returns false
otherwise.public abstract boolean setSensorDataCallback(L callback, S sensor, int mode, long maxDelay)
callback - Indicates the callback to set for receiving 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.true if the callback is set successfully; returns false
otherwise.public abstract boolean releaseSensorDataCallback(L callback, S sensor)
callback - Indicates the callback set for receiving 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 sensors in the category will be released.true if the callback is released successfully; returns false
otherwise.public abstract boolean releaseSensorDataCallback(L callback)
callback - Indicates the callback set for receiving sensor data. If this parameter is
null, releasing the callback fails.true if the callback is released successfully; returns false
otherwise.public long getSensorMinSampleInterval(int sensorId)
You can query this value and set the sampling interval accordingly.
sensorId - Indicates the ID of a sensor. You can use the
SensorBase.getSensorId() method to obtain the sensor ID.public int runCommand(int sensorId,
int commandType,
int commandParameter)
sensorId - Indicates the ID of a sensor. You can use the
SensorBase.getSensorId() method to obtain the sensor ID.commandType - Indicates the type of the command to execute.
SensorBase defines the constants to store the command types.
For example, you can pass SensorBase.COMMAND_TYPE_FLUSH to this parameter to flush
data.commandParameter - Indicates the parameter of the command to execute.
SensorBase defines the constants to store the command
parameters. For example, you can pass SensorBase.COMMAND_PARAMETER_FLUSH to this
parameter to flush data.