NFC and App Inventor What I understood and what I would like to do

Hi everybody,
I write this post because I hope to help people that like me use NFC, and find somebody that can help to solve some problems that I found.
Two months ago I started to work with NFC and the great App Inventor, unfortunately I saw that it wasn't possible to do a lot of things, so I decided, for the first time, to create my own extension and I started with Mifare Classic RFID Technology because I know something about it .NFC and Mifare UID Extension
After that some people asked me to add features about different RFID tags because they really need and they do not have in the actual Near Field component. So I started to study what can I do.
This is the results of my test, to enable your app to recognise the different kinds of RFID tecnology You have to add in the AndroidManifest.xml of your apk :


and in the xml folder of the apk you have to add the nfc_tech_list.xml here an example:
nfc_tech_list
as You understand the most important rows to add in the manifest is android.nfc.action.TECH_DISCOVERED when You approach a tag near your phone your app check the nfc_tech_list and if there is a match Your app can manage the data.
For example with my extension I was able to read the UID of Mifare Ultralight, NTAG213, NTAG215.
Nice, but really I don't like to add manually all this things to the apk file. I would like to do it directly to the extension!!
Well something it is possible but not everything ( my opinion, but I hope I am wrong) as I found in documentation App Inventor Annotations https://docs.google.com/document/d/1UsjVC6bjf4rKPLP6FULUPeB6C8O-A5GbpYjvvwqTYK8/edit#heading=h.jyvtlmbuvwhc
You can add Activities and Metadata in the extension as We need in the AndroidManifest.xml

and the extension is compiled successfully, but when You try to compile tha app that use the extension You have errors, and the problem (I suppose) is that You have not added the nfc_tech_list.xml in the correct folder, and actually I do not how to do automatically in the extension!!!!! (really need help for this)

Another important thing that I saw in my test (adding manually the things that I told above) is when You approach a Tag to the phone if You have others apps installed that use NFC, Android ask which app want to use.
This problem can be solved using the foreground dispatch, so your app opened has the priority to other app installed that handle the same intent:
https://developer.android.com/guide/topics/connectivity/nfc/advanced-nfc.html#foreground-dispatch

Now the problem (for me) is how to handle this on a extension, for example in the article above is written: 1. Add the following code in the onCreate() method of your activity
How can I add a onCreate() on a extension, is it possible? I did not find an example.
Is it the correct way to do it? or there is another approach.

Well I think I said all, this is what I was able to do and understand, I hope this is a way to improve the NFC support in App Inventor
Best Regards
Marco

4 Likes

About the problem to create the Foreground Dispatch and the onCreate(). I followed this advice https://github.com/mit-cml/appinventor-sources/issues/2165
and I used Initialize in the extension

public void Initialize() {
        
        nfcAdapter = NfcAdapter.getDefaultAdapter(activity);
        mPendingIntent = PendingIntent.getActivity(activity, 0, new Intent(activity, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        
        IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        IntentFilter ntech = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
        IntentFilter td = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
        mFilters = new IntentFilter[] {ntech,ndef,td};
        mTechLists  = new String[][] { new String[] { NfcA.class.getName(),MifareUltralight.class.getName(),NdefFormatable.class.getName()},
                                       new String[] { NfcA.class.getName(),MifareUltralight.class.getName(),Ndef.class.getName()}
                                     };
        
        appendLog("Intent Filter creato!"); 
                                     
  }

and in onResume i added:

@Override
  public void onResume() {
    Intent intent = activity.getIntent();
    Log.d(TAG, "Nearfield on onResume.  Intent is: " + intent);
    if (nfcAdapter != null) 
        {
            nfcAdapter.enableForegroundDispatch(activity, mPendingIntent, mFilters, mTechLists);
        }
  }

Works? Yes and No, the Initialize works as I want, the code is executed when I open the app ( I see this because I added a Log file to check), but the ForegroungDispatch doesn't work, I still have to choose which app to use when I approach the Tag (NTAG 213) to the phone.

If somebody can help, really appreciated
Best Regards
Marco

3 Likes

In recent weeks I have been looking for some help to try to implement an extension that could better cover the use of the NFC sensor with App Inventor. But unfortunately I see that there is no interest from the community so I cannot continue with this project.

2 Likes

I moved your thread into the #open-source-development category so probably someone there is able to answer your questions

Taifun

2 Likes

Hi Marco,
Have you found the answer to your NFC activation question? I am also trying to create an NFC app but the NFC Ball and Cup game (which has NFC) just does not work!

Hi @Lavanian_Dorairaj ,
sorry no, I did not find.
As You can see in the previous posts I did not receive answer about the problems found.
So I don't know if is not possible to do with App Inventor or simply I did not find the documentation that explain how to do
Best Regards
Marco