Search code examples
androidnfc

Disable programmatically NFC while my app is open


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 Settings.Global.NFC_ON 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?


Solution

  • There is no way to programmatically disable via settings, you need to get the user to do this.

    You can silently intercept all NFC interactions and then do nothing with them, this to the user looks like NFC has been disabled.

    I do this for quite a few of my Activities with:-

    public class MainActivity extends AppCompatActivity implements NfcAdapter.ReaderCallback{
    
        private NfcAdapter mNfcAdapter;
    
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        }
    
    
        protected void onResume() {
            super.onResume();
    
            if(mNfcAdapter!= null) {
    
            // Request all Tag types are sent to this Activity
            // With Platform sounds off, so it's silent
                mNfcAdapter.enableReaderMode(this,
                        this,
                        NfcAdapter.FLAG_READER_NFC_A |
                                NfcAdapter.FLAG_READER_NFC_B |
                                NfcAdapter.FLAG_READER_NFC_F |
                                NfcAdapter.FLAG_READER_NFC_V |
                                NfcAdapter.FLAG_READER_NFC_BARCODE |
                                NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK |
                                NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS,
                        null);
            }
    
        }
    
        protected void onPause() {
            super.onPause();
            if(mNfcAdapter!= null)
                mNfcAdapter.disableReaderMode(this);
        }
    
        public void onTagDiscovered(Tag tag) {
        // Do nothing when a Tag is presented
            Log.v("MainActivity", "onTagDiscovered:Start");
        }
    }