Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

NFC External record is returning in wrong format?

I've successfully written an external record to an NFC tag. When I use a 3rd party tag reader to evaluate the external record that was written, I see the appropriate value, which is a single, positive integer.

However, when I run my code (below) to see what the value of the payload (external record) is on the tag (using a Toast) in order to incorporate that value into an "if" statement, I get different values. So far, I've seen the following:

B@41fb4278 or B@41fb1190.

At this point, the value of the external record is just "2". How can I just return/write simply 2?

protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);


    if(intent.hasExtra(NfcAdapter.EXTRA_TAG))

    {
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        byte[] payload = "2".getBytes();  ///this is where the ID (payload) for the tag is assigned.

        NdefRecord[] ndefRecords = new NdefRecord[2];
        ndefRecords[0] = NdefRecord.createExternal("com.example.bmt_admin", "externaltype", payload);
        ndefRecords[1] = NdefRecord.createApplicationRecord("com.example.bmt_01");
        NdefMessage ndefMessage = new NdefMessage(ndefRecords);
        writeNdefMessage(tag, ndefMessage);

        Toast.makeText(this, "NFC Scan: " + payload, Toast.LENGTH_SHORT).show();
    }

}

Thanks for any help!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

payload is defined as byte[]. When you use payload in your toast() statment, you use it a pointer to that array. Therefore what you see is the address of the array. When you want to get a string representation of a byte[], you can use for example:

String s = new String(payload);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...