We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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?
Answers
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.@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.
@coolphill You can also check the sensor tutorial in the Processing Android website: http://android.processing.org/tutorials/sensors/index.html
Kf