How to use Magnetic Field Sensor?

edited November 2017 in Android Mode

I can't figure out how to use the Magnetic field sensor, I may be completely dumb but take a look at this:

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;

Context context;
SensorManager manager;
Sensor sensor;
float X1;
float Y1;
float Z1;

void setup() {
  fullScreen();

  context = getActivity();
  manager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
  sensor = manager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

 // manager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME);
}
public void onSensorChanged(SensorEvent event)
     {
       X1 = event.values[0];
       Y1 = event.values[1];
       Z1 = event.values[2];
     }

void draw() {


  text(str(X1) + " " + str(Y1) + " " + str(Z1) , width/2, height/2);
}

All I get is 0.0, 0.0, 0.0 (And my phone does have the sensor) Does anyone know the proper way of doing this?

Tagged:

Answers

  • Answer ✓

    Please install the ketai library. Check the ketai.org website for an example.

    Kf

  • Sad to see the library is programmed using the private keyword way to much... For the rest it seems nice, it would be nice if it would be integrated in the default android mode.

  • edited December 2017 Answer ✓

    @coolphill===

    you dont need ketai but:

    your code cannot run for a lot of reasons:

    • first is that you dont get any context and so your sensor is null: you are in a fragment

    • second is that you never add a listener to your sensor manager: it cannot return any values

    • third is that you never clear your background in draw

    here a snippet which will run (using your code and modifying it); in order to improve it you have to add onPause() && onResume() for registering and unregistering.

        import android.content.Context;
        import android.hardware.Sensor;
        import android.hardware.SensorManager;
        import android.hardware.SensorEvent;
        import android.hardware.SensorEventListener;
    
        Context context;
        SensorManager manager;
        Sensor sensor;
    
        float X1;
        float Y1;
        float Z1;
    
    
        void setup() {
          fullScreen();//better in settings()
    
          context = getActivity().getApplicationContext();//get the context from the //main activity
          manager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
          sensor = manager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    
         //verify wether the sensor is ok; if yes add a listener
         if (sensor != null) {
                    manager.registerListener(listener, sensor,
                            SensorManager.SENSOR_DELAY_NORMAL);
                   println("CAPTEUR", "ENREGISTREMENT DU CAPTEUR MAGNETIQUE");
    
                } else {
    
                    println("SORRY!!!!-CE TELEPHONE N'A PAS DE CAPTEUR MAGNETIQUE");
    
                }
          };
    
      //adding the listener (two methods are mandatory from the interface) 
    
        public SensorEventListener listener = new SensorEventListener() {
    
    
                public void onAccuracyChanged(Sensor sensor, int accuracy) {
                }
    
               public void onSensorChanged(SensorEvent event)
             {
               float[] val = event.values;
               X1 = val[0];
               Y1 = val[1];
               Z1 = val[2];
    
             }
            };
    
    
        void draw() {
         background(255);//if you want to see your values
    
         textSize(36);//phone res is probably too high for using default!
    
         fill(255,0,0);
          text(str(X1) + " " + str(Y1) + " " + str(Z1) , width/2, height/2);
        }
    
  • @coolphill You can also check the sensor tutorial in the Processing Android website: http://android.processing.org/tutorials/sensors/index.html

    Kf

Sign In or Register to comment.