What I want to archieve is a sort of "magnetic fingerprint" of a location. I use the MAGNETIC_FIELD sensor and in the event I get the 3 values for the (unfortunately not further explained) X, Y and Z axis.
Problem is, that the values change as I rotate the device, so I guess the 3 axis are relative to the device. What I'd need is to compensate the device rotation so that I get the same 3 values, regardless of how the device is rotated.
I tried to multiply with the rotation matrix (I know how to get that), tried to multiply with the inclination matrix and so on, but nothing works. Regardless of what I try, still the values change when I rotate the device.
So does anyone know how to do it right? Preferrably with code, because I read a lot of stuff like 'well then you'll have to compensate that using rotation matrix' but did not find a single concrete, working example.
Do this
Solution :
Possibility 1 :
MAGNETIC_FIELD is very unstable regarding rotation you could not relate on just some math to convert landscape and portrait values, the main reason is the hardware use different captors for different axises then when rotate occur you use a different hardware captor the value will never be the same, on some hi end device it will be the same but not on most devices.
if you want something to rely on and compatible on many devices you need to forget calculating the MAGNETIC_FIELD with rotation, but just force the orientation with your application. Force "portrait" orientation mode
Possibility 2 :
You talked about
"magnetic fingerprint" of a location.
if it's only about identifying a location without GPS you have plenty of other informations to work with. first "Wifi SSIDs" then "Mobile Networks Cells" also "Connected Wifi" etc. if this interest you i could give you code for it.Possibility 3 :
If you absolutely need to calculate MAGNETIC_FIELD for your location, and don't want to force rotation... you could catch both value landscape and portrait instead of calculating them. then when you compare just compare to both values.
Also if you are looking to locate place where you have magnet or hight magnetic location you could work with a hi tolerance percentage just to detect if a magnet is present or so,
Note :
If you stick with the math don't forget that the function will be different on almost every device... don't hesitate to give more infos about your question i'll be pleased to adapt my answer ;)
The coordinates of the magnetic field vector are given relative to the mobile phone, like in this picture:
To get the magnetic field vector in the coordinate system of the following picture:
you have to multiply the magnetic vector
m
with the rotation matrixR
retrieved from getRotationMatrix() likeR * m
. This vector will point through the earth to the magnetic north pole.If you also multiply it with inclination
I
, the vector would be rotated around the X-axis to be fully on the Y-axis:This vector should be constant for each position on the earth. However, results on your mobile phone may show slight deviations, because you have to be very careful not to change the position of the sensor when rotating the device.
to get the strength of the magnetic field, you have to get the x,y,z values of the magnetic field (from Sensor.TYPE_MAGNETIC_FIELD), and apply the following formula:
magnetic_field_strength is expressed in microtesla (µT)
It could be noted that the average magnetic field strength of the Earth is 50 µT, according to this website.
So a possible code would be: