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.wordpress.tonytam.avatar.AvatarWearActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.jj_layout);//from w  w  w  . ja  v  a2s.  co  m
    setupViews();
    mHandler = new Handler();
    mJumpCounter = Utils.getCounterFromPreference(this);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    renewTimer();
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
    mSensorAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

    mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).addApi(Wearable.API).build();
}

From source file:uk.ac.horizon.ubihelper.service.Service.java

@Override
public void onCreate() {
    // One-time set-up...
    Log.d(TAG, "onCreate()");
    // TODO/*from  w ww . j a va2s . c o m*/
    super.onCreate();
    // handler for requests
    mHandler = new Handler();
    // create taskbar notification
    int icon = R.drawable.service_notification_icon;
    CharSequence tickerText = getText(R.string.notification_start_message);
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);

    Context context = this;
    CharSequence contentTitle = getText(R.string.notification_title);
    CharSequence contentText = getText(R.string.notification_description);
    Intent notificationIntent = new Intent(this, MainPreferences.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

    startForeground(RUNNING_ID, notification);

    channelManager = new ChannelManager(peerChannelFactory);
    // sensors
    if (!isEmulator()) {
        Log.d(TAG, "Create sensor channels...");

        SensorChannel magnetic = new SensorChannel("magnetic", this, Sensor.TYPE_MAGNETIC_FIELD);
        channelManager.addChannel(magnetic);
        SensorChannel accelerometer = new SensorChannel("accelerometer", this, Sensor.TYPE_ACCELEROMETER);
        channelManager.addChannel(accelerometer);
        SensorChannel gyro = new SensorChannel("gyro", this, Sensor.TYPE_GYROSCOPE);
        channelManager.addChannel(gyro);
        SensorChannel light = new SensorChannel("light", this, Sensor.TYPE_LIGHT);
        channelManager.addChannel(light);
        SensorChannel pressure = new SensorChannel("pressure", this, Sensor.TYPE_PRESSURE);
        channelManager.addChannel(pressure);
        SensorChannel proximity = new SensorChannel("proximity", this, Sensor.TYPE_PROXIMITY);
        channelManager.addChannel(proximity);
        SensorChannel temperature = new SensorChannel("temperature", this, Sensor.TYPE_TEMPERATURE);
        channelManager.addChannel(temperature);

        try {
            Field f = Sensor.class.getField("TYPE_AMBIENT_TEMPERATURE");
            SensorChannel sc = new SensorChannel("ambientTemperature", this, f.getInt(null));
            channelManager.addChannel(sc);
        } catch (Exception e) {
            Log.d(TAG, "Could not get field Sensor.TYPE_AMBIENT_TEMPERATURE");
        }
        try {
            Field f = Sensor.class.getField("TYPE_RELATIVE_HUMIDITY");
            SensorChannel sc = new SensorChannel("relativeHumidity", this, f.getInt(null));
            channelManager.addChannel(sc);
        } catch (Exception e) {
            Log.d(TAG, "Could not get field Sensor.TYPE_AMBIENT_TEMPERATURE");
        }

        // all sensors by full name
        SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        if (sensorManager != null) {
            List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
            for (Sensor sensor : sensors) {
                SensorChannel sc = new SensorChannel("sensor." + sensor.getName(), this, sensor);
                channelManager.addChannel(sc);
            }
        }
        BluetoothDiscoveryChannel btchannel = new BluetoothDiscoveryChannel(this, mHandler, "bluetooth");
        channelManager.addChannel(btchannel);
        WifiScannerChannel wifichannel = new WifiScannerChannel(this, mHandler, "wifi");
        channelManager.addChannel(wifichannel);
        GpsStatusChannel gpsstatus = new GpsStatusChannel("gpsstatus", this);
        channelManager.addChannel(gpsstatus);
        CellLocationChannel cellchannel = new CellLocationChannel(mHandler, this, "cell.location", false);
        channelManager.addChannel(cellchannel);
        CellLocationChannel cellchannel2 = new CellLocationChannel(mHandler, this, "cell.neighbors", true);
        channelManager.addChannel(cellchannel2);
        CellStrengthChannel cellchannel3 = new CellStrengthChannel(this, "cell.strength");
        channelManager.addChannel(cellchannel3);
    }
    channelManager.addChannel(new TimeChannel(mHandler, "time"));
    List<String> locationProviders = LocationChannel.getAllProviders(this);
    for (String provider : locationProviders) {
        LocationChannel locchannel = new LocationChannel("location." + provider, this, provider);
        channelManager.addChannel(locchannel);
    }

    channelManager.addChannel(new MicChannel(mHandler, "mic"));

    Log.d(TAG, "Create http server...");

    // http server
    httpPort = getPort();

    httpListener = new HttpListener(this, httpPort);
    httpListener.start();

    // peer communication
    peerManager = new PeerManager(this);
    int serverPort = peerManager.getServerPort();

    channelManager.addChannel(new EnabledPeersChannel(this, peerManager, "peers"));

    // wifi discovery
    wifiDiscoveryManager = new WifiDiscoveryManager(this);
    wifiDiscoverable = getWifiDiscoverable();
    wifiDiscoveryManager.setServerPort(serverPort);
    wifiDiscoveryManager.setEnabled(wifiDiscoverable);

    bluetooth = BluetoothAdapter.getDefaultAdapter();
    if (bluetooth != null)
        btmac = bluetooth.getAddress();

    telephony = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    if (telephony != null)
        imei = telephony.getDeviceId();

    logManager = new LogManager(this, channelManager);
    logManager.checkPreferences();

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    prefs.registerOnSharedPreferenceChangeListener(onRunChangeListener);

    Log.d(TAG, "onCreate() finished");
}

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

