DEV Community

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

Posted on

17

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? ๐Ÿค–๐ŸŽ‰

Sentry mobile image

Tired of users complaining about slow app loading and janky UI?

Improve performance with key strategies like TTID/TTFD & app start analysis.

Read the blog post

Top comments (0)

Sentry growth stunted Image

If you are wasting time trying to track down the cause of a crash, itโ€™s time for a better solution. Get your crash rates to zero (or close to zero as possible) with less time and effort.

Try Sentry for more visibility into crashes, better workflow tools, and customizable alerts and reporting.

Switch Tools