Using magnetic sensor

Announcements

Measure CO2 When It Matters - Infineon’s XENSIV™ PAS CO2 now comes in SparkFun Red. Check it now!

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
VictorJohnsonan
Level 1
Level 1
5 sign-ins First reply posted First question asked

Hi

I want to get the three coordinates values of the magnetic field measured by the sensor of my phone. For this, I get a handle to the SensorManager by using sm=(SensorManager)getApplicationContext().getSystemService(Context.SENSOR_SERVICE), then get the sensor with cm=sm.getDefaultSensor(SensorManager.SENSOR_MAGNETIC_FIELD). I then register a SensorEventListener to the SensorManager with sm.registerListener(new SensorListener(),cm,SensorManager.SENSOR_DELAY_UI).

The classSensorListener is a class of my own implementing the SensorEventListener interface. In it's OnSensorChanged method, I get the values from the sensor and I display them. The problem is that I only get the values 1,0 and 0. And they are rarely updated (I have put a counter on the onSensorChanged calls to see how often the update takes place). Changing the time to SENSOR_DELAY_NORMAL doesnot improve anything.

To check if the problem was related to the magnetic sensor, I have added, in the same way, a listener to an accelerometer sensor. The result is very confusing : now, the magnetic sensor generates updates, but not the accelerometer one. And if I remove the accelerometer sensor event listener, I still receive the magnetic sensor events which where missing before adding the accelerometer sensor event listener.(???????????)

Any idea about what is wrong in my code?

0 Likes
1 Reply
Deepa_V
Moderator
Moderator
Moderator
First comment on KBA 50 likes received 250 replies posted

Hi @VictorJohnsonan ,

The forum is for supporting infineon products only. In case you have any queries on one of our products, we'll be happy to support you. 

Best regards,

Deepa

0 Likes