Forum / MIFARE SDK / Crash when executing startForeGroundDispatch
Tagged: android ev2 taplinx
-
Hi guys, I'm using a Nexus 7 Android 6.0.1 and TapLinx v1.1. From time to time the app crashes because it seems the enableForegroundDispatch method is invoked before the app is resumed. Below the code and the crash stack trace:
@Override
protected void onResume() {
super.onResume();
tapLinxLibInstance.startForeGroundDispatch(); // The stack trace points to this line (101)
}
Caused by: java.lang.IllegalStateException: Foreground dispatch can only be enabled when your activity is resumed
at android.nfc.NfcAdapter.enableForegroundDispatch(NfcAdapter.java:1200)
at com.nxp.nfclib.NxpNfcLib.startForeGroundDispatch(:5059)
at com.myapp.MainActivity.onResume(MainActivity.java:101)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1258)
at android.app.Activity.performResume(Activity.java:6327)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3092)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1388)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I do not understand why is crashing if I'm calling startForeGroundDispatch from onResume o_O
+ 1 | - 0
Hello Diego,
You cannot call startForeGroundDispatch() inside of onResume(). Please have a look to the Android life cycle states:
https://developer.android.com/guide/components/activities/activity-lifecycle.html
Regards,
The TapLinx team
+ 0 | - 0
Hi, thanks for your reply. What startForeGroundDispatch does internally is to call the NfcAdapter#enableForegroundDispatch method which according to the Android documentation must to be called from onResume ("...enableForegroundDispatch() must be called from the main thread and only when the activity is in the foreground (calling in onResume() guarantees this)") https://developer.android.com/guide/topics/connectivity/nfc/advanced-nfc.html#foreground-dispatch
On the other hand the sample application that you provide (https://www.mifare.net/wp-content/uploads/2016/08/SrcsampleNxpNfcLib-1.0.zip) calls startForeGroundDispatch from onResume. Where should I call startForeGroundDispatch method from?
Cheers.
+ 0 | - 0
Hi Diego,
Did you find a way to manage this problem?
Thanks,
+ 0 | - 0
Hi Diego,
Sorry, my mistake. enableForegroundDispatch() (which is used from TapLinx startForeGroundDispatch()) should be used in onResume(). The IllegalStateException is thrown if the app life cycle is already in the foreground.
The first question for me is: does a method in your app call onResume() directly? Usually this methods called by Android. Can you identify the current life cycle state in the logs?
The TapLinx team
+ 0 | - 0
I have run into the same problem when using TapLinx SDK 1.4.
With just one Activity everything works fine. However, when I transition between two Activities that both use startForeGroundDispatch in onResume and stopForeGroundDispatch in onPause the crashes happen with the the stack trace in the original post.
Edit: Found the cause: registerActivity needs to be called in onResume before the call to startForeGroundDispatch. I had it in onCreate before which caused TapLinx to use the wrong activity.
+ 0 | - 0
Hi Peter,
To drive a single resource (the NFC adapter) from two entities is always critical. I would drive the NFC adapter in a background thread and communicate to the background thread with Java synchronizing methods. Then it does not matter how many entities access the NFC interface. It is better to put all NFC tasks into a separate thread, because you have to deal with unpredictable situations, exceptions and time delays.
The TapLinx team
+ 1 | - 0
-
AuthorPosts
Viewing 7 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic.