Good Day to everyone.
One problem regarding the above question
What can I do to detect tag without ‘false’?
All other function of the application seems to work just fine
‘Log.d(“YourTag”, “NFC tag detected: ” + tag.toString());’
Following the above log statement, the tag.toString always shows ‘False’
What seems to be the problem??
This is the onNewIntent
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.d("YourTag", "onNewIntent called");
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if (tag != null) {
processNfcTag(tag);
} else {
Log.e("YourTag", "Tag detection failed");
}
}
and this is the function for processNfcTag
private void processNfcTag(Tag tag) {
NFCTagFlow nfctag = new NFCTagFlow(this);
FileIO fileIO = new FileIO(this); // Instantiate FileIO
Log.d("YourTag", "Processing NFC tag");
int sectorOrPage = 4;
// RequestA Command
byte[] requestACommand = {(byte) 0x03, (byte) 0x26}; // Request Idle (Wake up one)
byte[] requestAResponse = nfctag.sendNFCCommand(tag, requestACommand);
if (requestAResponse[0] == 0x00) {
byte[] atqResponse = Arrays.copyOfRange(requestAResponse, 1, 3);
Log.d("YourTag", "RequestA Command successful. ATQ response: " + Arrays.toString(atqResponse));
} else {
nfctag.handleCommandError(requestAResponse[0]);
}
// Anticoll Command
byte[] anticollCommand = {(byte) 0x04}; // Anticollision command
byte[] anticollResponse = nfctag.sendNFCCommand(tag, anticollCommand);
byte[] uid = null;
if (anticollResponse[0] == 0x00) {
boolean multipleCards = anticollResponse[1] == 0x01;
uid = Arrays.copyOfRange(anticollResponse, 2, 6); // Extracting UID
String hexUID = nfctag.asciiToHex(new String(uid, StandardCharsets.US_ASCII));
Log.d("YourTag", "Anticoll Command successful. UID: " + hexUID);
} else {
nfctag.handleCommandError(anticollResponse[0]);
}
if (uid != null) {
// Select Command (with UID)
byte[] selectCommand = new byte[uid.length + 1];
selectCommand[0] = 0x05; // Select Command
System.arraycopy(uid, 0, selectCommand, 1, uid.length);
byte[] selectResponse = nfctag.sendNFCCommand(tag, selectCommand);
if (selectResponse[0] == 0x00) {
Log.d("YourTag", "Select Command successful. UID: " + Arrays.toString(uid));
} else {
nfctag.handleCommandError(selectResponse[0]);
}
} else {
Log.e("YourTag", "UID is null, Select Command cannot proceed");
}
// Inserted Halt Command
byte[] haltCommand = {(byte) 0x06}; // Halt command
byte[] haltResponse = nfctag.sendNFCCommand(tag, haltCommand);
if (haltResponse[0] == 0x00) {
Log.d("YourTag", "Halt Command successful");
} else {
nfctag.handleCommandError(haltResponse[0]);
}
// NFC_ActivateCard Command
byte[] activateCardCommand = new byte[]{/* Construct according to your parameters */};
byte[] activateCardResponse = nfctag.sendNFCCommand(tag, activateCardCommand);
if (activateCardResponse[0] == 0x00) {
Log.d("YourTag", "NFC_ActivateCard Command successful");
} else {
nfctag.handleCommandError(activateCardResponse[0]);
}
// NFC_Exchange Command
byte[] dataToSend = {/* Your data here */};
byte[] exchangeCommand = new byte[dataToSend.length + 2];
exchangeCommand[0] = (byte) 0xB5; // Exchange command code
exchangeCommand[1] = (byte) dataToSend.length;
System.arraycopy(dataToSend, 0, exchangeCommand, 2, dataToSend.length);
byte[] exchangeResponse = nfctag.sendNFCCommand(tag, exchangeCommand);
if (exchangeResponse[0] == 0x00) {
Log.d("YourTag", "NFC_Exchange Command successful. Data received: " + Arrays.toString(exchangeResponse));
} else {
nfctag.handleCommandError(exchangeResponse[0]);
}
// Send file to NFC tag
new Thread(() -> {
boolean sendResult = nfctag.sendFileToTag(tag, sectorOrPage);
if (sendResult) {
Log.d("YourTag", "File sent to tag successfully.");
} else {
Log.e("YourTag", "Failed to send file to tag.");
}
}).start();
// Handling NdefMessage if needed
final Ndef ndef = Ndef.get(tag);
final NdefMessage[] ndefMessage = {null};
if (ndef != null) {
try {
ndef.connect();
ndefMessage[0] = ndef.getNdefMessage();
ndef.close();
} catch (IOException | FormatException e) {
e.printStackTrace();
}
}
if (ndefMessage[0] != null) {
new Thread(() -> {
File directory = fileIO.createFile(MainActivity.this);
if (directory != null) {
fileIO.writeDataToFile(directory, ndefMessage[0]);
runOnUiThread(() -> {
textView3.setText("NFC Message Written to File");
});
} else {
Log.e("FileIO", "Directory is null, cannot write to file.");
}
}).start();
} else {
Log.e("YourTag", "NdefMessage is null");
}
}
}
1