@Override
public void onSensorChanged(SensorEvent event) {
    // Only look at accelerometer events
    if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) {
        return;/*  w  ww  .  j a va  2  s  . c o  m*/
    }

    // If not running, then just return
    if (mStatus == STOPPED) {
        return;
    }
    mStatus = RUNNING;
    if (mAccuracy >= SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM) {
        mTimestamp = System.currentTimeMillis();
        mX = event.values[0];
        mY = event.values[1];
        mZ = event.values[2];

        win();
    }
}

From source file:com.kircherelectronics.gyroscopeexplorer.activity.filter.Orientation.java

public void onResume() {
    calibratedGyroscopeEnabled = getPrefCalibratedGyroscopeEnabled();
    meanFilterSmoothingEnabled = getPrefMeanFilterSmoothingEnabled();
    meanFilterTimeConstant = getPrefMeanFilterSmoothingTimeConstant();

    sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_FASTEST);

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

    if (calibratedGyroscopeEnabled) {
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE),
                SensorManager.SENSOR_DELAY_FASTEST);
    } else {/* w  w w.  java2 s  .  c o m*/
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
            sensorManager.registerListener(this,
                    sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE_UNCALIBRATED),
                    SensorManager.SENSOR_DELAY_FASTEST);
        }
    }
}

From source file:com.sorin.cloudcog.car.obd2.RubyBluetoothChatActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (D)// w w w  .  j  a  v a  2  s  .  c om
        Log.e(TAG, "+++ ON CREATE +++");

    // Set up the window layout
    setContentView(R.layout.car_pager_main_ruby);
    // toast message at begining of activity start
    Toast.makeText(this, "Ruby Red Style OBD2 data console", Toast.LENGTH_SHORT).show();
    mRubyAdapter = new CarTabFragmentHandlerRuby(getSupportFragmentManager());
    mRubyPager = (ViewPager) findViewById(R.id.pager_ruby);
    mRubyPager.setAdapter(mRubyAdapter);

    mRubyIndicator = (TitlePageIndicator) findViewById(R.id.indicator_ruby);
    mRubyIndicator.setViewPager(mRubyPager);

    // When shacked it will return to main activity initialization
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mShakeDetector = new ShakeDetectorActivity();
    mShakeDetector.setOnShakeListener(new OnShakeListener() {
        // shake handler
        @Override
        public void onShake(int count) {

            RubyBluetoothChatActivity.this.finish();

        }
    });
    // Get local Bluetooth adapter
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    // If the adapter is null, then Bluetooth is not supported
    if (mBluetoothAdapter == null) {
        Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
        finish();
        return;
    }
}

