0

I'am trying to read gyroscope values from HAL using the below apk. The apk is successfully enables the gyroscope sensor and reads and process events from kernel ,no crash in the adb-log but i'am not able to read any data even in the log or on the screen. For Accelerometer and magnetic field I got proper data.(on kitkat board) I would like to know why the data is missing.Why can't I read any x value if it is able go in reading events successfully?

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public class MySensorActivity extends Activity implements SensorEventListener {

    private TextView tv;
    private SensorManager mSensorManager;
    private Sensor mLightSensor,mGyroSensor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv= (TextView)findViewById(R.id.txt2);
        // Get an instance of the sensor service
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mLightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
        mGyroSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);

        PackageManager PM= this.getPackageManager();
        boolean gyro = PM.hasSystemFeature(PackageManager.FEATURE_SENSOR_GYROSCOPE);
        boolean light = PM.hasSystemFeature(PackageManager.FEATURE_SENSOR_LIGHT);

        if(gyro){

            if(light){
                Toast.makeText(getApplicationContext(),"Both light and gyroscope sensors are present", Toast.LENGTH_LONG).show();
            }
            else
                Toast.makeText(getApplicationContext(),"Only gyroscope sensor is present", Toast.LENGTH_LONG).show();

        }
    }

    @Override
    public final void onAccuracyChanged(Sensor sensor, int accuracy) {
        // Do something if sensor accuracy changes.
    }

    @Override
    public final void onSensorChanged(SensorEvent event) {

        //float lux = event.values[0];
        float angularXSpeed = event.values[0];
        tv.setText("Angular X speed level is: " + "" +angularXSpeed); 
    }

    @Override
    protected void onResume() {
        // Register a listener for the sensor.
        super.onResume();
        mSensorManager.registerListener(this, mGyroSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onPause() {
        // important to unregister the sensor when the activity pauses.
        super.onPause();
        mSensorManager.unregisterListener(this);
    }

}
kapilgm
  • 1,620
  • 1
  • 15
  • 22
jak
  • 3
  • 3

1 Answers1

1

You have to register listener for all sensor you want to use:

// Declare as static at start of class (Global Variables)

    static SensorManager sensorManager;
    static Sensor mAccelerometer;
    private Sensor mMagnetometer;
    private Sensor mLinearAccelertion;
    static Context mContext;

Then OnCreate Service Function

public void onCreate(){ 

    super.onCreate(); 
    mContext = getApplicationContext();

    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

    mAccelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mMagnetometer = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    mGyro = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE); 
    mLinearAccelertion = sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);

    sensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    sensorManager.registerListener(this, mMagnetometer, SensorManager.SENSOR_DELAY_NORMAL);
    sensorManager.registerListener(this, mGyro, SensorManager.SENSOR_DELAY_NORMAL); 
    sensorManager.registerListener(this, mLinearAccelertion, SensorManager.SENSOR_DELAY_NORMAL);

} 

You have to check which sensor is creating event:

    public void onSensorChanged(SensorEvent event) {

        float[] values = event.values;
        Sensor mSensor = event.sensor;

        if(mSensor.getType() == Sensor.TYPE_ACCELEROMETER){
            float[] accelerometerValue = values;
        } 

        if(mSensor.getType() == Sensor.TYPE_LINEAR_ACCELERATION){           

        } 

        if(mSensor.getType() == Sensor.TYPE_MAGNETIC_FIELD){
            float[] magnetometerValue = values;
        } 

if(mSensor.getType() == Sensor.TYPE_GYROSCOPE){
           float[] gyroValue = values;
        } 


    } 

EDIT:

I have sensor code here:

My Algorithm to Calculate Position of Smartphone - GPS and Sensors

Community
  • 1
  • 1
Dawood Awan
  • 7,051
  • 10
  • 56
  • 119
  • Thanks.i will try.But i need to tell u onething.I have seperate apk for accel and magnetometer.Initially i was checking for only accelerometer and i got the events,later i have added an apk for magnetometer it was also getting data,but now when i add gyroscope it was not receiving any data. i need to know y? and i try this fusion code and check whether its working or not.Thankq for ur answer – jak Dec 12 '14 at 05:51
  • @jak That is what I am saying your a registering two sensors: 1. Gyro and 2. is Light Sensor. You have to check which sensor in the onSensorChanged function – Dawood Awan Dec 12 '14 at 08:47
  • APK is working properly.I am able to read angular x speed level on the screen.. But my other problem is: wandboard(kitkat) has gyro sensor ,i have included a HAL for gyro and by launching the apk through android studio im unable to even enter into onsensorchange method,remaining oncreate and registering listener in sensor java are doing their job correctly.I dont knw where the data is getting stucked.im using logcat to see the result by adding log.i statement in onsensorchanged – jak Dec 16 '14 at 06:47
  • @jak Is it possible for you to post the LogCat of your application? and also the latest working code with snapshot of application where it gets stuck – Dawood Awan Dec 16 '14 at 08:40