ES Libraries Documentation

Online documentation:
Home
What Sensor Data?
Collecting Sensor Data
Formatting Sensor Data
Store/Transfer Data
Research
Contributing
Project Github Page

Collecting Sensor Data

1. Start with the Sensor Manager

All control of sensor data collection is done via the ES Sensor Manager singleton.

ESSensorManager sm = ESSensorManager.getSensorManager(context);

2. Get a sample of sensor data

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);

3. Subscribe to a sensor's data

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.

a. Implement a sensor listener

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.
	}
}

b. Subscribe to the sensor

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.

c. Pause and resume your subscription

If needed, you can pause and resume your subscription using your subscription id.

sm.pauseSubscription(id);
sm.unPauseSubscription(id);

d. Unsubscribe from the sensor

When you have finished with your subscription, you can unsubscribe:

sm.unsubscribeFromSensorData(id);

4. Configure the Sensor Manager

The ES Sensor Manager instance stores your current preferences as a set of key-value pairs.

a. Get and set global configuration variables

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);

b. Get and set sensor-specific configuration variables

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.