NFC External record is returning in wrong format?

萝らか妹 提交于 2019-12-13 08:53:16

问题


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!!


回答1:


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);


来源:https://stackoverflow.com/questions/39758748/nfc-external-record-is-returning-in-wrong-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!