What does registerActivity do?

Forum / MIFARE general topics and applications / What does registerActivity do?

  • 12. December 2016 at 22:28
    I am trying to figure out what does registerActivity do, but the extremely poor documentation doesn't help.

    I am having an issue in which one NFC-ready actvity (A) is being invoked while reading a NFC Tag in another NFC-ready activity (B). It seems that registerActivity only works once and registers the first activity which was used, and then keeps using the same one.

    What does registerActivity exactly do, is it possible to unregister an activity and finally does it make any sense to unregister an activity?

    Thank you for your time.
    + 0  |  - 0

    Re: What does registerActivity do?

    13. December 2016 at 13:21
    Hi Francisco,

    I assume you mean the method of the TapLinx Library. In this case you are in a wrong category. We have a separate forum for TapLinx users.

    Well, registerActivity() in TapLinx SDK means, that the activity should be able to receive NFC intents from the operating system. For receiving NFC intents you have to prepare somethings, which is embedded in the SDK. The first parameter is the package key, which you get from the NXP registry. There are no unregisterActivity(), so the app is registered for the life time of the app.

    Regards,
    The TapLinx Team

    + 0  |  - 0

    Re: What does registerActivity do?

    13. December 2016 at 14:28
    Thank you for your explanation but I already know that. As stated in the documentation the parameters are explained there. But what isn't explained is what does registerActivity really do. For example do I have to use it only once per lifetime of the application? Once for every activity? Also, I have uninstalled the application in which I use the SDK, installed it again fresh from scratch, and when I call isActivityRegistered() in the launcher activity, without having registered any other activity before, it tells me it is already registered. I repeated the procedure and this time without any Internet connection and the same result. Why is it still registered?

    This is causing me problems because another activity (e.g. activity A) is intercepting the NFC intents of another one (e.g. activity B). If I call registerActivity() everytime I start a NFC activity then I am getting errors related to "java.lang.illegalStateException: Foreground dispatch can only be enabled when your activity is resumed"
    + 0  |  - 0

    Re: What does registerActivity do?

    14. December 2016 at 9:29
    Hi Francisco,

    We have to be careful. The term “register activity” is also used in a different context in the Android framework. This method initialize the library instance and—if it never happened before, it connects to the NXP server one time. Here it is checked, if the package name of the running app is registered.

    This means, you can have more activities in your Android application, but the (TapLinx)registerActivity() must be called only once, for instance in your main activity. If you want to develop another app (with a different package name) you have to register this app also on our server and get a new key for this app.

    On word of which app takes NFC events. Your app is “registered” on your local Android system as NFC receiver. You see all “local NFC receivers” if you tap a card to your phone and only the home screen is visible. In this case Android ask you which app should be started. If your app is currently running, the method startForeGroundDispatch() is called and you signal to Android the willingness to take NFC intents. If your app is hided, you call stopForeGroundDispatch() and signal to Android, that no NFC intents can be handled in your app.

    Kind regards,
    The TapLinx Team
    + 0  |  - 0

    Re: What does registerActivity do?

    14. December 2016 at 14:29
    Thank you for the explanation, that was helpful. But unfortunately I tried what you said and placed the call to registerActivity() only on the onCreate() of my main activity, and the application crashes. But if instead I put it on the onResume() of every NFC activity, before calling startForeGroundDispatch() it works flawlessly. I suspect there might be a bug in which once the library is initialized it doesn't update its state to recognize other activities since invocations to startForeGroundDispatch() and stopForeGroundDispatch() always end in error.

    There's also a question regarding

    if it never happened before, it connects to the NXP server one time

    How can that be if I uninstalled the app completely, disabled any Internet connection in my device, then deployed from Android Studio the app again, and still isActivityRegistered() returns true?


    Regards.
    + 0  |  - 0

    Re: What does registerActivity do?

    14. December 2016 at 15:21
    Hi Francisco,

    Please have a look into one of the code snippets which comes with the application note. Please use the snippet below to implement the required methods. You will never need the method isApplicationRegistered(). The library instance should be used like the snippet below:

    public class MainActivity extends AppCompatActivity
    {
    // The package key you will get from the registration server
    private static String m_strPackageKey = "00112233445566778899aabbccddeeff";

    // The TapLinX library instance
    private NxpNfcLib m_libInstance = null;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // more code...

    initializeLibrary();
    }

    @Override
    protected void onResume()
    {
    m_libInstance.startForeGroundDispatch();
    super.onResume();
    }

    @Override
    protected void onPause()
    {
    m_libInstance.stopForeGroundDispatch();
    super.onPause();
    }

    private void initializeLibrary()
    {
    m_libInstance = NxpNfcLib.getInstance();
    m_libInstance.registerActivity(this, m_strPackageKey);
    }

    // ...
    }

    If you app still crashes, please send me a Logcat log file.

    Regards,
    The TapLinx Team
    + 0  |  - 0

    Re: What does registerActivity do?

    14. December 2016 at 15:36
    Yes, I followed the application note tutorial, and even downloaded the source code, but there there is only one activity, not multiple.

    Here is the error regarding the dispatch:


    --------- beginning of crash
    E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.SOMETHING.SOMETHING.debug, PID: 8608
    java.lang.RuntimeException: Unable to resume activity {com.SOMETHING.SOMETHING.debug/com.SOMETHING.SOMETHING.mypackagename.AnActivityThatIsntMainActivity}: java.lang.IllegalStateException: Foreground dispatch can only be enabled when your activity is resumed
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3160)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3191)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2529)
    at android.app.ActivityThread.access$900(ActivityThread.java:154)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:234)
    at android.app.ActivityThread.main(ActivityThread.java:5526)
    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)
    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(:4065)
    at com.SOMETHING.SOMETHING.mypackagename.NFCActivityUpInTheHierarchy.onResume(NFCActivityUpInTheHierarchy.kt:77)
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1259)
    at android.app.Activity.performResume(Activity.java:6361)
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3149)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3191) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2529) 
    at android.app.ActivityThread.access$900(ActivityThread.java:154) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:234) 
    at android.app.ActivityThread.main(ActivityThread.java:5526) 
    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)

    + 0  |  - 0

    Re: What does registerActivity do?

    15. December 2016 at 8:57
    Hi Francisco,

    It seems that your app registration is disordered (be careful, this has nothing to do with the registerActivity() method in TapLinx!). I am talking about activity life cycle states of Android which are reflect the calls onCreate(), onPause() on Resume() etc. It seems that you try to call startForeGroundDispatch() in any other method, but not in onResume(). Please take in mind that this methods can only be used in the main UI thread!

    I am quite sure that the issue has nothing to do with TapLinx. You should check out one of the examples and put the changes step by step into your app.

    Regards,
    The TapLinx Team
    + 0  |  - 0

    Re: What does registerActivity do?

    15. December 2016 at 14:57
    I am sorry but seems that we aren't on the same page, let me quote myself here:

    But if instead I put it on the onResume() of every NFC activity, before calling startForeGroundDispatch() it works flawlessly.


    Here I am refering to registerActivity().

    My calls to the method startForeGroundDispatch() are in the right place, as well as those for stopForeGroundDispatch(). Conversely, I am quite sure that it has something to do with the SDK since changing where I put the registerACtivity() determines whether it works or not. So to be clear:

    If I put registerActivity() after super.onResume() and before startForeGroundDispatch() then the code works.


    There's probably also a design flaw in Taplinx's SDK since taking into account what you said about registerActivity():

    Well, registerActivity() in TapLinx SDK means, that the activity should be able to receive NFC intents from the operating system


    Then one should invoke this method for every activity that needs to utilize NFC to be able to receive intents, since only invoking it once will cause that only the first registered activity receives intents. And since you said we should only call it once:

    This means, you can have more activities in your Android application, but the (TapLinx)registerActivity() must be called only once, for instance in your main activity.


    and the method belongs to a single instance (singleton of NxpNfcLib), then a global object (NxpNfcLib) is storing a local reference (the activity), for this reason I can surely tell you: that is a design flaw since it doesn't make any sense.

    Finally I am so sure that there's a problem (or at least it isn't well explained how to use the SDK) that I modified the demo application of the SDK found here (https://www.mifare.net/wp-content/uploads/2016/08/SrcsampleNxpNfcLib-1.0.zip ), added a new activity and as you'll experience it will crash with the same error.

    Here is the link: https://expirebox.com/download/4467168ea746a1f78f07a91742e3c15c.html

    Regards
    + 0  |  - 0

    Re: What does registerActivity do?

    15. December 2016 at 15:17
    Something happened with the links, please copy & paste them directly in the browser navigation bar. The link for the erro demo app expires in 48 hours.
    + 0  |  - 0
Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.