Created
November 9, 2017 13:56
-
-
Save ssaurel/05b1a7c06d1e832e3402cc37681cab82 to your computer and use it in GitHub Desktop.
resolveIntent method for the NFC Reader tutorial on the SSaurel's Channel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void onNewIntent(Intent intent) { | |
setIntent(intent); | |
resolveIntent(intent); | |
} | |
private void resolveIntent(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; | |
if (rawMsgs != null) { | |
msgs = new NdefMessage[rawMsgs.length]; | |
for (int i = 0; i < rawMsgs.length; i++) { | |
msgs[i] = (NdefMessage) rawMsgs[i]; | |
} | |
} else { | |
byte[] empty = new byte[0]; | |
byte[] id = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID); | |
Tag tag = (Tag) intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); | |
byte[] payload = dumpTagData(tag).getBytes(); | |
NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, id, payload); | |
NdefMessage msg = new NdefMessage(new NdefRecord[] {record}); | |
msgs = new NdefMessage[] {msg}; | |
} | |
displayMsgs(msgs); | |
} | |
} | |
private void displayMsgs(NdefMessage[] msgs) { | |
if (msgs == null || msgs.length == 0) | |
return; | |
StringBuilder builder = new StringBuilder(); | |
List<ParsedNdefRecord> records = NdefMessageParser.parse(msgs[0]); | |
final int size = records.size(); | |
for (int i = 0; i < size; i++) { | |
ParsedNdefRecord record = records.get(i); | |
String str = record.str(); | |
builder.append(str).append("\n"); | |
} | |
text.setText(builder.toString()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment