DEV Community

Cover image for Implementing NFC in Android with Kotlin : Like Magic, But with Phones!
shubhcode
shubhcode

Posted on

Implementing NFC in Android with Kotlin : Like Magic, But with Phones!

Hey there, fellow Android devs! Ever heard of NFC? No, it's not some secret society; its Near Field Communication – the tech that lets your phone have a chat with another phone or a fancy tag when they're like, "Hey, I'm close!" Think of it as your phone's way of saying, "High-five, buddy!" 🤝📱

NFC Unveiled: Beyond Tap-to-Pay:
So, NFC isn't just for tapping your phone to pay for stuff – it's like your phone's fancy way of doing secret handshakes with other devices. And guess what? You can make your Android app a part of this magic show!

Getting the NFC Party Started:
Ready to jump into the NFC pool? First things first, make sure your phone supports NFC, and ask for the permission nod in your AndroidManifest. Then, it's showtime – you've got two cool moves: reading and writing!

Reading NFC like a Pro:
Imagine you're making an event app, and you want folks to check-in with a tap. NFC's got your back! Here's a simple breakdown with some Dev Humor:

class EventCheckInActivity : AppCompatActivity() {

    private lateinit var nfcAdapter: NfcAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_check_in)

        nfcAdapter = NfcAdapter.getDefaultAdapter(this)
    }

    override fun onResume() {
        super.onResume()
        val pendingIntent = PendingIntent.getActivity(
            this, 0, Intent(this, javaClass).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0
        )
        nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null)
    }

    // Here comes the moment of truth!
    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        if (NfcAdapter.ACTION_NDEF_DISCOVERED == intent.action) {
            // Ta-da! Handle NFC data here
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

Writing NFC Tags:
Time to let your app write messages to NFC tags. Imagine you're making a travel app – users tap their phone, and boom, their flight info is written to a tag. Look, it's like coding your own magic wand!

class WriteNFCActivity : AppCompatActivity() {

    private lateinit var nfcAdapter: NfcAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_write_nfc)

        nfcAdapter = NfcAdapter.getDefaultAdapter(this)
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        if (NfcAdapter.ACTION_NDEF_DISCOVERED == intent.action) {
            val tag = intent.getParcelableExtra<Tag>(NfcAdapter.EXTRA_TAG)
            val message = "Hey, I'm the NFC master!".toByteArray()
            val ndef = Ndef.get(tag)

            ndef.connect()
            ndef.writeNdefMessage(NdefMessage(message))
            ndef.close()

            // Tag has been written. Bam!
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

So there you have it, code buddies! NFC isn't some mystical spell; it's your ticket to adding some real-world magic to your apps. Whether you're crafting event check-ins, sharing sweet content, or just having a tech chat with tags, NFC's got your back. It's like coding with a sprinkle of Harry Potter charm! 🪄🔮

Now go out there, tap into the NFC adventure, and let your Android app join the party. Happy tapping, fellow magicians! 🎩🌟🚀

Remember, this blog is just your starting point on the road to NFC awesomeness. Have fun, explore, and let your creativity run wild with NFC in your Android apps – it's all about making tech fun, isn't it? 🤖🎉

Top comments (0)