Reputation: 271
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);
}
Upvotes: 0
Views: 596
Reputation: 10228
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).
Upvotes: 0
Reputation: 768
If the problem is only with if condition try this code
if(text.toLowerCase().contains("silent")){
...
...
}
Upvotes: 1