i am trying to write a URL into an ntag215 that has password protection but i am not being able to do so. This password protection has been set by me. This means i know PWD and PACK for the tag, and i have correctly overwriten them in the tag (together with AUTH0, AUTH_LIM and PROT). The thing is that the authentication of the tag is correct, this means that when i try to verify the PACK i receive with the PACK i have, both are the same. Here is an example of how i do this. The functions i use here are ok.
URLActivateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (myTag == null) {
Toast.makeText(context, "my tag not detected", Toast.LENGTH_SHORT).show();
Toast.makeText(context, Error_detected, Toast.LENGTH_LONG).show();
} else {
Ndef ndef_writable = Ndef.get(myTag);
NfcA nfcA = NfcA.get(myTag);
try {
nfcA.connect(); // Intenta conectar con la tag NFC
if (ndef_writable.isWritable()) {
// Obtener la contraseña en formato de bytes
byte[] passwordBytes = getPasswordBytes();
try {
// Enviar el comando PWD_AUTH al tag NFC
byte[] response = nfcA.transceive(new byte[] {
(byte) 0x1B, // PWD_AUTH
passwordBytes[0], passwordBytes[1], passwordBytes[2], passwordBytes[3]
});
if ((response != null) && (response.length >= 2)) {
// Obtener PACK para verificar la autenticidad del tag
byte[] pack = Arrays.copyOf(response, 2);
// Verificar PACK para confirmar que el tag es auténtico
if (verifyPack(pack)) {
nfcA.close();
// Autenticación exitosa, ahora puedes realizar la escritura en la tag
writeURLtoTag(myTag, createUriMessage(edit_url.getText().toString()));
Toast.makeText(context, Write_Succes, Toast.LENGTH_LONG).show();
//... rest of the code
However the issue comes in the function writeURLtoTag. This is the function:
public void writeURLtoTag(Tag tag, NdefMessage message) {
if (tag != null) {
try {
Ndef ndefTag = Ndef.get(tag);
if (ndefTag == null) {
// Let's try to format the Tag in NDEF
NdefFormatable nForm = NdefFormatable.get(tag);
if (nForm != null) {
// Formatear la etiqueta si es necesario
nForm.connect();
nForm.format(message);
nForm.close();
} else {
// Manejar el caso en que la etiqueta no sea compatible con NDEF ni NdefFormatable
Log.e("TagAuth", "Tag not compatible with NDEF");
Toast.makeText(context, "Tag not compatible with NDEF", Toast.LENGTH_SHORT).show();
}
} else {
// Verificar el contenido actual de la etiqueta antes de escribir
NdefMessage currentMessage = ndefTag.getCachedNdefMessage();
if (currentMessage != null) {
// Puedes imprimir o manejar el contenido actual de la etiqueta si es necesario
Log.d("TagAuth", "Current NDEF Message: " + currentMessage.toString());
}
// Continuar con la conexión y escritura en la etiqueta
ndefTag.connect();
try {
checkTagStatus(tag);
ndefTag.writeNdefMessage(message);
Toast.makeText(context, "Write Successful", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Log.e("TagAuth", "IOException during writeNdefMessage: " + e.getMessage());
Toast.makeText(context, "IOException during writeNdefMessage", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
ndefTag.close();
}
}
catch(Exception e) {
Log.e("TagAuth", "Error during write Ndef: " + e.toString());
Toast.makeText(context, "Writing Error", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
Everytime i try to do ndefTag.writeNdefMessage(message) i get an IO exception "IOException during writeNdefMessage: null". I don't understand this. Why, if i have been authenticated, i can't write a new URL to the tag? Can someone help me to solve this problem?
Thank you very much