package com.ramzey.nfcokuma2025;
import android.app.PendingIntent; import android.content.Intent; import android.content.IntentFilter; import android.nfc.NdefMessage; import android.nfc.NdefRecord; import android.nfc.NfcAdapter; import android.nfc.tech.IsoDep; import android.nfc.tech.MifareClassic; import android.nfc.tech.MifareUltralight; import android.nfc.tech.Ndef; import android.nfc.tech.NfcA; import android.nfc.tech.NfcB; import android.nfc.tech.NfcF; import android.nfc.tech.NfcV; import android.os.Build; import android.os.Bundle; import android.os.Parcelable; import android.util.Log; import android.widget.TextView; import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.io.UnsupportedEncodingException; import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
TextView txtNfc;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d("1111", "aaaa"); txtNfc = findViewById(R.id.txtNfc);
}
// list of NFC technologies detected: private final String[][] techList = new String[][]{ new String[]{ NfcA.class.getName(), NfcB.class.getName(), NfcF.class.getName(), NfcV.class.getName(), IsoDep.class.getName(), MifareClassic.class.getName(), MifareUltralight.class.getName(), Ndef.class.getName() } };
@Override protected void onResume() { super.onResume(); // creating pending intent:
PendingIntent pendingIntent; 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_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE); } else { pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags( Intent.FLAG_ACTIVITY_SINGLE_TOP), PendingIntent.FLAG_UPDATE_CURRENT); }
// creating intent receiver for NFC events: IntentFilter filter = new IntentFilter(); filter.addAction(NfcAdapter.ACTION_TAG_DISCOVERED); filter.addAction(NfcAdapter.ACTION_NDEF_DISCOVERED); filter.addAction(NfcAdapter.ACTION_TECH_DISCOVERED); // enabling foreground dispatch for getting intent from NFC event: NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); nfcAdapter.enableForegroundDispatch(this, pendingIntent, new IntentFilter[]{filter}, this.techList); }
@Override protected void onPause() { super.onPause(); // disabling foreground dispatch: NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); nfcAdapter.disableForegroundDispatch(this); }
@Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); Toast.makeText(this, "NFC OKUNDU!", Toast.LENGTH_SHORT).show(); NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); txtNfc.setText("EXTRA_ID : "+ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID))); // EXTRA_ID
Parcelable[] rawMessages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); NdefMessage[] messages = null; if (rawMessages != null) { messages = new NdefMessage[rawMessages.length]; for (int i = 0; i < rawMessages.length; i++) { messages[i] = (NdefMessage) rawMessages[i]; NdefRecord[] records = messages[i].getRecords(); //if you are sure you have text then you don't need to test TNF for(NdefRecord record: records){ processRecord(record); } } } }
public void processRecord(NdefRecord record) {
short tnf = record.getTnf(); switch (tnf) {
case NdefRecord.TNF_WELL_KNOWN: { if (Arrays.equals(record.getType(), NdefRecord.RTD_TEXT)) { String yourtext = processRtdTextRecord(record.getPayload()); txtNfc.setText(txtNfc.getText().toString()+"\nrecord : "+yourtext); } else if (Arrays.equals(record.getType(), NdefRecord.RTD_URI)) { String yourtext = record.toUri().toString(); txtNfc.setText(txtNfc.getText().toString()+"\nrecord : "+yourtext); } else if (Arrays.equals(record.getType(), NdefRecord.RTD_SMART_POSTER)) { // processSmartPosterRecord(record); } else { //Record is not Text or URI or Poster } } case NdefRecord.TNF_MIME_MEDIA: { if (record.toMimeType().equals("MIME/Type")) { // handle this as you want } else { //Record is not our MIME } } // you can write more cases default: { //unsupported NDEF Record } } }
private String processRtdTextRecord(byte[] payload) { String textEncoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTF-16"; int languageCodeLength = payload[0] & 0063;
String text = ""; try { text = new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding); } catch (UnsupportedEncodingException e) { e.printStackTrace(); Log.e("UnsupportedEncoding", e.toString()); } return text; }
private String ByteArrayToHexString(byte[] inarray) { int i, j, in; String[] hex = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}; String out = "";
for (j = 0; j < inarray.length; ++j) { in = (int) inarray[j] & 0xff; i = (in >> 4) & 0x0f; out += hex[i]; i = in & 0x0f; out += hex[i]; } return out; }
} |