Unable to read NFC tags in Android using Kotlin For WearOs

674 Views Asked by At

am trying to build an Android app that can read the contents of NFC tags. I am using Kotlin and have followed the code example provided on the Android Developer website, but my app is not able to read any NFC tags.

Here is the code for my MainActivity:

package com.example.myapplicationxx

import NfcReader
import android.app.Activity
import android.content.ContentValues.TAG
import android.content.Intent
import android.nfc.NdefMessage
import android.nfc.NfcAdapter
import android.nfc.Tag
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.annotation.RequiresApi
import com.example.myapplicationxx.databinding.ActivityMainBinding

class MainActivity : Activity() {

    private lateinit var binding: ActivityMainBinding
    private var nfcAdapter: NfcAdapter? = null

    val callback = @RequiresApi(Build.VERSION_CODES.KITKAT)
    object : NfcAdapter.ReaderCallback {
        override fun onTagDiscovered(tag: Tag?) {
            Log.d("NFC" , "nfc is $TAG")
            // Implement your logic here when a tag is discovered
        }
    }

    @RequiresApi(Build.VERSION_CODES.KITKAT)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        val nfcAdapter = NfcAdapter.getDefaultAdapter(this)
        val isNfcSupported = nfcAdapter != null && nfcAdapter.isEnabled
        if(isNfcSupported){
            Toast.makeText(this,"is supported",Toast.LENGTH_LONG).show();
            nfcAdapter.enableReaderMode(this, callback, NfcAdapter.FLAG_READER_NFC_A or NfcAdapter.FLAG_READER_NFC_B or NfcAdapter.FLAG_READER_NFC_F or NfcAdapter.FLAG_READER_NFC_V or NfcAdapter.FLAG_READER_NFC_BARCODE, null)
        }else{
            Toast.makeText(this,"is not supported",Toast.LENGTH_LONG).show();

        }

    }


    @RequiresApi(Build.VERSION_CODES.KITKAT)
    override fun onResume() {
        super.onResume()
        try {
            val nfcReader = NfcReader()
            val isNfcSupported = nfcAdapter != null
            if (isNfcSupported){
                nfcAdapter?.enableReaderMode(this, callback, NfcAdapter.FLAG_READER_NFC_A or NfcAdapter.FLAG_READER_NFC_B or NfcAdapter.FLAG_READER_NFC_F or NfcAdapter.FLAG_READER_NFC_V or NfcAdapter.FLAG_READER_NFC_BARCODE, null)
            }
        } catch (e: Exception) {
            Log.e(TAG, "Error enabling NFC reader mode", e)
        }    }
    @RequiresApi(Build.VERSION_CODES.KITKAT)
    override fun onPause() {
        super.onPause()
//        nfcAdapter?.disableReaderMode(this)
    }
    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        Toast.makeText(this , "intent", Toast.LENGTH_LONG).show()

        if (NfcAdapter.ACTION_NDEF_DISCOVERED == intent.action) {
            val rawMessages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)

            if (rawMessages != null) {
                val messages = arrayOfNulls<NdefMessage>(rawMessages.size)

                for (i in rawMessages.indices) {
                    messages[i] = rawMessages[i] as NdefMessage
                }

                // Process the messages here
            }
        }
    }

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.NFC" />
    <uses-sdk android:minSdkVersion="10"/>
    <uses-feature android:name="android.hardware.nfc" android:required="true" />
    <uses-feature android:name="android.hardware.type.watch" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@android:style/Theme.DeviceDefault">
        <uses-library
            android:name="com.google.android.wearable"
            android:required="true" />

        <!--
               Set to true if your app is Standalone, that is, it does not require the handheld
               app to run.
        -->
        <meta-data
            android:name="com.google.android.wearable.standalone"
            android:value="true" />

        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.nfc.action.TECH_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

then i get a java.lang.UnsupportedOperationException at com.example.myapplicationxx.MainActivity.onCreate(MainActivity.kt:40)

in the documentation UnsupportedOperationException if FEATURE_NFC is unavailable.

but if i take everything out and just leave

@RequiresApi(Build.VERSION_CODES.KITKAT)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        val nfcAdapter = NfcAdapter.getDefaultAdapter(this)
        val isNfcSupported = nfcAdapter != null && nfcAdapter.isEnabled
        if(isNfcSupported){
            Toast.makeText(this,"is supported",Toast.LENGTH_LONG).show();
        }else{
            Toast.makeText(this,"is not supported",Toast.LENGTH_LONG).show();

        }
    }

then i get is supported on my watch if i activate the NFC funtionality and is not supported if i deactivate it

please i need help if anyone has any idea about how to fix this or read an nfc tag with a smartwatch please let me know

i tried using chat gpt but always get the same answer and tried the docs from wear os but did not really get a result

1

There are 1 best solutions below

3
Christian Agnoletto On

I made some other test and I saw that on Samsung watch 4 the reader is not available. Only the HCE is available. HCE let emulate a card. So Samsung Smartwatch 4 is able to emulate a Nfc card and not able to read a Nfc card.