androidnfcsamsung-mobileandroid-beam

getPackageManager().hasSystemFeature(NFC_SERVICE) Not Working in Samsung S5


For some reason getPackageManager().hasSystemFeature(NFC_SERVICE) is returning null in SamSung S5 even though I have NFC enabled as in the screen shot below.

enter image description here

And here is the code that I am using to test if NFC Service is available and it is failing the first test

//check for the existence of
    // NFC adapter and beaming capability

    if (!getPackageManager().hasSystemFeature(NFC_SERVICE)){
        //this device is not equipped with NFC
        makeToast("This device does not have NFC hardware.");
        return;
    }
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
        // Android Beam feature is not supported.
        makeToast("Android Beam is not supported in this device");
        return;

    }

    // Check whether NFC is enabled on device
    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);

    if (!mNfcAdapter.isEnabled()){
        makeToast("Please enable NFC");
    }

    if(!mNfcAdapter.isNdefPushEnabled()) {
        // Android Beam is disabled, show the settings UI
        // to enable Android Beam
        makeToast("Please enable Android Beam.");
        startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS));
        return;
    }

Is there anything I should do differently?


Solution

  • NFC_SERVICE is not a valid value to pass to hasSystemFeature(). Use FEATURE_NFC.