Online documentation:
Home
What Sensor Data?
Collecting Sensor Data
Formatting Sensor Data
Store/Transfer Data
Research
Contributing
Project Github Page
All control of sensor data collection is done via the ES Sensor Manager singleton.
ESSensorManager sm = ESSensorManager.getSensorManager(context);
To collect a single sample of sensor data using preconfigured settings, use the getDataFromSensor(int sensorId)
method. This blocking call returns an object of type SensorData
, which can then be cast to the specific kind of sensor data you have sampled. Each sensor in the library is assigned a unique numeric identifier: these are listed in SensorUtils.java
.
Here is an example of getting a sample from the accelerometer:
AccelerometerData d = (AccelerometerData) sm.getDataFromSensor(SensorUtils.SENSOR_TYPE_ACCELEROMETER);
To regularly receive data from a sensor, issue a subscription for it. Subscribing to a sensor using the ES Sensor Manager requires including the battery permissions.
public class ExampleSensorListener implements SensorDataListener
{
public void onDataSensed(SensorData data)
{
// This method will be called by the ES Sensor Manager when it has new data to publish
// and lets you decide what actions to take with that data.
}
public void onCrossingLowBatteryThreshold(boolean isBelowThreshold)
{
// This method will be called by the ES Sensor Manager when the phone's battery falls
// below a pre-defined, configurable threshold, and again when the phone has been charged
// above that threshold.
}
}
Here is an example of subscribing to receive accelerometer data:
SensorDataListener listener = new ExampleSensorListener();
int id = sm.subscribeToSensorData(SensorUtils.SENSOR_TYPE_ACCELEROMETER, listener);
The integer that is returned from this method is your subscription id.
If needed, you can pause and resume your subscription using your subscription id.
sm.pauseSubscription(id);
sm.unPauseSubscription(id);
When you have finished with your subscription, you can unsubscribe:
sm.unsubscribeFromSensorData(id);
The ES Sensor Manager instance stores your current preferences as a set of key-value pairs.
At a global level, you can control the ES Sensor Manager's low battery threshold, whether it should print Log.d status messages, and whether it should acquire a wake lock when it begins sensing.
For example, to get and then set the low battery threshold:
int lowBatteryThresh = (int) sm.getParameter(GlobalConfig.LOW_BATTERY_THRESHOLD);
sm.setGlobalConfig(GlobalConfig.LOW_BATTERY_THRESHOLD, 50);
At the sensor level, you can set a variety of parameters that are determined by the sensor's type (e.g., pull or push).
For example, to get the accelerometer's sensing window length:
long l = (long) sm.getSensorConfig(SensorUtils.SENSOR_TYPE_ACCELEROMETER,
PullSensorConfig.SENSE_WINDOW_LENGTH_MILLIS);
And to set it to 4 seconds:
sm.setSensorConfig(SensorUtils.SENSOR_TYPE_ACCELEROMETER,
PullSensorConfig.SENSE_WINDOW_LENGTH_MILLIS, 4 * 1000L);
For a full list of the parameters you can set, please refer to the config package in the library.