Example usage for android.hardware Sensor TYPE_ACCELEROMETER

List of usage examples for android.hardware Sensor TYPE_ACCELEROMETER

Introduction

In this page you can find the example usage for android.hardware Sensor TYPE_ACCELEROMETER.

Prototype

int TYPE_ACCELEROMETER

To view the source code for android.hardware Sensor TYPE_ACCELEROMETER.

Click Source Link

Document

A constant describing an accelerometer sensor type.

Usage

From source file:com.example.zoeoeh.ETuner.OpenGLClass.java

@Override
public void onSensorChanged(SensorEvent event) {

    // this detects the gesture
    Sensor mySensor = event.sensor;/*  w  w w . j a v  a 2  s.  co m*/

    // increment for strings, changes with direction
    int increment = 0;

    if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        float x = event.values[0]; // values for each axis
        float y = event.values[1];
        float z = event.values[2];

        long curTime = System.currentTimeMillis();

        if ((curTime - lastUpdate) > 200) {
            long diffTime = (curTime - lastUpdate);
            lastUpdate = curTime;

            boolean right;
            String direction;

            float speed = (x - last_x);

            if (speed > 0) {
                right = true;
                direction = "Right";
                increment = 1;
            } else {
                right = false;
                direction = "left";
                increment = -1;
            }

            speed = Math.abs(speed + y + z - last_y - last_z) / diffTime * 10000;

            if (speed > SHAKE_THRESHOLD) {
                // either positive or negative depending on the direction.
                currentSoundIndex += increment;

                if (currentSoundIndex < 0) {
                    currentSoundIndex = myStrings.length - 1;
                } else if (currentSoundIndex > myStrings.length - 1) {
                    currentSoundIndex = 0;
                }

                // display toast
                Toast.makeText(TabSwitcher.getmContext(),
                        "You shook me " + direction + ". Now Playing " + guitarStrings[currentSoundIndex],
                        Toast.LENGTH_SHORT).show();

                // change flag
                shakeDirty = true;

                // if play is checked, update the string
                if (playChecked) {
                    updatePlay();
                }
            }

            last_x = x;
            last_y = y;
            last_z = z;

        }

    }
}

From source file:com.kircherelectronics.androidlinearacceleration.sensor.AccelerationSensor.java

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        System.arraycopy(event.values, 0, acceleration, 0, event.values.length);

        timeStamp = event.timestamp;/*from   w w w  .j a  v a2 s  .com*/

        if (vehicleMode) {
            acceleration = quaternionToDeviceVehicleMode(acceleration);
        }

        notifyAccelerationObserver();
    }
}

From source file:com.polyvi.xface.extension.XAccelerometerExt.java

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // Only look at accelerometer events
    if (sensor.getType() != Sensor.TYPE_ACCELEROMETER) {
        return;//from  w w  w.  j  av  a2 s  .c  o m
    }

    // If not running, then just return
    if (mStatus == STOPPED) {
        return;
    }
    mAccuracy = accuracy;
}

From source file:com.stillnojetpacks.huffr.activities.MainActivity.java

@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
    Log.i(TAG, "SOUNDPOOL LOAD COMPLETE for sampleId:" + sampleId);

    loaded.put(sampleId, Boolean.TRUE);

    if (allSoundsLoaded()) {

        Log.i(TAG, "ALL SOUNDS LOADED! registering sensorManager");
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

        Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        if (accelerometer == null) {
            Log.i(TAG, "accelerometer is null");
        } else {//from ww  w .j a  v a 2  s. c  om
            sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
        }

        sprayButton.setEnabled(true);
        capOff();
    }
}

From source file:org.apache.cordova.devicemotion.AccelListener.java

/**
 * Start listening for acceleration sensor.
 *
 * @return          status of listener/*from   w  ww.j  a v a2 s  . co  m*/
