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.cubic9.android.droidglove.Main.java

@Override
public void onResume() {
    super.onResume();

    // show IP address
    mTextViewIp.setText("IP: " + getIPAddress());

    // prevent sleep
    mWakeLock.acquire();//from   w w  w  .jav a 2s .c o m

    // get values of sensors
    mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_GAME);
    mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
            SensorManager.SENSOR_DELAY_GAME);

    // get network information
    InetAddress mRemoteIP = null;
    try {
        mRemoteIP = InetAddress.getByName(ip);
    } catch (UnknownHostException e) {
        new AlertDialog.Builder(Main.this).setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle(getString(R.string.dialog_error_title))
                .setMessage(getString(R.string.dialog_error_get_ip))
                .setPositiveButton(R.string.dialog_error_ok, null).create().show();
        e.printStackTrace();
    }

    // Bring the IP Address and port together to form our OSC Sender
    try {
        mSender = new OSCPortOut(mRemoteIP, OSCPort.defaultSCOSCPort());
    } catch (SocketException e) {
        new AlertDialog.Builder(Main.this).setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle(getString(R.string.dialog_error_title))
                .setMessage(getString(R.string.dialog_error_sender))
                .setPositiveButton(R.string.dialog_error_ok, null).create().show();
        e.printStackTrace();
    }

    try {
        mReceiver = new OSCPortIn(OSCPort.defaultSCOSCPort());
    } catch (SocketException e) {
        new AlertDialog.Builder(Main.this).setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle(getString(R.string.dialog_error_title))
                .setMessage(getString(R.string.dialog_error_receiver))
                .setPositiveButton(R.string.dialog_error_ok, null).create().show();
        e.printStackTrace();
    }
    OSCListener listener = new OSCListener() {
        public void acceptMessage(java.util.Date time, OSCMessage message) {
            Object[] args = message.getArguments();
            int vibrationTime = (Integer) args[0];
            if (vibrationTime == 0) {
                mVibrator.vibrate(userVibrationTime);
            } else {
                mVibrator.vibrate(vibrationTime);
            }
        }
    };
    mReceiver.addListener(OSC_ADDRESS_TO_PHONE, listener);
    mReceiver.startListening();
}

From source file:edu.cens.loci.sensors.AccelerometerHandler.java

private synchronized void onSensorChangedLocked(SensorEvent event) {

    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

        mFlag = true;/*from  w w  w . j  a va  2s. c  o m*/

        if (mIsOn)
            mMagArr.addMagnitude(event.values[SensorManager.DATA_X], event.values[SensorManager.DATA_Y],
                    event.values[SensorManager.DATA_Z], event.accuracy);

        //mXArr.add(event.values[SensorManager.DATA_X]);
        //mYArr.add(event.values[SensorManager.DATA_Y]);
        //mZArr.add(event.values[SensorManager.DATA_Z]);
    }
}

From source file:de.tudarmstadt.dvs.myhealthassistant.myhealthhub.fragments.GraphPlotFragment.java

