How to Determine if Android Device has a Magnetometer (Compass)

public static final String FEATURE_SENSOR_COMPASS was added in API level 8
Feature for getSystemAvailableFeatures() and hasSystemFeature(String).
The device includes a magnetometer (compass).
Constant Value: "android.hardware.sensor.compass"


1. Get a PackageManager Instance named myPackageManager.

2. Create a Boolean Variable named bolCompassSupported

3. Set the Minimum and Target SDK version  to or greater. 

4. In the onCreate method, add the below code. This will return the compass feature availability to a boolean named bolCompassSupported. It will then display a toast displaying the compass feature availability of the device to the user.

bolCompassSupported = myPackageManager.hasSystemFeature(PackageManager.FEATURE_SENSOR_COMPASS);
if (bolCompassSupported) {
Toast.makeText(this, "Device has a Compass", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Device does NOT have a Compass!", Toast.LENGTH_LONG).show();
}

5. Compile and run!

Resources:
http://developer.android.com/reference/android/content/pm/PackageManager.html#FEATURE_SENSOR_COMPASS