/** * Notify that NFC is available */ protected void onNfcFeatureFound() { initializeNfc(); detectInitialNfcState(); }
@Override protected void onPause() { super.onPause(); if(nfcAdapter != null) { disableForeground(); // for quicksettings stopDetectingNfcStateChanges(); } }
/** * * Start detecting NDEF messages * */ protected void startDetecting() { if(!detecting) { enableForeground(); detecting = true; } }
@Override protected void onResume() { super.onResume(); if(nfcAdapter != null) { // enable foreground mode if nfc is on and we have started detecting boolean enabled = nfcAdapter.isEnabled(); if(enabled && detecting) { enableForeground(); } detectNfcStateChanges(); // for quicksettings startDetectingNfcStateChanges(); } if(!intentProcessed) { intentProcessed = true; processIntent(); } }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "onCreate"); nxpMifareClassic = hasMifareClassic(); // Check for available NFC Adapter PackageManager pm = getPackageManager(); if(pm.hasSystemFeature(PackageManager.FEATURE_NFC) && NfcAdapter.getDefaultAdapter(this) != null) { Log.d(TAG, "NFC feature found"); onNfcFeatureFound(); } else { Log.d(TAG, "NFC feature not found"); onNfcFeatureNotFound(); } }
/** * * Process the current intent, looking for NFC-related actions * */ public void processIntent() { Intent intent = getIntent(); // Check to see that the Activity started due to an Android Beam if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) { Log.d(TAG, "Process NDEF discovered action"); onNfcIntentDetected(intent, NfcAdapter.ACTION_NDEF_DISCOVERED); } else if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) { Log.d(TAG, "Process TAG discovered action"); onNfcIntentDetected(intent, NfcAdapter.ACTION_TAG_DISCOVERED); } else if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) { Log.d(TAG, "Process TECH discovered action"); onNfcIntentDetected(intent, NfcAdapter.ACTION_TECH_DISCOVERED); } else if (ACTION_TAG_LEFT_FIELD.equals(intent.getAction())) { Log.d(TAG, "Process tag lost action"); onNfcTagLost(intent); // NOTE: This seems not to work as expected } else { Log.d(TAG, "Ignore action " + intent.getAction()); } }
/** * * Stop detecting NDEF messages * */ protected void stopDetecting() { if(detecting) { disableForeground(); detecting = false; } }
private boolean hasMifareClassic() { return getPackageManager().hasSystemFeature("com.nxp.mifare"); }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); startReceivingTagBroadcasts(); startReceivingReaderBroadcasts(); startReceivingServiceBroadcasts(); }
@Override protected void onDestroy() { stopReceivingTagBroadcasts(); stopReceivingReaderBroadcasts(); stopReceivingServiceBroadcasts(); super.onDestroy(); }