简体   繁体   English

读取NFC标签上的“有效载荷”数据?

[英]Read “payload” data on NFC tag?

I'm using the following code to “read” an nfc tag: 我正在使用以下代码“读取”nfc标记:

@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? 我收到一个错误,说它“无法解析方法getNdefMessageFromIntent”我是否应该使用另一种方法来“读取”标签上的有效负载? 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();
}

} }

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

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