简体   繁体   English

比较从NFC标签读取的数据

[英]Comparing Data Read from NFC Tag

Hi I am reading data from a NFC Tag and trying to compare it with a String but the if loop is getting failed while comparison of string.My code for reading NFC data and comparing it with String is as. 嗨,我正在从NFC标记中读取数据并尝试将其与String进行比较,但是在比较string时if循环失败了。我读取NFC数据并将其与String进行比较的代码如下。 Thank You. 谢谢。

   Parcelable[] messages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
                //NdefMessage message = ndef.getNdefMessage();
                if (messages != null) {
                    NdefMessage[] ndefMessages = new NdefMessage[messages.length];
                    for (int i = 0; i < messages.length; i++) {
                        ndefMessages[i] = (NdefMessage) messages[i];
                    }
                NdefRecord record = ndefMessages[0].getRecords()[0];

                byte[] payload = record.getPayload();
                String text = new String(payload);
                txtRead.setText(text);


                                if(text.equalsIgnoreCase("silent")){
                    Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
                    AudioManager audiomanage = (AudioManager)getSystemService(Context.AUDIO_SERVICE);  
                    audiomanage.setRingerMode(AudioManager.RINGER_MODE_SILENT); 
                }

If the problem is only with if condition try this code 如果问题仅在于条件,请尝试以下代码

if(text.toLowerCase().contains("silent")){
...
...
}

An NDEF text record contains, before the actual text, information about the text's language and, more important, the text encoding (character set used). NDEF文本记录在实际文本之前包含有关文本语言以及更重要的是文本编码(使用的字符集)的信息。 You should inspect those bytes to know whether the actual text is encoded in UTF-8 or UTF-16 (and use that for converting the bytes to a String). 您应该检查这些字节,以了解实际文本是使用UTF-8还是UTF-16编码的(并将其转换为字符串)。

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

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