I am trying to make my app pair with an NFC device...
# compose
e
I am trying to make my app pair with an NFC device. I have a button in my app that calls my activatePairing function, however I am getting a Background activity launch blocked error. I don’t really get this, since it’s not a background activity; I have the app open. I guess the problem is something to do with my pendingIntent? I’m honestly not sure, and this is my first time working with NFC so I’m a bit lost.
Copy code
class MainActivity : ComponentActivity() {
    onCreate{
    super.onCreate(savedInstanceState)
    //...

    adapter = NfcAdapter.getDefaultAdapter(this)
    val intent = Intent(this, javaClass).apply {
        addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
    }

    pendingIntent = PendingIntent.getActivity(
        this,
        0,
        intent,
        PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
    )
}

override fun onResume() {
    super.onResume()
    val intentFiltersArray = arrayOf(IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED).apply {
        addDataScheme("https")
        validDeviceURLs.forEach { addDataAuthority(it, null) }
    })
    adapter?.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, null)
}

override fun onNewIntent(intent: Intent) {
    super.onNewIntent(intent)
    nfcViewModel.handleNfcIntent(intent)
}
``````
I don't necessarily expect someone to be able to solve this since it requires a lot of context to the codebase that I can't expect someone to read all of. But if someone can tell me if the problem is here with the intents, or somewhere else in my app, that would really be appreciated.
c
Well, you Problem is also not related to Compose or Kotlin at all. You should ask in an Android forum. Check the channel description in #C0B8M7BUY for some links.
e
Why is it not related to Kotlin? Isn't it related to how i'm implementing the nfc activation in Kotlin? Sorry like I said I've never done this before so I'm a bit lost
c
Yeah, that’s the point - you have an issue with an Android API and here is a workspace by JetBrains not from Google.
e
Alright, sorry for the mistake and thanks for pointing me in the right direction