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

爷,独闯天下 提交于 2019-12-23 14:55:00

问题


  1. 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

  2. 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]);

回答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.




回答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 .



来源:https://stackoverflow.com/questions/16371777/how-to-detect-a-metal-using-magnetic-sensor-in-android-phone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!