LLevo unos días intentando hacer un lector y escritor de tags nfc en Android Studio, he preguntado en otro momento pero no veo la pregunta asi que repito con algunos cambios. He estado siguiendo un tutorial para este nuevo codigo, pero no lo consigo hacer funcionar, ni en Fragments de mi app ni en un MainActivity normal. Lo raro es que ni lee ni escribe en la tarjeta como debería, pero si me salgo de la app y leo el tagNFC al salirme el aviso de android de leer la etiqueta me abre mi app con el texto de la etiqueta nfc. me deberia mostrar ‘write(“PlainText|” + edit_message.getText().toString(), myTag);’ pero me muestra ‘ nfc_contents.setText ( “Contenido de la tarjeta NFC: ” + text);
‘ ahora os enseño todo el código. Además, me muestra este error en el logcat cada vez que leo una tarjeta nfc 2024-05-04 20:09:41.404 2573-2702 ActivityStarterImpl system_server E Error: Activity class {com.alfonso.nfcplay/com.alfonso.nfcplay.ui.read.ReadFragment} does not exist, como si estuviese buscando un paquete dentro del mismo paquete.
Pero bueno, el código:
”’
`package com.alfonso.nfcplay.ui.read;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.NfcManager;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import com.alfonso.nfcplay.R;
import com.alfonso.nfcplay.databinding.FragmentReadBinding;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
public class ReadFragment extends Fragment {
public static final String ErrorDetected="No NFC Tag Detected";
public static final String WriteSuccess="Text Written Succesfully";
public static final String WriteError="Error during writing, Try again!";
PendingIntent pendingIntent;
IntentFilter[] writingTagFilters;
boolean writeMode;
Tag myTag;
TextView edit_message;
TextView nfc_contents;
Button ActivateButton;
NfcAdapter nfcAdapter;
FragmentReadBinding binding;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
binding = FragmentReadBinding.inflate(inflater, container, false);
View root = binding.getRoot();
edit_message= root.findViewById(R.id.edit_message);
nfc_contents= root.findViewById(R.id.nfc_contents);
ActivateButton = root.findViewById(R.id.ActivateButton);
ActivateButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
try {
readFromIntent(requireActivity().getIntent());
if(myTag == null){
Toast.makeText(requireContext(),ErrorDetected,Toast.LENGTH_SHORT).show();
} else {
write("PlainText|" + edit_message.getText().toString(), myTag);
Toast.makeText(requireContext(), WriteSuccess, Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
NfcManager nfcManager = (NfcManager) requireContext().getSystemService(Context.NFC_SERVICE);
nfcAdapter = nfcManager.getDefaultAdapter();
if (nfcAdapter != null) {
// Check NFC enabled
if (!nfcAdapter.isEnabled()) {
Toast.makeText(requireContext(), "Por favor, activa el NFC para leer tarjetas", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(requireContext(), "Este dispositivo no soporta NFC", Toast.LENGTH_SHORT).show();
}
readFromIntent(requireActivity().getIntent());
pendingIntent = PendingIntent.getActivity(requireContext(),0, new Intent(getActivity(), getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), PendingIntent.FLAG_IMMUTABLE);
IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
writingTagFilters = new IntentFilter[]{tagDetected};
ReadViewModel readViewModel = new ViewModelProvider(this).get(ReadViewModel.class);
final TextView textView = binding.Read;
readViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
return root;
}
public void readFromIntent(Intent intent){
String action = intent.getAction();
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action) || NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)){
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage[] msgs = null;
if (rawMsgs!= null){
msgs = new NdefMessage[rawMsgs.length];
for (int i=0; i<rawMsgs.length;i++){
msgs[i] = (NdefMessage) rawMsgs[i];
}
}
buildTagViews(msgs);
}
}
public void buildTagViews(NdefMessage[] msgs){
if(msgs == null || msgs.length == 0) return;
String text = "";
byte[] payload = msgs[0].getRecords()[0].getPayload();
String textEncoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTF-16";
int languageCodeLength = payload[0] & 51;
try {
text = new String(payload, languageCodeLength+1, payload.length - languageCodeLength - 1, textEncoding);
} catch (UnsupportedEncodingException e) {
Log.e("UnsupportedEncoding", e.toString());
}
nfc_contents.setText ( "Contenido de la tarjeta NFC: " + text);
}
private void write(String text, Tag tag){
try {
NdefRecord[] records = { createRecord(text) };
NdefMessage message = new NdefMessage(records);
Ndef ndef = Ndef.get(tag);
if (ndef != null) {
ndef.connect();
ndef.writeNdefMessage(message);
ndef.close();
Toast.makeText(requireContext(), WriteSuccess, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(requireContext(), "NFC no soportado", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(requireContext(), WriteError, Toast.LENGTH_SHORT).show();
Log.e("WriteError", Objects.requireNonNull(e.getMessage()));
}
}
private NdefRecord createRecord(String text){
String lang = "en";
byte[] textBytes = text.getBytes();
byte[] langBytes = text.getBytes(StandardCharsets.US_ASCII);
int langLength = langBytes.length;
int textLength = textBytes.length;
byte[] payload = new byte[1 + langLength + textLength];
payload[0] = (byte) langLength;
System.arraycopy(langBytes, 0, payload, 1, langLength);
System.arraycopy(textBytes,0, payload,1 + langLength, textLength);
NdefRecord recordNFC = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], payload);
return recordNFC;
}
public void onNewIntent(Intent intent) {
readFromIntent(intent);
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
myTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
}
}
@Override
public void onPause() {
super.onPause();
WriteModeOff();
}
@Override
public void onResume() {
super.onResume();
WriteModeOn();
}
private void WriteModeOn(){
writeMode = true;
nfcAdapter.enableForegroundDispatch(requireActivity(), pendingIntent, writingTagFilters, null);
}
private void WriteModeOff(){
writeMode = false;
nfcAdapter.disableForegroundDispatch(requireActivity());
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
}
`
Quiero que me lea las tarjetas, todas las opciones y resultados que me han salido hasta ahora los he puesto en la pregunta
Alfonso Sánchez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.