How to detect metal using a magnetic sensor in an Android phone?

  • I want to detect metal using magnetic sensor values. I get values ​​continuously like x = 30.00, y = -20.00, z = -13.00

  • Now I want to know how to use these values ​​to detect any metal (mathematical data, formulas)

code

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]); 
+4
source share
2 answers

To detect metal, you must check the intensity of the magnetic field, i.e. magnitude of the magnetic field vector.

 float mag = Math.sqrt(x^2 + y^2 + z^2); 

Then you need to compare this value with the expected value of the magnetic field at your location on Earth. Fortunately, Android provides features for this. Take a look at GeomagneticField , the link is here https://developer.android.com/reference/android/hardware/GeomagneticField.html

Then, if the value that you read from the sensors is quite far from the expected value, there is “something” (you guessed it, metal), which interferes with the Earth’s magnetic field near your sensor. For example, a test that you can implement is as follows:

 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 with a bit with values ​​of 1.4 and 0.6 so that it matches your application. Please note that this will never work in 100% of cases, because the magnetic sensors on the smartphone are quite cheap and nasty.

+5
source

You can detect the magnetic field using android. The magnetic field sensor is not metals. But metals that have a magnetic field, such as iron, nickel, etc., will be detected. Because ferrous metals behave just like a live electric cable.

0
source

Source: https://habr.com/ru/post/1479105/


All Articles