在Android中从NFC读取ID

时间:2013-07-21 05:37:38

标签: android tags nfc

如何从Android中的NFC标签中读取ID?

2 个答案:

答案 0 :(得分:2)

这就是你想要的:

byte[] tagId = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
NdefMessage[] msgs = (NdefMessage[]) intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);

然后你必须从byte解析为String。

String ByteArrayToHexString(byte [] inarray) 
    {
    int i, j, in;
    String [] hex = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
    String out= "";

    for(j = 0 ; j < inarray.length ; ++j) 
        {
        in = (int) inarray[j] & 0xff;
        i = (in >> 4) & 0x0f;
        out += hex[i];
        i = in & 0x0f;
        out += hex[i];
        }
    return out;
}

答案 1 :(得分:1)

您可以关注主题:How to read from detected NFC tag (NDEF message). Android NFC 通过首先搜索NFC基础教程来获得想法可能会有所帮助。