示例#1
1
  void registerSensorListeners() {
    if (config.useAccelerometer) {
      manager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
      if (manager.getSensorList(Sensor.TYPE_ACCELEROMETER).size() == 0) {
        accelerometerAvailable = false;
      } else {
        Sensor accelerometer = manager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
        accelerometerListener =
            new SensorListener(
                this.nativeOrientation, this.accelerometerValues, this.magneticFieldValues);
        accelerometerAvailable =
            manager.registerListener(
                accelerometerListener, accelerometer, SensorManager.SENSOR_DELAY_GAME);
      }
    } else accelerometerAvailable = false;

    if (config.useCompass) {
      if (manager == null)
        manager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
      Sensor sensor = manager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
      if (sensor != null) {
        compassAvailable = accelerometerAvailable;
        if (compassAvailable) {
          compassListener =
              new SensorListener(
                  this.nativeOrientation, this.accelerometerValues, this.magneticFieldValues);
          compassAvailable =
              manager.registerListener(compassListener, sensor, SensorManager.SENSOR_DELAY_GAME);
        }
      } else {
        compassAvailable = false;
      }
    } else compassAvailable = false;
    Gdx.app.log("AndroidInput", "sensor listener setup");
  }
示例#2
0
 public OrientationUtil(Context context) {
   mSensorManager = (SensorManager) context.getSystemService(Activity.SENSOR_SERVICE);
   mSensorManager.registerListener(
       this,
       mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
       SensorManager.SENSOR_DELAY_NORMAL);
 }
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
   switch (item.getItemId()) {
     case R.id.menu_settings:
       getFragmentManager()
           .beginTransaction()
           .addToBackStack(null)
           .replace(android.R.id.content, settingsFragment)
           .commit();
       break;
     case R.id.menu_auto_sensor_mode:
       mUseAutoSensorMode = !mUseAutoSensorMode;
       if (mUseAutoSensorMode) {
         mSensorManager.registerListener(
             mAccelerometerModel, mAccelerometerSensor, SensorManager.SENSOR_DELAY_FASTEST);
         mSensorManager.registerListener(
             mMagneticFieldModel, mMagneticFieldSensor, SensorManager.SENSOR_DELAY_FASTEST);
         // Reset perspective matrix to AutoSensor settings in case zoom changed during manual mode
         mStarMapperRenderer.resetPerspective();
       } else {
         mSensorManager.unregisterListener(mAccelerometerModel);
         mSensorManager.unregisterListener(mMagneticFieldModel);
       }
       break;
     case R.id.menu_test:
       // this won't do anything
       break;
     default:
       return false;
   }
   return true;
 }
 @Override
 protected void onStop() {
   /** La actividad ya no va a ser visible para el usuario. */
   super.onStop();
   sensorManager.unregisterListener(this, mStepCounterSensor);
   sensorManager.unregisterListener(this, mStepDetectorSensor);
 }
 @Override
 protected void registerSensors(SensorEventListener mSensorCallback, SensorManager sm) {
   sm.registerListener(
       mSensorCallback,
       sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
       SensorManager.SENSOR_DELAY_GAME);
 }
示例#6
0
 @Override
 protected void onResume() {
   super.onResume();
   mSensorManager.registerListener(this, mMagneticFieldSensor, SensorManager.SENSOR_DELAY_FASTEST);
   mSensorManager.registerListener(this, mGyroscopeSensor, SensorManager.SENSOR_DELAY_FASTEST);
   mSensorManager.registerListener(this, mAccelerometerSensor, SensorManager.SENSOR_DELAY_FASTEST);
 }
  /** Calculate the fused orientation. */
  private void calculateFusedOrientation() {
    float oneMinusCoeff = (1.0f - filterCoefficient);

    // Apply the complementary filter. // We multiply each rotation by their
    // coefficients (scalar matrices)...

    // Scale our quaternion for the gyroscope
    quatGyro = quatGyro.multiply(filterCoefficient);

    // Scale our quaternion for the accel/mag
    quatAccelMag = quatAccelMag.multiply(oneMinusCoeff);

    // ...and then add the two quaternions together.
    // output[0] = alpha * output[0] + (1 - alpha) * input[0];
    quatGyro = quatGyro.add(quatAccelMag);

    // Now we get a structure we can pass to get a rotation matrix, and then
    // an orientation vector from Android.
    fusedVector[0] = (float) quatGyro.getVectorPart()[0];
    fusedVector[1] = (float) quatGyro.getVectorPart()[1];
    fusedVector[2] = (float) quatGyro.getVectorPart()[2];
    fusedVector[3] = (float) quatGyro.getScalarPart();

    // We need a rotation matrix so we can get the orientation vector...
    // Getting Euler
    // angles from a quaternion is not trivial, so this is the easiest way,
    // but perhaps
    // not the fastest way of doing this.
    SensorManager.getRotationMatrixFromVector(fusedMatrix, fusedVector);

    // Get the fused orienatation
    SensorManager.getOrientation(fusedMatrix, fusedOrientation);
  }
