[Tutorial] Learn to create a Magnetometer Metal Detector App for Android
- Hello,
I create this thread to present you a tutorial learning you how to create a Magnetometer Metal Detector App for Android. You can also discover this tutorial in video on Youtube :
Learn to create a Magnetometer Metal Detector on Android
Manufacturers continuously add more features and sensors to smartphones. Thus, your smartphone has now a magnetic field sensor which can help you to use your smartphone as a Magnetometer Metal Detector. For example, we have published an application of this kind on the Google Play Store : https://forums.androidcentral.com/e?...token=abCIlDQs
Magnetometer Metal Detector lets you to measure magnetic field around you by using the magnetic sensor that is built into your smartphone and tablet. It helps you to detect metal objects (steel, iron) around you. When a metal object is near to you, the magnetic field level will increase. Note that gold, silver an copper coins cannot be detected since they are classified as non-ferrous metal and so, they have no magnetic field.
In this tutorial, you are going to learn how to create a Magnetometer Metal Detector application like this we have published on the Google Play Store. First, you need to create a simple layout with a TextView widget centered on the screen :
Code:<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:background="#000000" tools:context="com.ssaurel.magnetometerapp.MainActivity"> <TextView android:id="@+id/value" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="45.00" android:textColor="#FFFFFF" android:textStyle="bold" android:textSize="23sp" android:layout_centerInParent="true"/> </RelativeLayout>
Magnetic Value = sqrt(Value_X * Value_X + Value_Y * Value_Y + Value_Z * Value_Z)
Thus, the magnetic value is equal to the square root of the sum of squared values on each axes. Code for the Activity will have the following form :
Code:package com.ssaurel.magnetometerapp; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.util.Locale; public class MainActivity extends AppCompatActivity implements SensorEventListener{ private TextView value; private SensorManager sensorManager; public static DecimalFormat DECIMAL_FORMATTER; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); value = (TextView) findViewById(R.id.value); // define decimal formatter DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US); symbols.setDecimalSeparator('.'); DECIMAL_FORMATTER = new DecimalFormat("#.000", symbols); sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); } @Override protected void onResume() { super.onResume(); sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_NORMAL); } @Override protected void onPause() { super.onPause(); sensorManager.unregisterListener(this); } @Override public void onSensorChanged(SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) { // get values for each axes X,Y,Z float magX = event.values[0]; float magY = event.values[1]; float magZ = event.values[2]; double magnitude = Math.sqrt((magX * magX) + (magY * magY) + (magZ * magZ)); // set value on the screen value.setText(DECIMAL_FORMATTER.format(magnitude) + " \u00B5Tesla"); } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }
Now, you can launch your application to try your Magnetometer Metal Detector :
That’s all for this tutorial. You can know make your own Magnetometer Metal Detector and improve it by adding a graph to show recent magnetic field activity or a Gauge View to display the magnetic field value like on the version published on the Google Play Store. You can show the application in action in this demo video :
Thanks.
Sylvain11-15-2016 04:28 AMLike 0
- Forum
- Android Developers
- Software Development and Hacking
[Tutorial] Learn to create a Magnetometer Metal Detector App for Android
« What is the file " snp_log.txt " ? Should and can, I delete it?
|
[Tutorial] Learn to Auto Restart an Android App after a Crash or a Force Close Error »
Similar Threads
-
I want to sort Android lockscreen notifications by time. How can i achieve this. Thanks
By AC Question in forum Ask a QuestionReplies: 1Last Post: 11-17-2016, 02:30 AM -
Regarding Google Play store publish app
By AC Question in forum Ask a QuestionReplies: 1Last Post: 11-17-2016, 02:28 AM -
Fingerprint for website / app login?
By amyf27 in forum LG V20Replies: 1Last Post: 11-15-2016, 05:18 AM -
how to get download manager system apk
By AC Question in forum Ask a QuestionReplies: 1Last Post: 11-15-2016, 04:30 AM
Tags for this Thread
LINK TO POST COPIED TO CLIPBOARD