private void updateTitleAndType() {

    ArrayList<String> grpTitle = new ArrayList<String>();
    ArrayList<Integer> grpType = new ArrayList<Integer>();
    ArrayList<Boolean> grpBarType = new ArrayList<Boolean>();

    SharedPreferences pref = PreferenceManager
            .getDefaultSharedPreferences(getActivity().getApplicationContext());

    if (pref.getBoolean(getResources().getString(R.string.in_hum), false)) {
        grpTitle.add("Humidity");
        grpType.add(Sensor.TYPE_RELATIVE_HUMIDITY);
        grpBarType.add(false);/*from   w  ww  . ja v  a  2  s .c o  m*/

    }
    if (pref.getBoolean(getResources().getString(R.string.in_lig), false)) {
        grpTitle.add("Light in lux/min");
        grpType.add(Sensor.TYPE_LIGHT);
        grpBarType.add(false);

    }
    if (pref.getBoolean(getResources().getString(R.string.in_pres), false)) {
        grpTitle.add("Pressure in min");
        grpType.add(Sensor.TYPE_PRESSURE);
        grpBarType.add(false);

    }
    if (pref.getBoolean(getResources().getString(R.string.in_prox), false)) {
        grpTitle.add("Proximity in min");
        grpType.add(Sensor.TYPE_PROXIMITY);
        grpBarType.add(false);

    }
    if (pref.getBoolean(getResources().getString(R.string.in_tem), false)) {
        grpTitle.add("Ambient Temperature in C");
        grpType.add(Sensor.TYPE_AMBIENT_TEMPERATURE);
        grpBarType.add(false);

    }
    if (pref.getBoolean(getResources().getString(R.string.pulse), false)) {
        grpTitle.add("Heart Rate bpm");
        grpType.add(999);
        grpBarType.add(false);

    }
    if (pref.getBoolean(getResources().getString(R.string.in_acc), false)) {
        grpTitle.add("Motion Strength/min");
        grpType.add(Sensor.TYPE_ACCELEROMETER);
        grpBarType.add(true);

    }
    if (pref.getBoolean(getResources().getString(R.string.in_mag), false)) {
        grpTitle.add("Magnetic Field Strength/min");
        grpType.add(Sensor.TYPE_MAGNETIC_FIELD);
        grpBarType.add(true);

    }
    if (pref.getBoolean(getResources().getString(R.string.in_grav), false)) {
        grpTitle.add("Gravity Strength/min");
        grpType.add(Sensor.TYPE_GRAVITY);
        grpBarType.add(true);

    }

    if (pref.getBoolean(getResources().getString(R.string.in_gyrs), false)) {
        grpTitle.add("Gyroscope Strength/min");
        grpType.add(Sensor.TYPE_GYROSCOPE);
        grpBarType.add(true);

    }
    if (pref.getBoolean(getResources().getString(R.string.in_lin_acc), false)) {
        grpTitle.add("Linear Accelerometer Strength/min");
        grpType.add(Sensor.TYPE_LINEAR_ACCELERATION);
        grpBarType.add(true);

    }
    if (grpTitle.size() == 2 && grpType.size() == 2) {
        firstGrpTitle = grpTitle.get(0);
        firstGrpType = grpType.get(0);
        isFirstBar = grpBarType.get(0);

        secondGrpTitle = grpTitle.get(1);
        secondGrpType = grpType.get(1);
        isSecondBar = grpBarType.get(1);
    } else if (grpTitle.size() == 1 && grpType.size() == 1) {
        firstGrpTitle = grpTitle.get(0);
        firstGrpType = grpType.get(0);
        isFirstBar = grpBarType.get(0);

        secondGrpTitle = "";
        secondGrpType = -1;
    }
}

From source file:name.setup.dance.StepService.java

private void registerDetector() {
    mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER /*| 
                                                                        Sensor.TYPE_MAGNETIC_FIELD | 
                                                                        Sensor.TYPE_ORIENTATION*/);
    mSensorManager.registerListener(mStepDetector, mSensor, SensorManager.SENSOR_DELAY_FASTEST);
}

From source file:com.phonegap.AccelListener.java

/**
 * Sensor listener event./*ww  w  . ja  va2  s  .c  om*/
 * 
 * @param SensorEvent event
 */
public void onSensorChanged(SensorEvent event) {

    // Only look at accelerometer events
    if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) {
        return;
    }

    // If not running, then just return
    if (this.status == AccelListener.STOPPED) {
        return;
    }

    // Save time that event was received
    this.timestamp = System.currentTimeMillis();
    this.x = event.values[0];
    this.y = event.values[1];
    this.z = event.values[2];

    this.setStatus(AccelListener.RUNNING);

    // If values haven't been read for TIMEOUT time, then turn off accelerometer sensor to save power
    if ((this.timestamp - this.lastAccessTime) > this.TIMEOUT) {
        this.stop();
    }
}

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

/**
 * Sensor listener event./*  ww w .j  ava2 s  .com*/
 *
 * @param SensorEvent event
 */
