Search code examples
javaandroidreact-nativenfc

API to disable NFC in Android app immediately


I am trying to determine how an Android app can stop the phone from broadcasting NFC signals at any particular time.

I know that this is possible because I am using an app where when a particular modal screen is opened NFC no longer functions. I know this is happening because if I hold up the phone to my NFC reader when the modal is open, nothing is recognised. As soon as I close the modal, the NFC reader instantly picks up the device.

I am unsure how to integrate this into my app. I have read other answers including this one which seem to state it's not possible, which can't be true because I'm seeing it happen before my eyes.

Is NfcAdapter.enableReaderMode what I am looking for? I am trying to determine if this will work but as I'm unsure how to implement it I can't test it.


Solution

  • I managed to disable NFC by using enableReaderMode as I originally thought. After adding the code below into my React Native application's overridden onCreate method, I was able to disable NFC throughout the app.

    import android.nfc.NfcAdapter;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
        adapter.enableReaderMode(this, null, NfcAdapter.STATE_OFF, null);
    }