How To Detect A Metal Using Magnetic Sensor In Android Phone?
Solution 1:
To detect metal, you have to check the intensity of the magnetic field, i.e. the magnitude of the magnetic field vector.
float mag = Math.sqrt(x^2 + y^2 + z^2);
Then you need to compare this value to the expected value of the magnetic field at your location on Earth. Luckily, Android provides functions to do so. Look at the GeomagneticField
, reference is here https://developer.android.com/reference/android/hardware/GeomagneticField.html
Then if the value you are reading out of the sensors is quite far from the expected value, there's "something" (you guessed it, metal) that is disturbing the Earth magnetic field in the vicinity of your sensor. A test you could implement for instance is the following:
if (mag > 1.4*expectedMag || mag < 0.6*expectedMag) {
//there is a high probability that some metal is close to the sensor
} else {
//everything is normal
}
You should experiment a bit with the 1.4
and 0.6
values so that it fits your application. Note that this is never going to work 100% of the time because the magnetic sensors on a smartphone are quite cheap and nasty.
Solution 2:
You can detect magnetic field using android Magnetic field sensor not metals.But Metals which are having magnetic field also will be detected e.g iron,nickel etc.Because ferrous metals behave the same way as a live electric cable .
Post a Comment for "How To Detect A Metal Using Magnetic Sensor In Android Phone?"