public void onSensorChanged(SensorEvent event) {
    // Only look at accelerometer events
    if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) {
        return;
    }

    // If not running, then just return
    if (this.status == BSMotionSensorsPlugin.STOPPED) {
        return;
    }
    this.setStatus(BSMotionSensorsPlugin.RUNNING);

    if (this.accuracy >= SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM) {

        // Save time that event was received
        this.timestamp = System.currentTimeMillis();
        this.x = event.values[0];
        this.y = event.values[1];
        this.z = event.values[2];

        this.win();
    }
}

From source file:com.example.jony.myapp.reader_APP.ui.DailyDetailsActivity.java

@Override
public void onSensorChanged(SensorEvent event) {

    if (isShakeMode == false) {
        return;/* w w w  . j a  v a2s.co m*/
    }

    float value[] = event.values;
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        if ((Math.abs(value[0]) + Math.abs(value[1]) + Math.abs(value[2])) > CONSTANT.shakeValue) {
            onBackPressed();
        }
    }
}

From source file:de.tudarmstadt.dvs.myhealthassistant.myhealthhub.services.InternalSensorService.java

@Override
public void onSensorChanged(SensorEvent event) {
    if (pref == null)
        pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

    int mSensorType = event.sensor.getType();
    if (mSensorType == Sensor.TYPE_ACCELEROMETER) {

        int x = Math.round(event.values[0]);
        int y = Math.round(event.values[1]);
        int z = Math.round(event.values[2]);

        // using ACCELEROMETER_KNEE only as a demonstration,
        // in practice new event can be created
        AccDeviceSensorEvent accEvt = new AccDeviceSensorEvent("eventID", getTimestamp(), "producerID",
                SensorReadingEvent.ACCELEROMETER_ON_DEVICE, getTimestamp(), x, y, z, x, y, z);

        sendToChannel(accEvt, AbstractChannel.RECEIVER); // for external
        // apps/* w w w.  j a va 2 s. c o  m*/
        // subscription
        gotAccEvent(getTimestamp(), Sensor.TYPE_ACCELEROMETER, x, y, z); // for
        // on device
        // graphViews
        // using
    }

    else if (mSensorType == Sensor.TYPE_LIGHT) {

        float x = event.values[0];

        // using ambientLightEvent only as a demonstration,
        // in practice new event can be created
        AmbientLightEvent lightEvnt = new AmbientLightEvent("eventID", getTimestamp(), "producerID",
                SensorReadingEvent.AMBIENT_LIGHT, getTimestamp(), "location", "object", x,
                AmbientLightEvent.UNIT_LUX);
        sendToChannel(lightEvnt, AbstractChannel.RECEIVER);
        gotLightEvent(getTimestamp(), Sensor.TYPE_LIGHT, x);

    }

    else if (mSensorType == Sensor.TYPE_PRESSURE) {
        float x = event.values[0];
        AmbientPressureEvent temEvnt = new AmbientPressureEvent("eventID", getTimestamp(), "producerID",
                SensorReadingEvent.AMBIENT_PRESSURE, getTimestamp(), "location", "object", x,
                AmbientPressureEvent.UNIT_HPA);
        sendToChannel(temEvnt, AbstractChannel.RECEIVER);
        gotLightEvent(getTimestamp(), Sensor.TYPE_PRESSURE, x);
    }

    else if (mSensorType == Sensor.TYPE_PROXIMITY) {
        float x = event.values[0];
        ProximityEvent evnt = new ProximityEvent("eventID", getTimestamp(), "producerID",
                SensorReadingEvent.PROXIMITY, getTimestamp(), "location", "object", x, "");
        sendToChannel(evnt, AbstractChannel.RECEIVER);
        gotLightEvent(getTimestamp(), Sensor.TYPE_PROXIMITY, x);
    }

    else if (mSensorType == Sensor.TYPE_AMBIENT_TEMPERATURE) {
        float x = event.values[0];
        TemperatureEvent evnt = new TemperatureEvent("eventID", getTimestamp(), "producerID",
                SensorReadingEvent.ROOM_TEMPERATURE, getTimestamp(), "location", "object", x,
                TemperatureEvent.UNIT_CELSIUS);
        sendToChannel(evnt, AbstractChannel.RECEIVER);
        gotLightEvent(getTimestamp(), Sensor.TYPE_AMBIENT_TEMPERATURE, x);
    }

    else if (mSensorType == Sensor.TYPE_RELATIVE_HUMIDITY) {
        float x = event.values[0];
        HumidityEvent evnt = new HumidityEvent("eventID", getTimestamp(), "producerID",
                SensorReadingEvent.HUMIDITY, getTimestamp(), "location", "object", x);
        sendToChannel(evnt, AbstractChannel.RECEIVER);
        gotLightEvent(getTimestamp(), Sensor.TYPE_RELATIVE_HUMIDITY, x);
    }

    else if (mSensorType == Sensor.TYPE_GYROSCOPE) {

        int x = Math.round(event.values[0]);
        int y = Math.round(event.values[1]);
        int z = Math.round(event.values[2]);

        GyroscopeSensorEvent gSEvnt = new GyroscopeSensorEvent("eventID", getTimestamp(), "producerID",
                SensorReadingEvent.GYROSCOPE, getTimestamp(), x, y, z, x, y, z);
        sendToChannel(gSEvnt, AbstractChannel.RECEIVER);
        gotAccEvent(getTimestamp(), Sensor.TYPE_GYROSCOPE, x, y, z);

    }

    else if (mSensorType == Sensor.TYPE_MAGNETIC_FIELD) {
        int x = Math.round(event.values[0]);
        int y = Math.round(event.values[1]);
        int z = Math.round(event.values[2]);

        MagneticSensorEvent gSEvnt = new MagneticSensorEvent("eventID", getTimestamp(), "producerID",
                SensorReadingEvent.MAGNETIC_FIELD, getTimestamp(), x, y, z, x, y, z);
        sendToChannel(gSEvnt, AbstractChannel.RECEIVER);
        gotAccEvent(getTimestamp(), Sensor.TYPE_MAGNETIC_FIELD, x, y, z);

    }

    else if (mSensorType == Sensor.TYPE_LINEAR_ACCELERATION) {
        int x = Math.round(event.values[0]);
        int y = Math.round(event.values[1]);
        int z = Math.round(event.values[2]);

        // using ACCELEROMETER_KNEE only as a demonstration,
        // in practice new event can be created
        AccDeviceSensorEvent accEvt = new AccDeviceSensorEvent("eventID", getTimestamp(), "producerID",
                SensorReadingEvent.ACCELEROMETER_ON_DEVICE, getTimestamp(), x, y, z, x, y, z);

        sendToChannel(accEvt, AbstractChannel.RECEIVER);
        gotAccEvent(getTimestamp(), Sensor.TYPE_LINEAR_ACCELERATION, x, y, z);
    }

}

