I coded a fix tonight that solves my issue temporary.
You need a Samsung Knox licence to implement this fix (or to be root I guess).
The fix adds 2 to 4 seconds compared with my previous workflow to read and check a password against the card, which is quite significant. Thus it is only temporary. I will update with a better solution in time.
My code and Fidesmo's code both detect tag when already pressed against the phone for 4.4.4 but fails for 5.1.1. Funnily enough, if you lock/unlock the phone, the phone detects the tag and both applications (Fidesmo's and mine) receive the onTagDiscovered callback. That comes from Android "switching off" NFC when screen is off (security reasons I think). From this constatation, the fix is obvious :
Fix : stop and start NFC + set up a receiver to listen to the NFC turn on/off.
Do whatever your implementation was doing before.
IntentFilter filter = new IntentFilter("android.nfc.action.ADAPTER_STATE_CHANGED");
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(Start.this, "broadcast received : "+ NfcAdapter.getDefaultAdapter(Start.this).isEnabled(), Toast.LENGTH_SHORT).show();
// My code for communication with NFC Card :
nfcCardApi= new nfcCardApi(Start.this);
nfcCardApi2= new APICardNFC(nfcCardApi);
APICardNFC.initForNFC(Start.this, nfcTypeCard);
APICardNFC.startWaitingCard();
tvInfo.setText(getResources().getString(R.string.login_pass_card));
}
};
registerReceiver(receiver, filter);
// restart NFC to try to grab the tag :
disableEnableNFC();
and disableEnableNFC() :
protected void disableEnableNFC() {
DeviceSettingsPolicy mDeviceSettings = DeviceSettingsPolicy.getInstance(context);
try {
if ( mDeviceSettings.startNFC(false)) {
if (Params.tagFgDebug && fgDebugLocal){Log.i(Params.tagGen, tagLocal + "mDeviceSettings.startNFC(false) : true NFC disable " );};
} else {
if (Params.tagFgDebug && fgDebugLocal){Log.i(Params.tagGen, tagLocal + "mDeviceSettings.startNFC(false) : false NFC disable FAILED " );};
}
if ( mDeviceSettings.startNFC(true)) {
if (Params.tagFgDebug && fgDebugLocal){Log.i(Params.tagGen, tagLocal + "mDeviceSettings.startNFC(true) : true NFC enable " );};
} else {
if (Params.tagFgDebug && fgDebugLocal){Log.i(Params.tagGen, tagLocal + "mDeviceSettings.startNFC(true) : false NFC enable FAILED " );};
}
} catch (SecurityException e) {
new TePVException(tagLocal, "disableEnableNFC", "SecurityException: " + e.getLocalizedMessage());
}
}