Sensor

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

Sensor API

The Sensor API makes sensor data available to the SmartWatch 2 apps, for example light and accelerometer readings. The Sensor API contains a number of classes to manage the sensor type, to capture sensor events, and to query various parameters for each type of sensor.

For information on what sensors are supported in the SmartWatch 2, see the Hardware specifications and API compatibility guide.

The basics

To get sensor data, in this example, accelerometer data, perform the following steps:

  1. Import the packages necessary for the Sensor API.
  2. Create an ArrayList of AccessorySensor objects .
  3. Add the sensor to the ArrayList.
  4. Register a listener.
  5. Get the values.

Note: Getting other sensor data should be analogous to this process.

Import the packages necessary for the Sensor API

First, import the following packages included in the Sony Add-on SDK package:

import com.sonyericsson.extras.liveware.aef.registration.Registration.SensorTypeValue;
import com.sonyericsson.extras.liveware.aef.sensor.Sensor;
import com.sonyericsson.extras.liveware.aef.sensor.Sensor.SensorAccuracy;
import com.sonyericsson.extras.liveware.extension.util.sensor.AccessorySensor;
import com.sonyericsson.extras.liveware.extension.util.sensor.AccessorySensorEvent;
import com.sonyericsson.extras.liveware.extension.util.sensor.AccessorySensorEventListener;
import com.sonyericsson.extras.liveware.extension.util.sensor.AccessorySensorException;
import com.sonyericsson.extras.liveware.extension.util.sensor.AccessorySensorManager;

Create an ArrayList of AccessorySensor objects

Next, define a member variable to hold the AccessorySensor objects as follows:

private List mSensors = new ArrayList();

Add the sensor to the ArrayList

Next, add the sensor to the list you just created as follows:

if (DeviceInfoHelper.isSensorSupported(context, hostAppPackageName,
                SensorTypeValue.ACCELEROMETER)) {
            mSensors.add(manager.getSensor(SensorTypeValue.ACCELEROMETER));
        }

Register a listener

Next, register a listener. You have the option of registering a fixed rate listener or an interrupt listener as follows:

sensor.registerInterruptListener(mListener);
sensor.registerFixedRateListener(mListener, Sensor.SensorRates.SENSOR_DELAY_UI)

Get the values

Finally, in your listener, override the onSensorEvent() method which returns an AccessorySensorEvent object with the sensor data as follows:

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

Sensor code example

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