I'm using the following code to “read” an nfc tag:
@Override
protected void onResume () {
super.onResume();
// read nfc tag....this is what will "read" the external record?
if (getIntent().hasExtra(NfcAdapter.EXTRA_TAG)) {
NdefMessage ndefMessage = this.getNdefMessageFromIntent(getIntent());
if(ndefMessage.getRecords().length > 0){
NdefRecord ndefRecord = ndefMessage.getRecords()[0];
String payload = new String(ndefRecord.getPayload());
Toast.makeText(this, payload, Toast.LENGTH_SHORT).show();
}
}
enableForegroundDispatchSystem();
}
I'm getting an error, saying that it “cannot resolve method getNdefMessageFromIntent” Is there another method that I should use to “read” the payload on the tag? Not sure how to fix this….Thanks for any help!!
You may try the following:
if (intent.getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMsgs != null) { // NDEF Message available
NdefMessage msg =(NdefMessage) rawMsgs[0];
NdefRecord ndefRecords = ndefMessage.getRecords();
String payload = new String(ndefRecord[0].getPayload());
Toast.makeText(this, payload, Toast.LENGTH_SHORT).show();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.