From source file:com.javadog.cgeowear.cgeoWear.java

/**
 * Initializes the screen, whether the Activity was freshly-launched or onNewIntent was run.
 *
 * @param i The launch intent./*from   w  w w .ja  va 2  s . co m*/
 */
private void initScreen(Intent i) {
    String cacheName = i.getStringExtra(MessageDataSet.KEY_CACHE_NAME);
    String geocode = i.getStringExtra(MessageDataSet.KEY_GEOCODE);
    distance = i.getFloatExtra(MessageDataSet.KEY_DISTANCE, 0f);
    direction = i.getFloatExtra(MessageDataSet.KEY_DIRECTION, 0f);
    geocacheLocation = i.getParcelableExtra(MessageDataSet.KEY_CACHE_LOCATION);

    //Start listening for compass updates, if the user wants
    useWatchCompass = i.getBooleanExtra(MessageDataSet.KEY_WATCH_COMPASS, false);
    if (accelerometer != null && magnetometer != null) {
        sensorManager.unregisterListener(this, accelerometer);
        sensorManager.unregisterListener(this, magnetometer);
    }
    Log.d(DEBUG_TAG, useWatchCompass ? "Using watch compass." : "Using phone compass.");
    if (useWatchCompass) {
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        magnetometer = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
        sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
        sensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_NORMAL);
    }

    tv_cacheName.setText(cacheName);
    tv_geocode.setText(geocode);
    setDistanceFormatted(distance);
    rotateCompass(direction);
}

From source file:com.example.basensortracker.SensorFusionActivity.java

@Override
public void onSensorChanged(SensorEvent event) {
    switch (event.sensor.getType()) {
    case Sensor.TYPE_ACCELEROMETER:
        // copy new accelerometer data into accel array and calculate
        // orientation
        System.arraycopy(event.values, 0, accel, 0, 3);
        calculateAccMagOrientation();/*w ww . j  a  v  a2 s.c  o  m*/
        break;

    case Sensor.TYPE_GYROSCOPE:
        // process gyro data
        gyroFunction(event);
        break;

    case Sensor.TYPE_MAGNETIC_FIELD:
        // copy new magnetometer data into magnet array
        System.arraycopy(event.values, 0, magnet, 0, 3);
        break;
    }
}

From source file:com.kircherelectronics.gyroscopeexplorer.activity.GyroscopeActivity.java

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        // Android reuses events, so you probably want a copy
        System.arraycopy(event.values, 0, acceleration, 0, event.values.length);
        orientationFusion.setAcceleration(acceleration);
    } else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
        // Android reuses events, so you probably want a copy
        System.arraycopy(event.values, 0, magnetic, 0, event.values.length);
        orientationFusion.setMagneticField(this.magnetic);
    } else if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
        // Android reuses events, so you probably want a copy
        System.arraycopy(event.values, 0, rotation, 0, event.values.length);
        // Filter the rotation
        fusedOrientation = orientationFusion.filter(this.rotation);

        if (meanFilterEnabled) {
            fusedOrientation = meanFilter.filter(fusedOrientation);
        }/* w  w  w .ja v a 2 s . c  o  m*/

        dataLogger.setRotation(fusedOrientation);
    }
}

From source file:me.ziccard.secureit.fragment.AccelerometerFragment.java

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

    view = new AccelerometerGLSurfaceView(this.getActivity());
    ((FrameLayout) getActivity().findViewById(R.id.opengl)).addView(view);

    accelerometerText = (TextView) getActivity().findViewById(R.id.accelerometer_text);

    sensorMgr = (SensorManager) getActivity().getSystemService(Activity.SENSOR_SERVICE);
    accelerometer = (Sensor) sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

    if (accelerometer == null) {
        Log.i("AccelerometerFrament", "Warning: no accelerometer");
    } else {//from w w  w  . jav a  2s  .c o  m
        sensorMgr.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    }
}