From source file:com.inloc.dr.StepService.java

private void registerDetector() {
    mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER /*| 
                                                                        Sensor.TYPE_MAGNETIC_FIELD | 
                                                                        Sensor.TYPE_ORIENTATION*/);
    mSensorManager.registerListener(mStepDetector, mSensor, SensorManager.SENSOR_DELAY_FASTEST);
    mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
    mSensorManager.registerListener(mTurnDetector, mSensor, SensorManager.SENSOR_DELAY_GAME);
}

From source file:org.chromium.latency.walt.AccelerometerFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    logger = SimpleLogger.getInstance(getContext());
    waltDevice = WaltDevice.getInstance(getContext());

    // Inflate the layout for this fragment
    final View view = inflater.inflate(R.layout.fragment_accelerometer, container, false);
    logTextView = (TextView) view.findViewById(R.id.txt_log);
    startButton = view.findViewById(R.id.button_start);
    latencyChart = (ScatterChart) view.findViewById(R.id.latency_chart);
    latencyChartLayout = view.findViewById(R.id.latency_chart_layout);
    logTextView.setMovementMethod(new ScrollingMovementMethod());
    view.findViewById(R.id.button_close_chart).setOnClickListener(this);
    sensorManager = (SensorManager) getContext().getSystemService(Context.SENSOR_SERVICE);
    accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    if (accelerometer == null) {
        logger.log("ERROR! Accelerometer sensor not found");
    }//w w  w .j  av  a 2  s.c  o m
    return view;
}