Sensor data

This guide explains how to access sensor data from your SmartEyeglass app.

Data from device sensors, such as the light sensor and accelerometer, is available to you through the Sensor API defined by AccessorySensorManager in the SmartExtensionsUtils library. The methods in this class allow you to manage sensors of different types, capture sensor events, and query various parameters for each type of sensor.

For specific information on the sensors that SmartEyeglass supports, see the Hardware overview page.

The basics

To get started working with sensor data, set up a project using the HelloSensors sample as a base; see more about creating SmartEyeglass projects in the How to create your first SmartEyeglass project tutorial.

You will use the Sensor API for listening to sensor data, so the class that extends RegistrationInformation must get the version that supports this functionality:

@Override
public int getRequiredSensorApiVersion() {
    return 1;
}

The HelloSensors project does this. If you don’t use this sample as a base, make sure you do the same.

Select sensors to listen to

Instantiate an AccessorySensorManager member, and use it to start listening to the sensors you are interested in. See the API references for a complete list of sensors that are available. You can also check at run time to see if a particular sensor is available on the Sony accessory that your extension is currently running on:

List mSensors = new ArrayList();
AccessorySensorManager manager = new AccessorySensorManager(context, hostAppPackageName);

    // Add accelerometer, if supported by the host application.
    if (DeviceInfoHelper.isSensorSupported(context, hostAppPackageName,
                                           SensorTypeValue.ACCELEROMETER)) {
        mSensors.add(manager.getSensor(SensorTypeValue.ACCELEROMETER));
    }

    // Add magnetic field sensor, if supported by the host application.
    if (DeviceInfoHelper.isSensorSupported(context, hostAppPackageName,
                                           SensorTypeValue.MAGNETIC_FIELD)) {
        mSensors.add(manager.getSensor(SensorTypeValue.MAGNETIC_FIELD));
    }

Register a sensor listener

To respond to sensor input, you must register event listeners for the events of interest. You can create either a fixed- rate listener that polls at specified intervals, or an interrupt listener:

//Fixed-rate listener
sensor.registerFixedRateListener(mListener, Sensor.SensorRates.SENSOR_DELAY_UI);
//Interrupt listener
sensor.registerInterruptListener(mListener);

An event listener can be active for only one sensor at a time. If you have been listening to events for one sensor and wish to switch to another sensor, you must unregister the current listener and register anew listener. See the sample for an example of how to switch sensor listeners.

Get data values

In your listener, override the onSensorEvent() method in order to access the returned AccessorySensorEvent object that contains the sensor data. For example:

private final AccessorySensorEventListener mListener = new AccessorySensorEventListener() {
   @Override
   public void onSensorEvent(AccessorySensorEvent sensorEvent) {
   	float[] values = sensorEvent.getSensorValues();
TextView xView = (TextView)sensorLayout.findViewById(R.id.sensor_value_x);
TextView yView = (TextView)sensorLayout.findViewById(R.id.sensor_value_y);
TextView zView = (TextView)sensorLayout.findViewById(R.id.sensor_value_z);

// Show values with one decimal.
xView.setText(String.format("%.1f", values[0]));
yView.setText(String.format("%.1f", values[1]));
zView.setText(String.format("%.1f", values[2]));
       }
   };

Rotation vector data

The sensor data values returned for the rotation vector are reversed in direction from the Android standard. The axis values represent rotations toward these directions:

  SmartEyeglass Android
X-axis points North points East
Y-axis points East points North
Z-axis points toward ground points toward sky

For both SmartEyeglass and standard Android, the Z-axis value is perpendicular to the ground plane.

SmartEyeglass rotation vector coordinates

Rotation vector coordinate directions.

For more information, see the sensor documentation on Google’s Android developer website:

• Learn about motion sensors.
• Check out Google’s sensor overview.

Learn from the code example

For the full code example of how to get sensor data, see the HelloSensors sample project in the /samples folder of the SmartEyeglass SDK.

Comments 0

Sort by:

Showing 0 of 0 comments. Show all comments