Android : How to check if Android mobile has Camera,Bluetooth,NFC,Sensors


           Do you want to know the features available in your Android device? like Camera, Bluetooth, WiFi, NFC, Camera flash, front facing camera, Microphone

It is simple to write  the code for this requirement.
Android gives us abstract class android.content.pm.PackageManager which has hasSystemFeature() method. This method can be used to check the available features.

Example:
PackageManager packageManager = context.getPackageManager();
if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
              // You have camera in your device
}

Some of the  important features are;
The device is capable of communicating with other devices via Bluetooth.
The device has a camera facing away from the screen.
The device's camera supports auto-focus.
The device's camera supports flash.
The device has a front facing camera.
The device supports live wallpapers.
The device can record audio via a microphone.
The device can communicate using Near-Field Communications (NFC).
The device includes an accelerometer.
The device includes a barometer (air pressure sensor.)
The device includes a magnetometer (compass).
The device includes a gyroscope.
The device includes a light sensor.
The device includes a proximity sensor.
This is a device dedicated to showing UI on a television.
The device's touch screen supports multitouch sufficient for basic two-finger gesture detection.
The device's touch screen is capable of tracking two or more fingers fully independently.
The device supports WiFi (802.11) networking.



























































Sample application:

In this sample application I am checking some of the above mentioned features and displaying whether those features are available or not.

Project structure:



AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.featurescheck"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.featurescheck.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dip"
        android:layout_marginLeft="50dip"
        android:id="@+id/res" />

</RelativeLayout>

MainActivity.java


package com.example.featurescheck;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  TextView value = (TextView) findViewById(R.id.res);
  String result = "";
  Context context = this;
  //Create PackageManager instance
  PackageManager packageManager = context.getPackageManager();
  if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
   result += "Camera: YES";
  }else{
   result += "Camera: NO";
  }
  if (packageManager
    .hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)) {
   result += "\nFront facing camera: YES";
  }else{
   result += "\nFront facing camera: NO";
  }
  if (packageManager
    .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
   result += "\nCamera support flash: YES";
  }else{
   result += "\nCamera support flash: NO";
  }
  if (packageManager.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
   result += "\nBluetooth: YES";
  }else{
   result += "\nBluetooth: NO";
  }
  if (packageManager.hasSystemFeature(PackageManager.FEATURE_NFC)) {
   result += "\nNFC: YES";
  }else{
   result += "\nNFC: NO";
  }
  if (packageManager.hasSystemFeature(PackageManager.FEATURE_MICROPHONE)) {
   result += "\nMicrophone: YES";
  }else{
   result += "\nMicrophone: NO";
  }
  if (packageManager
    .hasSystemFeature(PackageManager.FEATURE_SENSOR_ACCELEROMETER)) {
   result += "\nAccelerometer sensor: YES";
  }else{
   result += "\nAccelerometer sensor: NO";
  }
  if (packageManager
    .hasSystemFeature(PackageManager.FEATURE_SENSOR_BAROMETER)) {
   result += "Barometer (air pressure sensor) :YES";
  }else{
   result += "Barometer (air pressure sensor) :NO";
  }
  if (packageManager
    .hasSystemFeature(PackageManager.FEATURE_SENSOR_COMPASS)) {
   result += "\nMagnetometer (compass): YES";
  }else{
   result += "\nMagnetometer (compass): NO";
  }
  if (packageManager
    .hasSystemFeature(PackageManager.FEATURE_SENSOR_GYROSCOPE)) {
   result += "\nGyroscope: YES";
  }else{
   result += "\nGyroscope: NO";
  }
  if (packageManager
    .hasSystemFeature(PackageManager.FEATURE_SENSOR_LIGHT)) {
   result += "\nLight sensor: YES";
  }else{
   result += "\nLight sensor: NO";
  }
  if (packageManager
    .hasSystemFeature(PackageManager.FEATURE_SENSOR_PROXIMITY)) {
   result += "\nProximity sensor: YES";
  } else {
   result += "\nProximity sensor: NO";
  }
  value.setText(result);
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }

}

Output:


Source code:

FeaturesCheck.zip

apk file:

FeaturesCheck.apk

Simple..!! Download this apk and check whether your Android mobile has Camera, front facing camera, bluetooth, NFC, microphone, Accelerometer sensor, magnetomer, gyroscope, light sensor, proximity sensor.






Reactions:

3 comments :

  1. Thers a typo in MainActivity.java
    look at the if statement in the light sensor!

    ReplyDelete