onTagDiscovered() not called any more when nfc tag already there after updating from 4.4.4 to 5.1.1 Samsung

╄→гoц情女王★ 提交于 2019-12-13 19:35:28

问题


I had a code able to detect an already present NFC tag when I start my NFCadapter. Which is very nice since it means you don't need to move the tag to detect and read it :)

Since I update a Samsung phone to 5.1.1 form 4.4.4, this is not working any more. Moving the tag to close contact does fire onTagDiscovered() but it used to be fire instantly.

AFAIK, Google changes around NFC should not have impacted my workflow : http://developer.android.com/sdk/api_diff/21/changes.html displays only added methods. And changes from 21 to 22 does not seem to have impact nfc am I right ?

Here is my call :

this.nfcAdapter.enableReaderMode(this.activity, this, NfcAdapter.FLAG_READER_NFC_A, Bundle.EMPTY);

Any idea about why the behaviour is degraded ? Any hints to work toward ?

I plan on testing this to other devices in 5.1.1 to check if it is samsung related only or Lollipop based. Finding such devices might take some time.

Thanks.


回答1:


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());
    }

}


来源:https://stackoverflow.com/questions/35069797/ontagdiscovered-not-called-any-more-when-nfc-tag-already-there-after-updating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!