I'm new to NFC Android and I was stuck for days trying to set a password for the NTAG216 Mifare Ultralight tag, I have a blank card and I'm trying to set it up and I assure you that there is no protection on the label but the command failed, I even did a search and found this link with examples but nothing worked for me, I still got the same error, I tried the tag on different devices but nothing, I really need help, if someone could help me please.
I specify that the writePage and readPages methods work correctly but it is with the transceive method works with the commands get_version and fast_read except when I try to define the password and the pack
package com.lancine.nfcapp;
import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.NfcA;
import android.os.Build;
import android.os.Bundle;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.ui.AppBarConfiguration;
import com.lancine.nfcapp.databinding.ActivityMainBinding;
import java.io.IOException;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity implements NfcAdapter.ReaderCallback {
private AppBarConfiguration appBarConfiguration;
private ActivityMainBinding binding;
NfcAdapter mAdapter;
PendingIntent pendingIntent;
Tag tag;
@SuppressLint("UnspecifiedImmutableFlag")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), PendingIntent.FLAG_MUTABLE);
} else {
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), PendingIntent.FLAG_ONE_SHOT);
}
mAdapter = NfcAdapter.getDefaultAdapter(this);
}
@Override
protected void onResume() {
super.onResume();
//mAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
if (mAdapter != null) {
Bundle options = new Bundle();
options.putInt(NfcAdapter.EXTRA_READER_PRESENCE_CHECK_DELAY, 250);
mAdapter.enableReaderMode(this, this, NfcAdapter.FLAG_READER_NFC_A | NfcAdapter.FLAG_READER_NFC_B | NfcAdapter.FLAG_READER_NFC_F | NfcAdapter.FLAG_READER_NFC_V | NfcAdapter.FLAG_READER_NFC_BARCODE | NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS, options);
}
}
@Override
public void onTagDiscovered(Tag tag) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createOneShot(150, 10));
} else {
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(200);
}
this.setPassword(tag);
}
@Override
protected void onPause() {
super.onPause();
if (mAdapter != null) {
mAdapter.disableReaderMode(this);
}
}
public void setPassword(Tag tag) {
(new Thread(() -> {
NfcA nfcA = null;
try {
nfcA = NfcA.get(tag);
if(nfcA != null) {
nfcA.connect();
byte[] data = nfcA.transceive(new byte[] {
(byte)0xA2, // WRITE
(byte)(229 & 0x0ff), // block address
(byte)0x34, (byte)0x36, (byte)0x37, (byte)0x32
});
System.out.println("Comment" + Arrays.toString(data));
nfcA.close();
}
} catch (IOException e) {
Log.e("TAG", "IOException", e);
e.printStackTrace();
}
})).start();
}
}
W/System.err: java.io.IOException: Transceive failed W/System.err: at android.nfc.TransceiveResult.getResponseOrThrow(TransceiveResult.java:52) W/System.err: at android.nfc.tech.BasicTagTechnology.transceive(BasicTagTechnology.java:151) W/System.err: at android.nfc.tech.MifareUltralight.transceive(MifareUltralight.java:215) W/System.err: at com.lancine.nfcapp.MainActivity.lambda$nfcCommand$0$com-lancine-nfcapp-MainActivity(MainActivity.java:153) W/System.err: at com.lancine.nfcapp.MainActivity$$ExternalSyntheticLambda0.run(D8$$SyntheticClass) W/System.err: at java.lang.Thread.run(Thread.java:761)
The information I get when I read the card with nxp's TagInfo app
Detailed protocol information:
ID: 04:DE:58:42:EC:64:80 ATQA: 0x4400 SAK: 0x00
# Memory content:
[00] * 04:DE:58 0A (UID0-UID2, BCC0)
[01] * 42:EC:64:80 (UID3-UID6)
[02] . 4A 48 00 00 (BCC1, INT, LOCK0-LOCK1)
[03] . E1:11:6D:00 (OTP0-OTP3)
[04] . 03 0B D1 01 |....|
[05] . 07 54 02 66 |.T.f|
[06] . 72 59 65 6F |rYeo|
[07] . 79 FE 00 00 |y...|
[08] . 32 22 2C 22 |2","|
[09] . 63 61 72 74 |cart|
[0A] . 5F 69 64 22 |_id"|
[0B] . 3A 22 31 30 |:"10|
[0C] . 30 30 30 30 |0000|
[0D] . 30 30 22 2C |00",|
[0E] . 22 65 74 61 |"eta|
[0F] . 62 5F 69 64 |b_id|
[10] . 22 3A 22 31 |":"1|
[11] . 30 30 30 30 |0000|
[12] . 30 30 22 2C |00",|
[13] . 22 65 74 61 |"eta|
[14] . 62 5F 6E 6F |b_no|
[15] . 6D 22 3A 22 |m":"|
[16] . 4C 59 43 45 |LYCE|
[E1] .r 00 00 00 00 |....|
[E2] .r 00 00 00 BD (LOCK2-LOCK4, CHK)
[E3] .r 04 00 00 E1 (CFG, MIRROR, AUTH0)
[E4] .r 00 05 -- -- (ACCESS)
[E5] +P XX XX XX XX (PWD0-PWD3)
[E6] +P XX XX -- -- (PACK0-PACK1)
*:locked & blocked, x:locked,
+:blocked, .:un(b)locked, ?:unknown
r:readable (write-protected),
p:password protected, -:write-only
P:password protected write-only