how to detect a metal using magnetic sensor in android phone?

android, android-sensors

Solution

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.

Problem

I want to detect a metal using magnetic sensor values. i am getting values continuously like x=30.00 ,y=-20.00 ,z=-13.00 now i want to know how to use these values for detecting any metal(mathameticalcalu,formulas) code is ``` sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); // get compass sensor (ie magnetic field) myCompassSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); float azimuth = Math.round(event.values[0]); float pitch = Math.round(event.values[1]); float roll = Math.round(event.values[2]); ```

Original source