Comparing Data Read from NFC Tag

╄→尐↘猪︶ㄣ 提交于 2020-01-07 08:21:25

问题


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

回答1:


An NDEF text record contains, before the actual text, information about the text's language and, more important, the text encoding (character set used). 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).




回答2:


If the problem is only with if condition try this code

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


来源:https://stackoverflow.com/questions/12470530/comparing-data-read-from-nfc-tag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!