Sensors

Sensors are fun. Everybody likes them and everybody wants to use them. The way to use sensors is very similar to the way to use location providers: your application registers a sensor listener with a specific sensor and is notified of updates. Listing 7–15 shows how you can register a listener with the device’s accelerometer.

Listing 7–15. Registering Sensor Listener With Accelerometer

    private void registerWithAccelerometer() {
        SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

        List<Sensor> sensors = sm.getSensorList(Sensor.TYPE_ACCELEROMETER);

        if (sensors != null&& ! sensors.isEmpty()) {
            SensorEventListener listener = new SensorEventListener() {

                @Override
                public void onAccuracyChanged(Sensor sensor, int accuracy) {
                    Log.i(TAG, "Accuracy changed to " + accuracy);
                }

                @Override
                public void onSensorChanged(SensorEvent event) {
                    /*
                    * Accelerometer: array of 3 values
                    *
                    * event.values[0] = acceleration minus Gx on the x-axis
                    * event.values[1] = acceleration minus Gy on the y-axis
                    * event.values[2] = acceleration minus Gz on the z-axis
                    */

                   Log.i(TAG, String.format("x:%.2f y:%.2f z:%.2f ",
                       event.values[0], event.values[1], event.values[2]));

                   // do something interesting here
                }
            };

            // we simply pick the first one
            Sensor sensor = sensors.get(0);
            Log.d(TAG, "Using sensor " + sensor.getName() + " from " +
sensor.getVendor());

            sm.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
        }
    }

In the same manner we could specify how often we wanted location updates, Android lets applications specify how often they want to receive sensor updates. While we used milliseconds with the location providers, we have to use one of four possible values to specify how often we want to receive sensor updates:

  • SENSOR_DELAY_NORMAL
  • SENSOR_DELAY_UI
  • SENSOR_DELAY_GAME
  • SENSOR_DELAY_FASTEST

On a Galaxy Tab 10.1 (using an MPL accelerometer from Invensense), the accelerometer’s NORMAL, UI, GAME, and FASTEST delays are about 180, 60, 20, and 10 milliseconds respectively. While these numbers vary depending on devices, faster updates will require more power. For example, on the Android G1 phone, using the NORMAL delay draws 10 mA from the battery while using the FASTEST delay draws 90 mA (15 mA for UI and 80 mA for GAME).

Like for location providers, reducing the frequency of the notification is the best way to preserve battery life. Since every device is different, your application can measure the frequency of the notifications for each of the four delays and choose the one that gives the best user experience while still conserving power. Another strategy, which may not apply to all applications, is to use the NORMAL or UI delay when you detect that the values don’t seem to change that much, and switch to a GAME or FASTEST delay when you detect sudden variations. Such strategy may give acceptable results for some applications and would result in a longer battery life.

Like other listeners, the sensor listeners should be disabled whenever notifications are not needed. For this purpose, use the SensorManager’s unregisterListener() methods.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.119.136