For most devices, sensors need to be configured before they start sending data. This configuration can include setting the sampling rate, enabling or disabling specific sensors, and other parameters that may vary by device.
To configure sensors, you first need to access the SensorConfiguration you want to configure. This can be done in two ways:
-
Using
relatedConfigurations
If you have aSensoryou want to configure, you can access its related sensor configurations directly:List<SensorConfiguration> configurations = sensor.relatedConfigurations;
-
Using
SensorConfigurationManager
If you have aWearablethat implementsSensorConfigurationManager, you can access the configurations like this:final sensorConfigurationManager = wearable.getCapability<SensorConfigurationManager>(); if (sensorConfigurationManager != null) { List<SensorConfiguration> configurations = sensorConfigurationManager.sensorConfigurations; }
Once you have a SensorConfiguration object, you can configure the sensor by applying a specific SensorConfigurationValue. The available values are accessible via the values property:
SensorConfiguration configuration;
List<SensorConfigurationValue> values = configuration.values;
SensorConfigurationValue valueToSet = values.firstWhere(...);
configuration.setConfiguration(valueToSet);Each SensorConfigurationValue can be identified by its key property.
To disable a sensor (e.g., to conserve power), set its configuration to the SensorConfigurationValue found in the offValue property of the SensorConfiguration:
SensorConfigurationValue? offValue = configuration.offValue;
if (offValue != null) {
configuration.setConfiguration(offValue);
}Sensor configurations can vary depending on the device and the supported sensors. Each configuration accepts a specific subtype of SensorConfigurationValue.
Sensors that support frequency control extend the SensorFrequencyConfiguration class and accept SensorFrequencyConfigurationValue instances. You can set the frequency either by choosing a value from values, or by using the convenience methods:
setFrequencyBestEffort(int targetFrequency)setMaximumFrequency()
The actual frequency of a SensorFrequencyConfigurationValue is available via its frequencyHz property.
Some sensors support configurable modes or features. These configurations extend the ConfigurableSensorConfiguration class and accept ConfigurableSensorConfigurationValue objects. The list of all available options for the configuration can be accessed through its availableOptions property.
Each ConfigurableSensorConfigurationValue sets a specific subset of options, which can be accessed via the options property.
Some sensors support a "Record on Device" option, allowing them to store data locally instead of streaming it. You can check for this feature by verifying whether a RecordSensorConfigOption is present in the options list of a ConfigurableSensorConfigurationValue.
Sensors that support real-time data streaming may offer a "Streaming Data" option. To check if it's available, look for a StreamSensorConfigOption in the options of a ConfigurableSensorConfigurationValue.