-1

I am working on a project where I have to find the direction. I wrote a code and did test. It worked fine in some devices, but in other devices it(the pointer) didn't even move. I studied about this and got to know that Magnetic Sensor is the only key on which Compass works.

    public class Compass extends Activity implements SensorListener {
  SensorManager sensorManager;
  static final int sensor = SensorManager.SENSOR_ORIENTATION;
  ViewCOm viewcom;


  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set full screen view
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    viewcom = new ViewCOm(this);

    setContentView(viewcom);

    // get sensor manager
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
  }

  // register to listen to sensors
  @Override
  public void onResume() {
    super.onResume();
    sensorManager.registerListener(this, sensor);
  }

  @Override
  public void onPause() {
    super.onPause();
    sensorManager.unregisterListener(this);
  }

  // Ignore for now
  public void onAccuracyChanged(int sensor, int accuracy) {
  }

  // Listen to sensor and provide output
  public void onSensorChanged(int sensor, float[] values) {
    if (sensor != Compass.sensor)
      return;
    int orientation = (int) values[0];
    viewcom.setDirection(orientation);
  }
}

MY Question is, is there any other way like GPS or MAP to create Compass??

Thanks in advance.

Devraj
  • 1,479
  • 1
  • 22
  • 42

2 Answers2

2

MY Question is, is there any other way like GPS or MAP to create Compass??

You cannot create compass feature without involving magnetic sensor. GPS will not help here either and it would only let you calculate the direction you are moving to (and as you'd need at least two different coords you cannot do that while standing). You can try to calculate where North is too, however note that precision of this will require bigger coords differences than i.e. 5 steps.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • 2
    work on your attitude. and your English, 'cos i got no really clue what the above line is about. – Marcin Orlowski Mar 31 '16 at 08:33
  • 2
    But what in my answer is not addressing your question really? You want compass without magnetic sensor - I answered it is not going to work really and why. If you need more clarification ask on the subject – Marcin Orlowski Mar 31 '16 at 08:42
  • 1
    :/ Sir, I didnt ask that I want to make a compass without Magnetic sensor. It was my curiosity to know that is it possible or not. but seniors are like school teacher. U love to slap but not understand. It is ok sir. I am really sorry about my words. but this is true. curiosity is the key of inventions. – Devraj Mar 31 '16 at 08:46
  • 1
    `It was my curiosity to know that is it possible or not` - I am sure my answer satisfies your curiosity too. – Marcin Orlowski Mar 31 '16 at 08:48
  • 1
    @Devraj is it possible to make a compass without Magnetic sensor? – Amit Desale Jul 23 '16 at 12:18
  • 1
    @AmitDesale no, we cannot. please check my answer for more details. – Devraj Jul 26 '16 at 06:47
1

Basically there are three major types of censors that can be use for compass.

1) HALL Sensor.

2) Anisotropic Magneto-resistive(AMR).

3) Giant Magneto-resistive(GMR).

Any of these type have a magnetic sensor that alters its resistance proportional to the magnetic fields in a particular direction.

Circuiting on the chip detects the magnetic filed strength and makes the field and its direction available as digital data. The CPU on the phone pull this data whenever compass data is required.

So, If you have anyone of them in your phone, you can use compass.

Devraj
  • 1,479
  • 1
  • 22
  • 42