I am using NFC in my main activity, android os 12 phones showing toast message like this "Targeting S+ (version 31 and above) requires that one of F..."
How to fix this issue.
Here is my sample code. Main activity:
import android.app.Dialog
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.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
///NFC
private var intentFiltersArray: Array<IntentFilter>? = null
private val nfcAdapter: NfcAdapter? by lazy {
NfcAdapter.getDefaultAdapter(this)
}
private var pendingIntent: PendingIntent? = null
//.......................................................................
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding= ActivityMainBinding.inflate(layoutInflater)
val mView = binding.root
setContentView(mView)
setSupportActionBar(binding.appBarMain.toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
////NFC config
try {
//nfc process start
pendingIntent = PendingIntent.getActivity(
this, 0, Intent(this, javaClass).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0
)
val ndef = IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED)
try {
ndef.addDataType("text/plain")
} catch (e: IntentFilter.MalformedMimeTypeException) {
throw RuntimeException("fail", e)
}
intentFiltersArray = arrayOf(ndef)
if (nfcAdapter == null) {
/* Toast.makeText(
applicationContext,
"This device doesn't support NFC.",
Toast.LENGTH_SHORT
).show()*/
} else if (!nfcAdapter!!.isEnabled) {
// code
}
}
catch (ex: java.lang.Exception)
{
Toast.makeText(applicationContext, ex.message, Toast.LENGTH_SHORT).show()
}
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
Log.d("jjnfc","nfc in")
NfcValidation().handleNFCIntent(intent!!,this,applicationContext)
}
//**************************************************************************************
override fun onResume() {
super.onResume()
try{
nfcAdapter?.enableForegroundDispatch(
this,
pendingIntent,
null,
null
)
Log.d("jjmain", "nfc onresume ")
}catch (e: Exception){
Log.d("jjmain", "nfc onresume error" + e.message)
}
}
override fun onPause() {
super.onPause()
try{
if (this.isFinishing) {
nfcAdapter?.disableForegroundDispatch(this)
}
Log.d("jjmain", "nfc onPause ")
}catch (e: Exception){
Log.d("jjmain", "nfc onPause error" + e.message)
}
}
}
Mainfeast
<uses-permission android:name="android.permission.NFC" />
<uses-feature
android:name="android.hardware.nfc"
android:required="false" />