*/
private int start() {
    // If already starting or running, then restart timeout and return
    if ((this.status == AccelListener.RUNNING) || (this.status == AccelListener.STARTING)) {
        startTimeout();
        return this.status;
    }

    this.setStatus(AccelListener.STARTING);

    // Get accelerometer from sensor manager
    List<Sensor> list = this.sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);

    // If found, then register as listener
    if ((list != null) && (list.size() > 0)) {
        this.mSensor = list.get(0);
        if (this.sensorManager.registerListener(this, this.mSensor, SensorManager.SENSOR_DELAY_UI)) {
            this.setStatus(AccelListener.STARTING);
            // CB-11531: Mark accuracy as 'reliable' - this is complementary to
            // setting it to 'unreliable' 'stop' method
            this.accuracy = SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM;
        } else {
            this.setStatus(AccelListener.ERROR_FAILED_TO_START);
            this.fail(AccelListener.ERROR_FAILED_TO_START, "Device sensor returned an error.");
            return this.status;
        }
        ;

    } else {
        this.setStatus(AccelListener.ERROR_FAILED_TO_START);
        this.fail(AccelListener.ERROR_FAILED_TO_START,
                "No sensors found to register accelerometer listening to.");
        return this.status;
    }

    startTimeout();

    return this.status;
}

From source file:fr.bde_eseo.eseomega.GantierActivity.java

public void initListeners() {
    senSensorManager.registerListener(this, senSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_FASTEST);

    senSensorManager.registerListener(this, senSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE),
            SensorManager.SENSOR_DELAY_FASTEST);

    senSensorManager.registerListener(this, senSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
            SensorManager.SENSOR_DELAY_FASTEST);
}

From source file:com.pseudosurface.levels.template.SimulatorActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Create a GLSurfaceView instance and set it
    // as the ContentView for this Activity
    //        mGLView.setPreserveEGLContextOnPause(false);

    this.victoryCallback = new VictoryInterface() {

        @Override//from  w  ww  .  j a  v a  2  s  . co m
        public void victoryCallback(int stars, long time) {
            // TODO Auto-generated method stub
            int level = getIntent().getExtras().getInt("level");
            LevelCompletionFragment.showLevelCompletionFragment(level, stars, time, SimulatorActivity.this);
        }
    };

    /////////////////////

    mGLView = new MyGLSurfaceView(this, this);
    setContentView(mGLView);

    /////////////////////

    //        RelativeLayout rLayout = new RelativeLayout(this);
    //        LayoutParams rlParams = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT); 
    //        rLayout.setLayoutParams(rlParams);
    //
    //        RelativeLayout.LayoutParams tParams = new RelativeLayout.LayoutParams
    //                (LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    //        tParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
    //        tParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
    //        TextView text=new TextView(this); 
    //        text.setText("GOLDEN Gate"); 
    //        text.setTextColor(Color.WHITE);                            
    //        text.setTypeface(Typeface.DEFAULT_BOLD);
    //        text.setLayoutParams(tParams);
    //
    //        mGLView = new MyGLSurfaceView(this, this);
    //        rLayout.addView(mGLView);
    //        rLayout.addView(text);
    //        setContentView(rLayout);

    /////////////////////

    sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    gravitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}

From source file:com.bsafe.sensors.motion.BSMotionSensorsPlugin.java

/**
 * Start listening for acceleration sensor.
 *
 * @return          status of listener//w  w w . j  av  a  2  s  .com
*/
private int start() {
    // If already starting or running, then restart timeout and return
    if ((this.status == BSMotionSensorsPlugin.RUNNING) || (this.status == BSMotionSensorsPlugin.STARTING)) {
        startTimeout();
        return this.status;
    }

    this.setStatus(BSMotionSensorsPlugin.STARTING);

    // Get accelerometer from sensor manager
    List<Sensor> list = this.sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);

    // If found, then register as listener
    if ((list != null) && (list.size() > 0)) {
        this.mSensor = list.get(0);
        if (this.sensorManager.registerListener(this, this.mSensor, SensorManager.SENSOR_DELAY_UI)) {
            this.setStatus(BSMotionSensorsPlugin.STARTING);
        } else {
            this.setStatus(BSMotionSensorsPlugin.ERROR_FAILED_TO_START);
            this.fail(BSMotionSensorsPlugin.ERROR_FAILED_TO_START, "Device sensor returned an error.");
            return this.status;
        }
        ;

    } else {
        this.setStatus(BSMotionSensorsPlugin.ERROR_FAILED_TO_START);
        this.fail(BSMotionSensorsPlugin.ERROR_FAILED_TO_START,
                "No sensors found to register accelerometer listening to.");
        return this.status;
    }

    startTimeout();

    return this.status;
}

From source file:com.microsoft.band.sdksample.SensorsFragment.java

