androidtagsnfcsystem-sounds

Android NFC: enable and disable the NFC detected sounds


I am dealing with NFC tags. My problem is that I cannot turn off the sound when NFC tags are detected. I started my research and also started getting confused:

Some say that we can and some that we can't disable those sounds.

Can we disable and enable the NFC sound programmatically?


Solution

  • Starting with API level 19 (Android 4.4) you can disable the NFC sounds while your app is in the foreground by using the newer reader-mode API to listen for NFC tags. The reader-mode API has a flag FLAG_READER_NO_PLATFORM_SOUNDS that can be used to disable the NFC discovery sounds.

    @Override
    protected void onResume() {
        super.onResume();
    
        NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
        adapter.enableReaderMode(this,
                new NfcAdapter.ReaderCallback() {
                    @Override
                    public void onTagDiscovered(final Tag tag) {
                        // do something
                    }
                },
                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_NO_PLATFORM_SOUNDS,
                null);
    }