1

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" />
Bolt UIX
  • 5,988
  • 6
  • 31
  • 58
  • Does this answer your question? [MediaSessionCompat:Targeting S+ (version 31 and above) requires that one of FLAG\_IMMUTABLE or FLAG\_MUTABLE be specified when creating a PendingIntent](https://stackoverflow.com/questions/68473542/mediasessioncompattargeting-s-version-31-and-above-requires-that-one-of-flag) Looks like a bug that has been fixed, may be check logcat to get the full error message instead of the shortened toast. – Andrew Nov 20 '21 at 18:40
  • I tried, but log does not show full error message – Bolt UIX Nov 21 '21 at 03:25

1 Answers1

3

Toast issue fixed in android s+ emulator: i don't have real device to test

//nfc process start
        pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            PendingIntent.getActivity(this, 0, Intent(this, javaClass).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), PendingIntent.FLAG_MUTABLE)
        } else{
            PendingIntent.getActivity(this, 0, Intent(this, javaClass).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0)
        }
Bolt UIX
  • 5,988
  • 6
  • 31
  • 58