Android - listening on NFC adapter state changed

I am trying to create an application that uses NFC. The goal is to display a DialogFragment containing a link to a button to navigate to settings and change it manually, and when this feature is enabled, disable the DialogFragment.

Problem. If the user enables / disables NFC using the icon in the failure notification tray, then onPause / onResume is not called and does not skip this condition entirely. I'm sure there is a receiver that I can register to and respond appropriately in real time. Any ideas, thoughts or links would be greatly appreciated!

The following code checks if the state is enabled / disabled. I also respond to it appropriately in the onResume event.

    NfcManager manager = (NfcManager) getSystemService(Context.NFC_SERVICE);
    NfcAdapter adapter = manager.getDefaultAdapter();

    if(adapter != null && adapter.isEnabled()) {
        detector = new NfcDetector(this);
        detector.setListener(this);
        onNfcFeatureFound();
    }
    else {
        onNfcFeatureNotFound();
    }

      

For other users viewing this post, the code below will take the user directly to the settings to enable / disable NFC:

startActivity(new Intent(android.provider.Settings.ACTION_NFC_SETTINGS));

      

+4


source to share


2 answers


I thought I should post an answer for other people looking for the same problem as I couldn't find the answer easily.

Add the following code to your onCreate () method:

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

      



An inner private class declared in your activity (or wherever 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
    NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);

    if(adapter != null && adapter.isEnabled()) {
        createNfcDetector();
        //NFC is available on device, but disabled
    }
    else {
        //NFC Is available and enabled
    }
}

      

+17


source


You can use ACTION_ADAPTER_STATE_CHANGED

to receive a broadcast message when adapter state changes, but this parameter is only available in API 18 and above. See this for documentation.

Until the age of 18, I do not know how to do this, unfortunately.



Also, as well as aside, android.provider.Settings.ACTION_NFC_SETTINGS

will work at API levels 16 and above. For previous versions, the NFC settings are located under "wireless settings". Take a look at the method ensureSensorIsOn

at the bottom of this blog post for sample code that checks the API level and redirects to the correct settings panel.

+4


source







All Articles