androidtagsnfcsystem-sounds

How to turn off NFC sound


I have a problem with NFC. Is there any posible way to turn off the NFC sound in Android API 16 and higher?

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_traveller_read_tag);
    context = this;

    tvNFCContent = findViewById(R.id.nfc_contents);

    nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (nfcAdapter == null) {
        // Stop here, we definitely need NFC
        Toast.makeText(this, "This device doesn't support NFC.", Toast.LENGTH_LONG).show();
        finish();
    }
    readFromIntent(getIntent());

    pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
    writeTagFilters = new IntentFilter[]{tagDetected};

    findViewById(R.id.menu).setVisibility(View.GONE);
}

private void readFromIntent(Intent intent) {
    String action = intent.getAction();
    if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
            || NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
            || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        NdefMessage[] msgs = null;
        if (rawMsgs != null) {
            msgs = new NdefMessage[rawMsgs.length];
            for (int i = 0; i < rawMsgs.length; i++) {
                msgs[i] = (NdefMessage) rawMsgs[i];
            }
        }
        buildTagViews(msgs);
    }
}

private void buildTagViews(NdefMessage[] msgs) {
    if (msgs == null || msgs.length == 0) return;

    String text = "";
    //String tagId = new String(msgs[0].getRecords()[0].getType());
    byte[] payload = msgs[0].getRecords()[0].getPayload();
    String textEncoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTF-16"; // Get the Text Encoding
    int languageCodeLength = payload[0] & 0063; // Get the Language Code, e.g. "en"
    // String languageCode = new String(payload, 1, languageCodeLength, "US-ASCII");

    try {
        // Get the Text
        text = new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding);
    } catch (UnsupportedEncodingException e) {
        Log.e("UnsupportedEncoding", e.toString());
    }

    int tempCoin = Integer.parseInt( DB.getValue(context, UserValue.KEY_COIN) ) + Integer.parseInt( text );
    DB.setValue(context,UserValue.KEY_COIN,String.valueOf(tempCoin));
    tvNFCContent.setText("حساب شما " + text +" تومان شارژ شد.");

    beep.playBeep(context, beep.TypeBeep.OK);

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            finish();
        }
    },1000);
}

@Override
protected void onNewIntent(Intent intent) {
    setIntent(intent);
    readFromIntent(intent);
    if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
        myTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    }
}

Solution

  • No, disabling the NFC discovery sound is not possible on Android API level 16. Only the reader-mode API introduced in Android API level 19 allows you to disable the platform sounds normally generated for NFC discovery events:. See NfcAdapter.enableReaderMode and the flag NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS.