dhouh548
dhouh548

Reputation: 31

NFC DeadObjectException when onResume is called

I developed an application Android that detect NFC card then it open IsoDep connection with it to send APDU and get response. When application detect the NFC card it open a work session, I created a thread that every 500 ms check the isoDep connection, if connection was lost application close the work session (stop pooling , delete card data ...).

My application is device owner and is locked and it contain only one activity that start a webview to turn html code for user interfaces.

In The MainActivity, I stop the pooling in the onPause() before the super.onPause() and I restart it in the onResume() after the super.onResume(). Also I always call NfcAdapter.enableReaderMode() in onResume() and NfcAdapter.disableReaderMode in onPause().

    //enable nfc dispatch
    Bundle options = new Bundle();              
    options.putInt(NfcAdapter.EXTRA_READER_PRESENCE_CHECK_DELAY, 50);               

    NfcManager manager = (NfcManager) activity.
            getApplicationContext().getSystemService(Context.NFC_SERVICE);
    if(manager == null) {
        return;
    }

    NfcAdapter nfcAdapter = manager.getDefaultAdapter();
    if(nfcAdapter == null) {
        return;
    }

    try {
        nfcAdapter.enableReaderMode(activity,
                new NfcAdapter.ReaderCallback() {
                    @Override
                    public void onTagDiscovered(final Tag tag) {
                        cardDetected(tag);
                    }
                },
                NfcAdapter.FLAG_READER_NFC_A | NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK |
                        NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS,
                options);
    } catch (Exception e) {
    }

    //disable nfc dispatch 
        NfcManager manager = (NfcManager)activity.getApplicationContext().getSystemService(Context.NFC_SERVICE);
    if(manager == null) {
        return;
    }

    NfcAdapter nfcAdapter = manager.getDefaultAdapter();
    if(nfcAdapter == null) {
        return;
    }

    try {
        nfcAdapter.disableReaderMode(activity);
    } catch (Exception e) {
    }

I use Samsung Xcover4 Android 8 to test the application. And my problem is sometime when the onResume is called I see in the logcat a DeadObjectException :

    E/NFC: NFC service dead - attempting to recover
    android.os.DeadObjectException
    at android.os.BinderProxy.transactNative(Native Method)
    at android.os.BinderProxy.transact(Binder.java:777)
    at android.nfc.INfcAdapter$Stub$Proxy.setAppCallback(INfcAdapter.java:1026)
    at      android.nfc.NfcActivityManager.requestNfcServiceCallback(NfcActivityMan     ager.java:494)
    at      android.nfc.NfcActivityManager.onActivityResumed(NfcActivityManager.java:674)
    at      android.app.Application.dispatchActivityResumed(Application.java:240)
    at android.app.Activity.onResume(Activity.java:1369)
    at      android.support.v4.app.FragmentActivity.onResume(FragmentActivity.java:514)
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1363)
    at android.app.Activity.performResume(Activity.java:7432)
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3780)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3845)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1773)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:7000)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)

When it happen card is never detected and I have to restart NFC or application to be able to detect card.

Upvotes: 3

Views: 723

Answers (0)

Related Questions