[General Questions and Answers] denisgottardello:
Hi. I have an app ever open and full screen in a tablet with nfc reader. The app works as time attendance so the users put the badge on the nfc reader to make attendances.

Sometime (can happen 2 times in a month) the nfc reader does not work. To restart wotking, close and reopen the app is enought. It is not necessary to restart the device.

No one error or excheption is fired. Can you check my source code in order to find an error, or a method not correctly implemented? Is there a time limit in order to listen the Android NFC events?

Best regards.

private final android.content.BroadcastReceiver nfcReceiverState= new android.content.BroadcastReceiver() {

@override

public void onReceive(android.content.Context context, android.content.Intent intent) {

if (intent.getAction().equals(android.nfc.NfcAdapter.ACTION_ADAPTER_STATE_CHANGED)) {

switch (intent.getIntExtra(android.nfc.NfcAdapter.EXTRA_ADAPTER_STATE, android.nfc.NfcAdapter.STATE_OFF)) {

case android.nfc.NfcAdapter.STATE_OFF: {

LastNFCState= "STATE_OFF";

OnNFCState(LastNFCState);

break;

}

case android.nfc.NfcAdapter.STATE_TURNING_OFF: {

LastNFCState= "STATE_TURNING_OFF";

OnNFCState(LastNFCState);

break;

}

case android.nfc.NfcAdapter.STATE_ON: {

LastNFCState= "STATE_ON";

OnNFCState(LastNFCState);

break;

}

case android.nfc.NfcAdapter.STATE_TURNING_ON: {

LastNFCState= "STATE_TURNING_ON";

OnNFCState(LastNFCState);

break;

}

}

}

}

};

@override

public void onCreate(android.os.Bundle savedState) {

super.onCreate(savedState);

NfcReader= android.nfc.NfcAdapter.getDefaultAdapter(this);

if (NfcReader!= null) {

pendingIntent= android.app.PendingIntent.getActivity(this, 0, new android.content.Intent(this, getClass()).addFlags(android.content.Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

Filters= new android.content.IntentFilter();

TechList= new String[][]{

new String[] {

android.nfc.tech.IsoDep.class.getName(),

android.nfc.tech.MifareClassic.class.getName(),

android.nfc.tech.MifareUltralight.class.getName(), android.nfc.tech.Ndef.class.getName(),

android.nfc.tech.NdefFormatable.class.getName(),

android.nfc.tech.NfcA.class.getName(),

android.nfc.tech.NfcB.class.getName(),

android.nfc.tech.NfcF.class.getName(),

android.nfc.tech.NfcV.class.getName()}

};

Filters.addAction(android.nfc.NfcAdapter.ACTION_NDEF_DISCOVERED);

Filters.addAction(android.nfc.NfcAdapter.ACTION_TAG_DISCOVERED);

Filters.addAction(android.nfc.NfcAdapter.ACTION_TECH_DISCOVERED);

}

}

@override

public void onNewIntent(android.content.Intent intent) {

setIntent(intent);

if (android.nfc.NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {

android.nfc.Tag tag= intent.getParcelableExtra(android.nfc.NfcAdapter.EXTRA_TAG);

byte[] id_tag= tag.getId();

LastBadge= "";

for (int count= 0; count< id_tag.length; count++) LastBadge+= String.format("%1$"+ "2"+ "s", Integer.toHexString(id_tag[count] & 0xff)).replace(' ', '0');

OnBadge(LastBadge);

}

}

@override

public void onPause() {

IsInPause= true;

System.out.println("onPause()1");

System.out.println("onPause()2");

System.out.println("onPause()3");

System.out.println("onPause()4");

System.out.println("onPause()5");

super.onPause();

if (NfcReader!= null) NfcReader.disableForegroundDispatch(this);

}

@override

protected void onResume() {

IsInPause= false;

System.out.println("onResume()1");

System.out.println("onResume()2");

System.out.println("onResume()3");

System.out.println("onResume()4");

System.out.println("onResume()5");

super.onResume();

if (NfcReader!= null) NfcReader.enableForegroundDispatch(this, pendingIntent, new android.content.IntentFilter[]{Filters}, TechList);

}