Hello I would like to create app in Kotlin that can work with NFC on wear os. So far app can detect whether device has NFC or is it on/off but now I am stuck on trying to detect the tag and displaying either toast or something in logs that there was connection. I attached how the project is structured as well as Detect.kt (that I think should be named MainActivity.kt), AndroidManifest.xml and NfcReceiver.kt
package com.example.detectnfc
import android.app.PendingIntent
import android.content.Intent
import android.content.IntentFilter
import android.nfc.NfcAdapter
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class Detect : AppCompatActivity() {
private var nfcAdapter: NfcAdapter? = null
private var messageTextView: TextView? = null
private var pendingIntent: PendingIntent? = null
private val nfcReceiver = NfcReceiver()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_wear)
messageTextView = findViewById(R.id.messageTextView)
Log.d("create", "created")
nfcAdapter = NfcAdapter.getDefaultAdapter(this)
if (nfcAdapter == null) {
// NFC is not available for this device
Log.d("detect", "NFC not available")
Toast.makeText(
this, "NFC is not available on this device",
Toast.LENGTH_SHORT
).show()
finish()
} else if (!nfcAdapter!!.isEnabled) {
// NFC is available for the device but not enabled
Log.d("detect", "NFC not enabled")
Toast.makeText(
this, "Turn on NFC in your device settings",
Toast.LENGTH_SHORT
).show()
finish()
} else {
// NFC is enabled, register the BroadcastReceiver
Log.d("detect", "NFC enabled")
val intentFilter = IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED)
registerReceiver(nfcReceiver, intentFilter)
}
// Create an Intent for the activity you want to launch
val intent = Intent(this, this::class.java)
// Set the FLAG_ACTIVITY_SINGLE_TOP flag
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
// Create a PendingIntent
pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)
}
override fun onDestroy() {
super.onDestroy()
// Unregister the BroadcastReceiver when the activity is destroyed
unregisterReceiver(nfcReceiver)
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
if (intent != null && NfcAdapter.ACTION_TAG_DISCOVERED == intent.action) {
// NFC tag detected, show a toast
Log.d("detect", "Tag Detected")
Toast.makeText(
this, "Tag Detected",
Toast.LENGTH_SHORT
).show()
// You can add additional handling for the detected tag here if needed.
}
}
}
The NfcReceiver
package com.example.detectnfc
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.nfc.NfcAdapter
import android.util.Log
import android.widget.Toast
class NfcReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (intent?.action == NfcAdapter.ACTION_TAG_DISCOVERED) {
Log.d("NfcReceiver", "NFC tag detected")
// Handle NFC tag detection here
// You can perform actions or notify the Detect activity as needed
Toast.makeText(
context, "NFC Tag Detected",
Toast.LENGTH_SHORT
).show()
}
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.detectnfc">
<uses-feature android:name="android.hardware.type.watch" />
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@android:style/Theme.DeviceDefault">
<meta-data
android:name="com.google.android.wearable.standalone"
android:value="true" />
<activity
android:name="com.example.detectnfc.Detect"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:exported="true">
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED"/>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Any help would be appreciated!
I tried to search for information but there are not that many articles about NFC specifically on smartwatches