I’ve been trying for a few days to make a nfc tag reader and writer in Android Studio, I’ve asked in another time but I don’t see the question so I repeat with some changes. I’ve been following a tutorial for this new code, but I can’t get it to work, neither in Fragments of my app nor in a normal MainActivity. The strange thing is that neither reads nor writes to the card as it should, but if I leave the app and read the NFC tag when I get the android prompt to read the tag it opens my app with the text of the nfc tag. it should show me ‘write(“PlainText|” + edit_message.getText().toString(), myTag);’ but it shows me ‘ nfc_contents.setText (“NFC card content: ” + text);
‘ now I show you all the code. Also, it shows me this error in the logcat every time I read a nfc card 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, as if it was looking for a package inside the same package.
But well, the code:
”’
`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;
}
}
`
I want it to read the cards for me, all the options and results that have come up so far I have put them in the question
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.
1