@Override
public void onSensorChanged(SensorEvent sensorEvent) {
    Sensor mySensor = sensorEvent.sensor;

    if (mySensor.getType() == Sensor.TYPE_GYROSCOPE) {
        temp_list[3] = (double) sensorEvent.values[0] * 180 / Math.PI;
        temp_list[4] = (double) sensorEvent.values[1] * 180 / Math.PI;
        temp_list[5] = (double) sensorEvent.values[2] * 180 / Math.PI;
        mTextAngX.setText(String.format("%.2f", temp_list[3]));
        mTextAngY.setText(String.format("%.2f", temp_list[4]));
        mTextAngZ.setText(String.format("%.2f", temp_list[5]));
    } else if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        temp_list[0] = (double) sensorEvent.values[0];
        temp_list[1] = (double) sensorEvent.values[1];
        temp_list[2] = (double) sensorEvent.values[2];
        mTextAccX.setText(String.format("%.3f", temp_list[0]));
        mTextAccY.setText(String.format("%.3f", temp_list[1]));
        mTextAccZ.setText(String.format("%.3f", temp_list[2]));
    }/*from  w  w  w .j  a v a 2s.  c o  m*/
}

From source file:it.cnr.isti.doremi.sleeplogger.SensorSamplingService.java

@Override
public void onCreate() {
    super.onCreate();
    Log.i(TAG, "onCreate()");

    mSensorManager = ((SensorManager) getSystemService(SENSOR_SERVICE));

    /*for (Sensor s : mSensorManager.getSensorList(Sensor.TYPE_ALL)) // print all the available sensors
    {/*  w  w  w  . ja  v  a 2 s .co m*/
       Log.d(TAG, "Sensor: " + s.getName() + ", type = " + s.getType());
    }*/

    // Use SENSOR_TYPE_HEARTRATE_GEAR_LIVE below to have a better accuracy (only for Samsung Gear Live)
    mHeartRateSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE);
    selHB = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent sensorEvent) {
            lastHB = sensorEvent.values[0];
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int i) {
        }
    };
    mSensorManager.registerListener(selHB, this.mHeartRateSensor, 500000); // Forced to 0.5s

    mAccelerationSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    selAcc = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent sensorEvent) {
            lastAcc = sensorEvent.values;
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int i) {
        }
    };
    mSensorManager.registerListener(selAcc, this.mAccelerationSensor, SAMPLING_INTERVAL * 750); // sampling interval * 0.75

    // ENABLE IF REQUIRED
    /*      mGyroscopeSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
          selGyroscope = new SensorEventListener() {
             @Override
             public void onSensorChanged(SensorEvent sensorEvent) { lastGyro = sensorEvent.values; }
            
             @Override
             public void onAccuracyChanged(Sensor sensor, int i) {}
          };
          mSensorManager.registerListener(selGyroscope, this.mGyroscopeSensor, SAMPLING_INTERVAL * 1000);
            
          mOrientationSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
          selOrientation = new SensorEventListener() {
             @Override
             public void onSensorChanged(SensorEvent sensorEvent) { lastOrientation = sensorEvent.values; }
            
             @Override
             public void onAccuracyChanged(Sensor sensor, int i) {}
          };
          mSensorManager.registerListener(selOrientation, this.mOrientationSensor, SAMPLING_INTERVAL * 1000);
            
          mStepSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
          selStep = new SensorEventListener() {
             @Override
             public void onSensorChanged(SensorEvent sensorEvent) { lastStepCount = sensorEvent.values[0]; }
            
             @Override
             public void onAccuracyChanged(Sensor sensor, int i) {}
          };
          mSensorManager.registerListener(selStep, this.mStepSensor, 1000000); // Forced to 1s
    */
    File logFile = new File(Environment.getExternalStorageDirectory() + File.separator + "logs" + File.separator
            + "hb_log-" + sdf.format(new Date()) + ".txt");
    try {
        logFile.getParentFile().mkdirs();
        logFile.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }

    try {
        Log.d(TAG, "Log saved to : " + logFile.getAbsolutePath());
        bw = new BufferedWriter(new FileWriter(logFile));
        bw.write(String.format(Locale.ENGLISH, LOG_HEADER));
    } catch (Exception e) {
        e.printStackTrace();
    }

    powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SensorSamplingServiceWakeLock");
}