示例#8
0
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sorry);
    // マナーモードを解除する
    SoundControl soundCon = new SoundControl(this);
    soundCon.setNormalRinger();
    // センサーオブジェクト作成
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

    // 画面のロックを防ぐ
    this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    tv = (TextView) findViewById(R.id.count);
    //	}
    //
    //	//アクティビティが動き始めたらリスナーを登録する
    //    public void onResume() {
    //    	super.onResume();
    // カウントアップスタート
    startTimer();
    // 加速度センサーリストを取得
    List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
    if (sensorList != null && !sensorList.isEmpty()) {
      Sensor sensor = sensorList.get(0);
      // リスナー登録(p163)
      sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_UI);
    }
  }
  @Override
  public void onSensorChanged(SensorEvent event) {

    DecimalFormat oneDigit = new DecimalFormat("#,##0.0");

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

      rawAccel = event.values.clone();
      accel[0] = event.values[0];
      accel[1] = event.values[1];
      accel[2] = event.values[2];

      String xPrepend, yPrepend, zPrepend, data = "";

      xPrepend = accel[0] > 0 ? "+" : "";
      yPrepend = accel[1] > 0 ? "+" : "";
      zPrepend = accel[2] > 0 ? "+" : "";

      if (x) {
        data = "X: " + xPrepend + oneDigit.format(accel[0]);
      }
      if (y) {
        if (!data.equals("")) {
          data += " , Y: " + yPrepend + oneDigit.format(accel[1]);
        } else {
          data += "Y: " + yPrepend + oneDigit.format(accel[1]);
        }
      }
      if (z) {
        if (!data.equals("")) {
          data += " , Z: " + zPrepend + oneDigit.format(accel[2]);
        } else {
          data += "Z: " + zPrepend + oneDigit.format(accel[2]);
        }
      }

      if (mag) {
        accel[3] =
            (float)
                Math.sqrt(Math.pow(accel[0], 2) + Math.pow(accel[1], 2) + Math.pow(accel[2], 2));

        if (!data.equals("")) {
          data += " , Magnitude: " + oneDigit.format(accel[3]);
        } else {
          data += "Magnitude: " + oneDigit.format(accel[3]);
        }
      }
      values.setText(data);

    } else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
      rawMag = event.values.clone();

      float rotation[] = new float[9];

      if (SensorManager.getRotationMatrix(rotation, null, rawAccel, rawMag)) {
        orientation = new float[3];
        SensorManager.getOrientation(rotation, orientation);
      }
    }
  }
 public void onPause() {
   if (sensorMgr != null) {
     sensorMgr.unregisterListener(
         this, sensorMgr.getDefaultSensor(SensorManager.SENSOR_ACCELEROMETER));
     sensorMgr = null;
   }
 }
示例#11
0
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {

    TAG =
        Aware.getSetting(getApplicationContext(), Aware_Preferences.DEBUG_TAG).length() > 0
            ? Aware.getSetting(getApplicationContext(), Aware_Preferences.DEBUG_TAG)
            : TAG;

    if (Aware.getSetting(this, Aware_Preferences.FREQUENCY_ACCELEROMETER).length() == 0) {
      Aware.setSetting(this, Aware_Preferences.FREQUENCY_ACCELEROMETER, SAMPLING_RATE);
    }

    if (SAMPLING_RATE
        != Integer.parseInt(
            Aware.getSetting(
                getApplicationContext(),
                Aware_Preferences.FREQUENCY_ACCELEROMETER))) { // changed parameters
      SAMPLING_RATE =
          Integer.parseInt(
              Aware.getSetting(getApplicationContext(), Aware_Preferences.FREQUENCY_ACCELEROMETER));
      sensorHandler.removeCallbacksAndMessages(null);
      mSensorManager.unregisterListener(this, mAccelerometer);
      mSensorManager.registerListener(this, mAccelerometer, SAMPLING_RATE, sensorHandler);
    }

    if (Aware.DEBUG)
      Log.d(TAG, "Accelerometer service active at " + SAMPLING_RATE + " microseconds...");

    return START_STICKY;
  }
示例#12
0
  protected void ativaListenersSensores(ArrayList<Sensor> sensor) {

    for (Sensor aux : sensor) {
      aux = sm.getDefaultSensor(aux.getType());
      sm.registerListener(this, aux, SensorManager.SENSOR_DELAY_NORMAL);
    }
  }
