NFC and Mifare UID

Hello everybody,
I read that actually the near field component only can read & write text tags only.
I would read the UID inside a Mifare classic Tag, I found this code


that could be right for this purpose.
My idea is create an extension where to add this code e check if works, or another solution could be add this code to the Near Field component,
but in both case I don't know how to do.
Could You help me?
Thank You
Best Regards
Marco

Maybe this extension can help you :

This extension only returns if nfc is present in the phone and if it is enabled. The author asks for returning the UID.

1 Like

Yes this extension do not return the UID, but it could be a good point to start to add the function I need, if only the author provide the source code

1 Like

Well in these two days I attempted to build my first extension I follow the tutorial and I was able to built successfully but the result is not as expected. ( No Uid when I approach a Mifare Classic Tag near the smartphone)
My approach is quiet simple I used the NearField.java from the latest App Inventor source and I changed the resolveIntent as follow:

void resolveIntent(Intent intent) {
Log.d(TAG, "resolve intent. Intent is: " + intent);
// Parse the intent
if(SdkLevel.getLevel() >= SdkLevel.LEVEL_GINGERBREAD){
//GingerbreadUtil.resolveNFCIntent(intent, this);

  if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
	  Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    byte[] idBytes = null;
    if (tag != null) {
        idBytes = tag.getId();
    } else {
        idBytes = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
    }
	if (idBytes != null) {
		tagContent = ByteArrayToHexString(idBytes);
	}
  }
  
  /*
	if (intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
			tagContent = ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID));
	}
  */
}

}
And then using then lastMessage property I thought to find in tagContent the Uid, but no is empty!

Reading GingerbreadUtil.java in the resolveNFCIntent I see there is:
Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); excatly what I need but is not used!

Another different problem is that if use Ai Companion i can download my app and make test but with the result I wrote above, instead if I try to create the apk i have a DX execution failed.

White flag for me, I surrender

Best Regards
Marco

hmm, so what you want is a block that can return the UID from Mifare

Yes Salman, the UID from Mifare is what I need.

I don't think I'm very far with my attempts, but I don't understand what's wrong

1 Like

This week, I read a lot of articles about NFC and Mifare to try to make my own extension, and I did some steps, for example I solved the problem about DX execution failed.( it was the folder and the name of the extension).
I understood that the app must have an IntentFilter in the Manifest with:
action android:name="android.nfc.action.TAG_DISCOVERED"/
category android:name="android.intent.category.DEFAULT"/
and at this point correctly a tag discovered to my app, but it is like this event open the app a second time.
Reading some articles I found that this a common problem connected to the onNewIntent.
Some peoples says that adding android:launchMode="singleTop" on the Manifest solve the problem but not for me obviously.
In the https://stackoverflow.com/questions/21575787/method-onnewintentintent-not-executing-in-nfc
They try to solve the problem using pendingIntent in onCreate, but I think cannot add onCreate in the extension (correct?)
Others says https://stackoverflow.com/questions/23446120/onnewintent-is-not-called to use

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
}
.
But in this case super and setIntent I have the error cannot find symbol.

I tried to solve by myself but i cant, some help is really appreciated.
Thank You 
Best Regards
Marco

I don't know what you are trying, don't you want to get the UID from mifare, and maybe we can solve your problem if you provide a good Get UID Code or Link to see the code

Hi, I upoladed the file of the extension that I am trying to do, I added some comments to explain what I am doing
Thank You
Best Regards
Marco
MifareUid.txt (11.4 KB)

I found this:


It is exactly what happens to me with TAG_DISCOVERED but adding android:launchMode="singleTop"
do not solve the problem

I found the problem!
It was easyer than I tought
First I wrote android:launchMode="singleTask" instead of "singleTop"
Second I added this command in the right place, before I added in the Manifest in the application section and didn't work, now I added in the activity section and it works perfectly.
Now the goal is to add this information not manually after created the apk, but add this directly in the extension.
Any help really appreciated
Thank You
Best Regards
Marco

the correct manifest

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.NFC"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application android:debuggable="false" android:icon="@mipmap/ic_launcher" android:label="NFC" android:name="com.google.appinventor.components.runtime.multidex.MultiDexApplication" android:networkSecurityConfig="@xml/network_security_config" android:requestLegacyExternalStorage="true" android:roundIcon="@mipmap/ic_launcher" android:theme="@style/AppTheme">
    <uses-library android:name="org.apache.http.legacy" android:required="false"/>
    <activity android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize" android:launchMode="singleTask" android:name=".Screen1" android:windowSoftInputMode="stateHidden">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
		<intent-filter>
            <action android:name="android.nfc.action.TAG_DISCOVERED"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>
    <provider android:authorities="appinventor.ai_mpbejo.NFC.provider" android:exported="false" android:grantUriPermissions="true" android:name="androidx.core.content.FileProvider">
        <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/>
    </provider>
</application>

4 posts were split to a new topic: NFC and Mifare UID Extension

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.