From source file:com.example.appf.CS3570.java

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
        mGyroscopeEvent = event.values;//from   ww  w  . java2  s .  c  o m
    }
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
        mGravity = event.values;

    if (mGravity != null && mGyroscopeEvent != null && mGLView != null && mGLView.mRenderer.mTetra != null) {
        filter.updateFilter(mGyroscopeEvent[0], mGyroscopeEvent[1], mGyroscopeEvent[2], mGravity[0],
                mGravity[1], mGravity[2]);
        filter.computerEuler();
        azimut = (float) filter.getYaw(); // orientation contains: azimut, pitch and roll
        pitch = (float) filter.getPitch();
        roll = (float) filter.getRoll();

        if (previousAzimuth == Float.MAX_VALUE) {
            previousAzimuth = azimut;
        }
        if (previousPitch == Float.MAX_VALUE) {
            previousPitch = pitch;
        }
        if (previousRoll == Float.MAX_VALUE) {
            previousRoll = roll;
        }

        float delta_azimut = azimut - previousAzimuth;
        float delta_pitch = pitch - previousPitch;
        float delta_roll = roll - previousRoll;
        //Log.e("eee", "Azimuth: "+ delta_azimut + " pitch: " + delta_pitch + " roll: " + delta_roll);
        //Log.e("eee", " roll: " + roll);
        //Log.e("eee", "Azimuth: " + azimut + " pitch: " + pitch + " roll: " + roll);

        if (Math.abs(delta_roll) > THRESHOLD && roll != Float.NaN) {
            previousRoll = roll;
            if (cam)
                mGLView.mRenderer.mCamera.rotateX(delta_roll * 180.0 / Math.PI * ROTATE_AMPLIFY);
            //mGLView.mRenderer.mTetra.rotate((float) (delta_roll * 180.0 / Math.PI) * ROTATE_AMPLIFY, 1, 0, 0);
            else
                mGLView.mRenderer.mTetra.pure_rotate((float) ((roll + Math.PI) * 180.0 / Math.PI), 1, 0, 0);
        }

        if (Math.abs(delta_pitch) > THRESHOLD && pitch != Float.NaN) {
            previousPitch = pitch;
            if (cam)
                mGLView.mRenderer.mCamera.rotateX(delta_pitch * 180.0 / Math.PI * ROTATE_AMPLIFY);
            //Log.e("eee", "" + ((delta_pitch)* 180.0 / Math.PI) );
            else
                mGLView.mRenderer.mTetra.pure_rotate((float) ((pitch + Math.PI) * 180.0 / Math.PI), 0, 1, 0);

        }

        if (Math.abs(delta_azimut) > THRESHOLD && azimut != Float.NaN) {
            previousAzimuth = azimut;
            if (cam)
                mGLView.mRenderer.mCamera.rotateY(delta_azimut * 180.0 / Math.PI * ROTATE_AMPLIFY);
            else
                //mGLView.mRenderer.mTetra.rotate((float)(delta_azimut * 180.0/Math.PI) * ROTATE_AMPLIFY, 0, 0, 1 );
                mGLView.mRenderer.mTetra.pure_rotate((float) (azimut * 180.0 / Math.PI), 0, 0, 1);
        }

        if (out != null) {
            //Log.e("eee", "{pitch:" + pitch + "}");
            Vector3 cam_pos = mGLView.mRenderer.mCamera.get_position();
            out.println("{\"pitch\":" + pitch + ", \"roll\": " + roll + ", \"yaw\": " + azimut
                    + ", \"translation\": [" + cam_pos.getX() + ", " + cam_pos.getY() + "," + cam_pos.getZ()
                    + "]}\0");
        }
        mGLView.requestRender();
    }

}