示例#13
0
  public void onCreate() {
    super.onCreate();
    intent = new Intent(BROADCAST_ACTION);

    // startup code
    // Barometer Setup
    try {
      sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
      if (sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null)
        accelSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
      else throw new Exception("Sensor not available");

      sensorManager.registerListener(this, accelSensor, SensorManager.SENSOR_DELAY_NORMAL);

      x = y = z = 0;

      /** Broadcast Stuff * */
      Toast.makeText(this, "Accel Service Started", Toast.LENGTH_SHORT).show();

      handler.removeCallbacks(sendUpdatesToUI);
      handler.postDelayed(sendUpdatesToUI, 1000); // 1 second
    } catch (Exception e) {
      Log.e(TAG, "Unable to start barometer", e);
      Toast.makeText(this, "Unable to start barometer: " + e.toString(), Toast.LENGTH_LONG).show();
    }
  }
  public void onDestroy() {
    running = false;
    if (scanThread != null) scanThread.interrupt();

    if (myWLocate != null) myWLocate.doPause();

    sensorManager.unregisterListener(
        this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER));

    if (scanData.getmView() != null) {
      ((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(scanData.getmView());
      scanData.setmView(null);
    }
    try {
      if (wl != null) wl.release();
    } catch (RuntimeException re) {
    }
    wl = null;
    try {
      scanThread.join(1000);
    } catch (InterruptedException ie) {

    }
    System.exit(0);
  }
示例#15
0
  @Override
  protected void onResume() {
    super.onResume();
    final TextView sensorValues = (TextView) findViewById(R.id.sensor_values);
    sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
    gravityListener =
        new SensorEventListener() {
          @Override
          public void onSensorChanged(SensorEvent event) {
            double length =
                Math.sqrt(
                    event.values[0] * event.values[0]
                        + event.values[1] * event.values[1]
                        + event.values[2] * event.values[2]);
            double alfa = Math.asin(event.values[0] / length) * 180 / Math.PI;
            double beta = Math.asin(event.values[1] / length) * 180 / Math.PI;
            sensorValues.setText(alfa + "\n" + beta);
          }

          @Override
          public void onAccuracyChanged(Sensor sensor, int accuracy) {}
        };
    sensorManager.registerListener(gravityListener, sensor, SensorManager.SENSOR_DELAY_GAME);
  }
示例#16
0
 @Override
 protected void onStop() {
   _sensorManager.unregisterListener(_linearAccelerationListener);
   _sensorManager.unregisterListener(_orientationSensorListener);
   _sensorManager.unregisterListener(_gyroSensorEventListener);
   super.onStop();
 }
示例#17
0
  public void getSensors() {
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

    mMagneticFieldSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    mGyroscopeSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
    mAccelerometerSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  }
示例#18
0
 public void start() {
   // 模拟器下启动时键盘模拟重力
   if (LSystem.isEmulator()) {
     _state._isConnected = true;
     sensorProcess = new SensorProcess(accelerometerValues);
     RealtimeProcessManager.get().addProcess(sensorProcess);
     return;
   }
   if (!_state._isConnected && manager == null) {
     manager = (SensorManager) LSystem.getActivity().getSystemService(Context.SENSOR_SERVICE);
     if (manager.getSensorList(Sensor.TYPE_ACCELEROMETER).size() == 0) {
       _state._isConnected = false;
     } else {
       Sensor accelerometer = manager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
       accelerometerListener =
           new SensorListener(this.accelerometerValues, this.magneticFieldValues);
       _state._isConnected =
           manager.registerListener(
               accelerometerListener, accelerometer, SensorManager.SENSOR_DELAY_GAME);
     }
     // 如果无法正常启动,则开启伪重力感应
     if (!_state._isConnected) {
       _state._isConnected = true;
       sensorProcess = new SensorProcess(accelerometerValues);
       RealtimeProcessManager.get().addProcess(sensorProcess);
     }
   }
 }
示例#19
0
  public void initializeSensor(Context context, boolean forceNavigationMode) {
    try {

      try {
        if (!Settings.getInstance()
                .getBooleanValue(getText(R.string.settings_key_orientation).toString())
            && !forceNavigationMode) {
          log.log(Level.FINE, "Orientation is disabled in settings");
          return;
        }
      } catch (NoSuchFieldError e) {
        log.log(Level.SEVERE, "", e);
        return;
      }

      log.log(Level.FINE, "initialize sensor");
      SensorManager mSensorManager =
          (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
      mSensorManager.registerListener(
          this,
          mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
          SensorManager.SENSOR_DELAY_NORMAL);
    } catch (Exception e) {
      log.log(Level.SEVERE, "", e);
    }
  }
  private void _listenTo(final int TYPE) {
    Sensor sensor = _sensorManager.getDefaultSensor(TYPE);

    if (!_isSupported(sensor)) return;

    _sensorManager.registerListener(this, sensor, sensor.getMaxDelay());
  }
 /** onResum */
 @Override
 protected void onResume() {
   /** Se llama cuando la actividad va a comenzar a interactuar con el usuario */
   super.onResume();
   sensorManager.registerListener(this, mStepCounterSensor, SensorManager.SENSOR_DELAY_FASTEST);
   sensorManager.registerListener(this, mStepDetectorSensor, SensorManager.SENSOR_DELAY_FASTEST);
 }
 @Override
 protected void onPause() {
   super.onPause();
   getWindow().getDecorView().removeCallbacks(r);
   SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
   sensorManager.unregisterListener(listener);
 }
 private void initSensor() {
   sensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);
   sensorManager.registerListener(
       this,
       sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
       SensorManager.SENSOR_DELAY_NORMAL);
 }
示例#24
0
 private void unregisterSensors() {
   Logger.d(this, "unregisterSensors");
   final SensorManager sensorManager = getSensorManager();
   if (sensorManager != null) {
     sensorManager.unregisterListener(this);
   }
 }
示例#25
0
  @Override
  public void init() {
    if (!pluginEnabled) {
      /* here should be your specific initialization code */

      SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
      frequency = prefs.getInt(EditActivity.PREF_FREQUENCY, 50);
      pluginId = prefs.getInt(EditActivity.KEY_PLUGIN_ID, -1);
      ignoreThreshold = EditActivity.getRate(frequency);

      prefs.registerOnSharedPreferenceChangeListener(this);

      // make sure not to call it twice
      sm = (SensorManager) getSystemService(SENSOR_SERVICE);
      List<Sensor> sensors = sm.getSensorList(Sensor.TYPE_ORIENTATION);
      if (sensors != null && sensors.size() > 0) {
        orientationSensor = sensors.get(0);
        sm.registerListener(this, orientationSensor, SensorManager.SENSOR_DELAY_UI);
        pluginEnabled = true;
      } else {
        Toast.makeText(
                this, "Accelerometer sensor is not available on this device!", Toast.LENGTH_SHORT)
            .show();
      }
    }
  }
示例#26
0
  private void register_listeners() {

    CSensorStates lSenStates = mSenStates;
    CLocProvStates lLPStates = mLPStates;
    DataOutputStream[] lfout = fout;
    SensorManager lSenMan = mSenMan;
    LocationManager lLocMan = mLocMan;

    // Register the sensors
    if (lfout[0] != null) {
      for (int i = 0; i < lSenStates.getNum(); i++) {
        if (lSenStates.getActive(i))
          lSenMan.registerListener(
              this, lSenMan.getDefaultSensor(lSenStates.getType(i)), lSenStates.getRate(i));
      }
    }

    // Register listeners for active location providers
    if (lfout[1] != null) {
      for (int i = 0; i < lLPStates.getNum(); i++) {
        if (lLPStates.getActive(i))
          lLocMan.requestLocationUpdates(
              lLPStates.getName(i), lLPStates.getMinTime(i), lLPStates.getMinDist(i), this);
      }
    }

    if (lfout[2] != null) {
      lLocMan.addGpsStatusListener(this);
    }
  }
示例#27
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setAmbientEnabled();

    mContainerView = (BoxInsetLayout) findViewById(R.id.container);
    mImageView = (ImageView) findViewById(R.id.imageView);

    gestureDetector = new GestureDetector(getApplicationContext(), new GameGestureListener());

    mImageView.setOnTouchListener(
        new View.OnTouchListener() {

          @Override
          public boolean onTouch(View v, MotionEvent event) {
            return gestureDetector.onTouchEvent(event);
          }
        });

    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mGyroSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
    mHeartSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE);

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

    // up vector cal
    mHeadTracker = HeadTracker.createFromContext(getApplicationContext());
    headView = new float[16];
  }
示例#28
0
  @Override
  protected void onResume() {
    super.onResume();

    mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
    mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_UI);
  }
 public AccelerometerHandler(Context context) {
   SensorManager manager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
   if (manager.getSensorList(Sensor.TYPE_ACCELEROMETER).size() != 0) {
     Sensor accelerometer = manager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
     manager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME);
   }
 }
示例#30
0
 protected void onPause() {
   super.onPause();
   accsensorManager.unregisterListener(this);
   proxsensorManager.unregisterListener(this);
   pressensorManager.unregisterListener(this);
   lightsensorManager.unregisterListener(this);
 }