0

I am developing an app which requires NFC. I found a way to make sure the user enables nfc if it isn't already...

public void nfcOnPressed(View view){

        NfcAdapter nfc = NfcAdapter.getDefaultAdapter(view.getContext());

        if (!nfc.isEnabled())
        {
            Toast.makeText(getApplicationContext(), "Activate NFC", Toast.LENGTH_LONG).show();
            startActivity(new Intent(Settings.ACTION_NFC_SETTINGS));
        } else{
            Toast.makeText(getApplicationContext(), "NFC is already activated.", Toast.LENGTH_LONG).show();
        }
    }

Now, here is my problem: the user has to go to another page to use the nfc feature but there is no check while being on the other page if he turns it off mid-process.

An example: I open my app, enable NFC, go to the next page (before going to the next page another method checks if it is still enabled, if it isn't I can't even move on) and then, after the new page is shown, I disable it. So now I am on the new page with all the things shown I can do with NFC but you can not, since, well, I disabled it.

Is there a way to have something like a timed check if NFC is enabled? Or is there a way to check it constantly (even thought I wouldn't really want to do that).

If you need more info, just tell me what you need.

Thank you very much. :)

1 Answers1

1

Why not listen for when the NFC state changes via a Broadcast Receiver?

Add the following code to your activities onCreate() method:

IntentFilter filter = new IntentFilter(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
this.registerReceiver(mReceiver, filter);

Inner private class declared within your activity (or anywhere else you like):

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();

        if (action.equals(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED)) {
            final int state = intent.getIntExtra(NfcAdapter.EXTRA_ADAPTER_STATE,
                                                 NfcAdapter.STATE_OFF);
            switch (state) {
            case NfcAdapter.STATE_OFF:
                break;
            case NfcAdapter.STATE_TURNING_OFF:
                break;
            case NfcAdapter.STATE_ON:
                break;
            case NfcAdapter.STATE_TURNING_ON:
                break;
            }
        }
    }
};

@Override
public void onDestroy() {
    super.onDestroy();

    // Remove the broadcast listener
    this.unregisterReceiver(mReceiver);
}

  // The following check needs to also be added to the onResume
@Override
protected void onResume() 
    super.onResume();
  // Check for available NFC Adapter
    PackageManager pm = getPackageManager();
    NfcManager manager = (NfcManager) getSystemService(Context.NFC_SERVICE);
    NfcAdapter adapter = manager.getDefaultAdapter();

    if(adapter != null && adapter.isEnabled()) {
        createNfcDetector();
        onNfcFeatureFound();
    }
    else {
        onNfcFeatureNotFound();
    }
}

Credit goes to: Android - Listening to NFC Adapter State Changed

Community
  • 1
  • 1
Dzhuneyt
  • 8,437
  • 14
  • 64
  • 118
  • Well, today you are my superhero. Thank you very much! It worked without a problem. Now I can finally focus on different things. :D –  Sep 02 '15 at 11:38