DEV Community

rfidcard
rfidcard

Posted on

Entertaining Applications of NFC

NFC technology isn't just for practical applications; it can also be a source of entertainment. One fun way to use NFC is by creating interactive games or experiences. For example, you can program NFC tags to trigger specific actions in a game, such as unlocking new levels or revealing hidden content.

Here's a simple code example demonstrating how to read an NFC tag using Android's NFC API:

`import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class NFCActivity extends AppCompatActivity {
private NfcAdapter nfcAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nfc);

    nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (nfcAdapter == null) {
        Toast.makeText(this, "NFC not supported", Toast.LENGTH_SHORT).show();
        finish();
    }
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        if (tag != null) {
            Ndef ndef = Ndef.get(tag);
            if (ndef != null) {
                // Read NDEF message from the tag
                // Process the data as needed
                Toast.makeText(this, "NFC tag detected", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

@Override
protected void onResume() {
    super.onResume();
    if (nfcAdapter != null) {
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),
                PendingIntent.FLAG_MUTABLE);
        nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
    }
}

@Override
protected void onPause() {
    super.onPause();
    if (nfcAdapter != null) {
        nfcAdapter.disableForegroundDispatch(this);
    }
}
Enter fullscreen mode Exit fullscreen mode

}
`

https://www.tjnfctag.com/why-do-i-keep-getting-an-nfc-tag-on-my-phone/

Top comments (0)