简体   繁体   English

如果不遵守我的条件,如何停止读取 NFC?

[英]How to stop Reading NFC , if my condition is not respected?

I want to stop the NFC reading process and display a textView in case of the my if condition is not respected.如果不遵守我的 if 条件,我想停止 NFC 读取过程并显示 textView。 If I do not have the 'S', I want the process of reading to be stopped my current code doesn't stop the process.如果我没有“S”,我希望停止读取过程,我当前的代码不会停止该过程。

 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());
        }


        if (text.charAt(0)!='S'){
            Alerte.setVisibility(View.GONE);
        }
else{
            Alerte.setVisibility(View.VISIBLE);
        }

Your probably using the Foreground dispatch mechanism to receive the NFC data to your running app though you don't show this code.尽管您没有显示此代码,但您可能使用前台调度机制将 NFC 数据接收到正在运行的应用程序。

So to stop receiving NFC Intent messages just call disableForegroundDispatch on the nfcAdaptor https://developer.android.com/reference/android/nfc/NfcAdapter#disableForegroundDispatch(android.app.Activity)因此,要停止接收 NFC Intent 消息,只需在 nfcAdaptor https://developer.android.com/reference/android/nfc/NfcAdapter#disableForegroundDispatch(android.app.Activity)上调用disableForegroundDispatch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM