Created
April 10, 2025 20:46
-
-
Save KristofferBerg/50d375e9f25e44d37eeaa80d630b28b7 to your computer and use it in GitHub Desktop.
MainActivity.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.myapplication | |
| import android.app.PendingIntent | |
| import android.content.Intent | |
| import android.content.IntentFilter | |
| import android.nfc.NfcAdapter | |
| import android.nfc.Tag | |
| import android.os.Bundle | |
| import android.util.Log | |
| import androidx.activity.ComponentActivity | |
| import androidx.activity.compose.setContent | |
| import androidx.activity.enableEdgeToEdge | |
| import androidx.compose.foundation.layout.fillMaxSize | |
| import androidx.compose.foundation.layout.padding | |
| import androidx.compose.material3.Scaffold | |
| import androidx.compose.material3.Text | |
| import androidx.compose.runtime.Composable | |
| import androidx.compose.ui.Modifier | |
| import androidx.compose.ui.tooling.preview.Preview | |
| import com.example.myapplication.ui.theme.MyApplicationTheme | |
| class MainActivity : ComponentActivity() { | |
| private var nfcAdapter: NfcAdapter? = null | |
| private lateinit var pendingIntent: PendingIntent | |
| private lateinit var intentFiltersArray: Array<IntentFilter> | |
| private lateinit var techListsArray: Array<Array<String>> | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| enableEdgeToEdge() | |
| // NFC setup | |
| nfcAdapter = NfcAdapter.getDefaultAdapter(this) | |
| if (nfcAdapter == null) { | |
| Log.e("NFC", "NFC not supported on this device") | |
| } else if (!nfcAdapter!!.isEnabled) { | |
| Log.e("NFC", "NFC is disabled") | |
| } | |
| // Create a PendingIntent to handle NFC tag discovery | |
| val intent = Intent(this, javaClass).apply { | |
| addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP) | |
| } | |
| pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_MUTABLE) | |
| // Create intent filters to handle NFC tag discovery | |
| // Change to ACTION_TAG_DISCOVERED to catch all tags | |
| val tagDetected = IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED) | |
| intentFiltersArray = arrayOf(tagDetected) | |
| // Setup tech lists to handle all tag technologies | |
| // We don't need to specify any tech lists for ACTION_TAG_DISCOVERED | |
| techListsArray = arrayOf() | |
| setContent { | |
| MyApplicationTheme { | |
| Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding -> | |
| Greeting( | |
| name = "Android", | |
| modifier = Modifier.padding(innerPadding) | |
| ) | |
| } | |
| } | |
| } | |
| } | |
| override fun onResume() { | |
| super.onResume() | |
| nfcAdapter?.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techListsArray) | |
| } | |
| override fun onPause() { | |
| super.onPause() | |
| nfcAdapter?.disableForegroundDispatch(this) | |
| } | |
| override fun onNewIntent(intent: Intent) { | |
| super.onNewIntent(intent) | |
| // Check for ACTION_TAG_DISCOVERED instead of ACTION_NDEF_DISCOVERED | |
| if (NfcAdapter.ACTION_TAG_DISCOVERED == intent.action) { | |
| val tag: Tag? = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG) | |
| if (tag != null) { | |
| val tagId = tag.id | |
| val tagIdString = tagId.joinToString(":") { String.format("%02X", it) } | |
| Log.d("NFC", "Tag discovered with ID: $tagIdString") | |
| } | |
| } | |
| } | |
| } | |
| @Composable | |
| fun Greeting(name: String, modifier: Modifier = Modifier) { | |
| Text( | |
| text = "Hello $name!", | |
| modifier = modifier | |
| ) | |
| } | |
| @Preview(showBackground = true) | |
| @Composable | |
| fun GreetingPreview() { | |
| MyApplicationTheme { | |
| Greeting("Android") | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment