Chinmay Pingale
Chinmay Pingale

Reputation: 87

NFC service dead android.os.DeadObjectException

We are working on NFC tag android app and it is working properly 50% of times, and rest of the times we get NFC service dead exception in below try blocks:

Scenario is we connect to nfc Tag first time and send data, it always works fine. Then we don't move phone away and after 1-2 seconds we again tries to connect to nfc tag(if not connected) to send data and 30% of times we get NFC service dead exception and it doesn't start until we disable and enable mobile's NFC.

Process.setThreadPriority(Process.THREAD_PRIORITY_DISPLAY 
                          + Process.THREAD_PRIORITY_MORE_FAVORABLE);                 //first line of doInBackground()
        if(((ArrayList<Byte>) params[0]).size()<=0){
            return "No image selected!";
        }
        NfcA nfcaTag = (NfcA) params[2];
        try{
                    nfcaTag = NfcA.get(tag);
                    if (!nfcaTag.isConnected()){
                        nfcaTag.connect();        //this is line 175 MainActivity
                    }

        try {
                    nfcaTag.close();
                }

Exception:

NFC service dead
                                                              android.os.DeadObjectException
                                                                  at android.os.BinderProxy.transactNative(Native Method)
                                                                  at android.os.BinderProxy.transact(Binder.java:622)
                                                                  at android.nfc.INfcTag$Stub$Proxy.connect(INfcTag.java:285)
                                                                  at android.nfc.tech.BasicTagTechnology.connect(BasicTagTechnology.java:73)
                                                                  at android.nfc.tech.NfcA.connect(NfcA.java)
                                                                  at de.silab.nfc.vistagnfcappv1.MainActivity$2.onClick(MainActivity.java:175)
                                                                  at android.view.View.performClick(View.java:5723)
                                                                  at android.view.View$PerformClick.run(View.java:22689)
                                                                  at android.os.Handler.handleCallback(Handler.java:836)
                                                                  at android.os.Handler.dispatchMessage(Handler.java:103)
                                                                  at android.os.Looper.loop(Looper.java:203)
                                                                  at android.app.ActivityThread.main(ActivityThread.java:6361)
                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)

Is there any way to solve this issue. We are stuck here since long time. And any way to restart NFCAdapter/ nfc service by catching this exception?

Upvotes: 3

Views: 1221

Answers (1)

Randall Arms
Randall Arms

Reputation: 516

Your nfcaTag's hosting process is dead. It has been close()'d. Your first step should be to debug by logging info to the console about your variables to see if something is unusual.

I find it strange that you close() your nfcaTag immediately after connect()'ing it. Maybe these two statements conflict?

Also, consider why you have initialized the nfcaTag only to change it immediately in the next try statement.

Is your NfcA.get(tag) occasionally returning a null NfcA object? Is tag occasionally null? You need to test every variable, especially the ones you haven't shared here completely in your question.

Upvotes: 2

Related Questions