I’m trying to programmatically disable NFC while a specific screen of my app is open, but it seems that the only way to do this is to use Intents. I’m searching for a programmatically way to achieve it.
Another solution would be to prevent other apps from intercepting NFC tags while my screen is in foreground.
I tried first to handle it by NfcAdapter
val nfcAdapter = NfcAdapter.getDefaultAdapter(context)
// Enable NFC if it's supported and disabled
if (nfcAdapter != null && !nfcAdapter.isEnabled) {
nfcAdapter.enable()
}
// Disable NFC if it's enabled
if (nfcAdapter != null && nfcAdapter.isEnabled) {
nfcAdapter.disable()
}
but methods enable()
and disable()
were removed in API level 29.
Than I tried with settings global
val contentResolver = context.contentResolver
// Enable NFC
Settings.Global.putInt(contentResolver, Settings.Global.NFC_ON, 1)
// Disable NFC
Settings.Global.putInt(contentResolver, Settings.Global.NFC_ON, 0)
but also this way was removed in API level 31.
I didn’t find anything else after the second solution other than using Intents.
Is there any other way to achieve this?