如何在Android手机中使用磁传感器检测金属? [英] how to detect a metal using magnetic sensor in android phone?

查看:139
本文介绍了如何在Android手机中使用磁传感器检测金属?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 我想使用磁传感器值检测金属.我一直在不断获取值,例如x = 30.00,y = -20.00,z = -13.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

现在我想知道如何使用这些值来检测任何金属(mathameticalcalu,分子式)

now i want to know how to use these values for detecting any metal(mathameticalcalu,formulas)

代码是

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

推荐答案

要检测金属,您必须检查磁场强度,即磁场矢量的大小.

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);

然后,您需要将此值与地球上您所在位置的磁场预期值进行比较.幸运的是,Android提供了这样做的功能.查看GeomagneticField,参考在这里 https://developer.android.com/reference/android/hardware/GeomagneticField.html

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
}

您应该对1.40.6值进行一些试验,以使其适合您的应用程序.请注意,这永远不会100%地起作用,因为智能手机上的磁传感器非常便宜且令人讨厌.

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.

这篇关于如何在Android手机中使用磁传感器检测金属?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