Android Question Vibration meter

sangee

Member
Licensed User
Longtime User
Hello All...

I am trying to make an app which can sense if the phone has been put on a vibrating platform such as a vehicle dashboard or a car mobile holder. My idea is to start my app which is like a dashboard app which the user can use to switch on and off things like bluetooth, wifi etc easily when driving (I know this is not advisable but I would want my app to have this feature :) ). When searching for some idea on how to go about I stumbled upon a post in Stackoverflow with below code.. Can anyone help in pointing to a direction I should take to achieve this using accelerometer please.. Thanks. Sangee

B4X:
 public class ShakeListener implements SensorEventListener
{
      private static final int FORCE_THRESHOLD = 300;
      private static final int TIME_THRESHOLD = 100;
      private static final int SHAKE_TIMEOUT = 500;
      private static final int SHAKE_DURATION = 1000;
      private static final int SHAKE_COUNT = 3;

      private SensorManager mSensorMgr;
      private float mLastX=-1.0f, mLastY=-1.0f, mLastZ=-1.0f;
      private long mLastTime;
      private OnShakeListener mShakeListener;
      private Context mContext;
      private int mShakeCount = 0;
      private long mLastShake;
      private long mLastForce;

      public interface OnShakeListener
      {
        public void onShake();
      }

      public ShakeListener(Context context)
      {
        mContext = context;
        resume();
      }

      public void setOnShakeListener(OnShakeListener listener)
      {
        mShakeListener = listener;
      }

      public void resume()
      {
        mSensorMgr = (SensorManager)mContext.getSystemService(Context.SENSOR_SERVICE);
        if (mSensorMgr == null)
        {
          throw new UnsupportedOperationException("Sensors not supported");
        }

        boolean supported = false;

        try
        {
            supported = mSensorMgr.registerListener(this, mSensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
        }
        catch (Exception e)
        {
            Toast.makeText(mContext, "Shaking not supported", Toast.LENGTH_LONG).show();
        }

        if ((!supported)&&(mSensorMgr != null)) mSensorMgr.unregisterListener(this);
      }

      public void pause()
      {
        if (mSensorMgr != null)
        {
          mSensorMgr.unregisterListener(this);
          mSensorMgr = null;
        }
      }

      public void onAccuracyChanged(Sensor sensor, int accuracy)
      {

      }

      public void onSensorChanged(SensorEvent event)
      {
        if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
            return;
        long now = System.currentTimeMillis();

        if ((now - mLastForce) > SHAKE_TIMEOUT)
        {
          mShakeCount = 0;
        }

        if ((now - mLastTime) > TIME_THRESHOLD)
        {
          long diff = now - mLastTime;
          float speed = Math.abs(event.values[SensorManager.DATA_X] + event.values[SensorManager.DATA_Y] + event.values[SensorManager.DATA_Z] - mLastX - mLastY - mLastZ) / diff * 10000;
          System.out.println("SPEED OF THE VIBRATION "+speed);
          if (speed > FORCE_THRESHOLD)
          {
            if ((++mShakeCount >= SHAKE_COUNT) && (now - mLastShake > SHAKE_DURATION))
            {
              mLastShake = now;
              mShakeCount = 0;
              if (mShakeListener != null)
              {
                mShakeListener.onShake();
              }
            }
            mLastForce = now;
          }
          mLastTime = now;
          mLastX = event.values[SensorManager.DATA_X];
          mLastY = event.values[SensorManager.DATA_Y];
          mLastZ = event.values[SensorManager.DATA_Z];
        }
      }
}
 

sangee

Member
Licensed User
Longtime User
oh With my version of B4A the Physical Activity Recognition Detection
does not work. I am unable to add the #AdditionalRes: option. Time to upgrade I suppose.

The second method of using Shake Event with Phonesensors will trigger a notification only when the phone is shaken using hand and not the vibration of phone itself.

Would it be a method to interpret sensor_SensorChanged events and check the axis to determine the vibration levels?.. some guidance would be very helpful please..
 
Upvote 0
Top