hwsecurity / de.cotech.hw.util / NfcStatusObserver /

NfcStatusObserver

open class NfcStatusObserver

A helper class to monitor whether NFC hardware is available and enabled.

Note that this class does not communicate with the NFC device on its own - its only purpose is to tell when NFC functionality is available, and has been enabled or disabled!

Example:

public class NfcActionActivity extends AppCompatActivity {
    private NfcStatusObserver nfcStatusObserver;

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (!SecurityKeyManager.getInstance().isNfcHardwareAvailable()) {
            startActivityToNicelyTellUserThatNfcHardwareIsUnavailable();
            finish();
            return;
        }

        this.nfcStatusObserver = new NfcStatusObserver((Context) this, (LifecycleOwner) this,
                nfcEnabled -> showOrHideNfcDisabledView(nfcEnabled));

        // this button should be part of the "nfc disabled view"
        View buttonPleaseEnableNfc = findViewById(R.id.buttonPleaseEnableNfc);
        buttonPleaseEnableNfc.setOnClickListener(v -> startAndroidNfcConfigActivityWithHint());
    }

    private void startAndroidNfcConfigActivityWithHint() {
        Toast.makeText(getApplicationContext(),
                "Please activate NFC and press Back to return to MyApplication", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(android.provider.Settings.ACTION_NFC_SETTINGS));
    }

    protected void onResume() {
        super.onResume();
        // In case we missed a change while the activity was in the background
        showOrHideNfcDisabledView(nfcStatusObserver.isNfcEnabled());
    }
}

Constructors

Name Summary
NfcStatusObserver open fun NfcStatusObserver(context: Context, lifecycleOwner: LifecycleOwner, nfcStatusCallback: NfcStatusObserver.NfcStatusCallback)Constructs an NfcStatusObserver and binds it to a Lifecycle.

Types

Name Summary
NfcStatusCallback interface NfcStatusCallback

Functions

Name Summary
isNfcEnabled

open fun isNfcEnabled(): Boolean
open fun isNfcEnabled(context: Context): Boolean

Returns true if NFC hardware is available and enabled.

onPause open fun onPause()
onResume open fun onResume()