获得了NFC硬件ID在Android中 [英] Getting the NFC Hardware ID In Android

查看:182
本文介绍了获得了NFC硬件ID在Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的事情非常简单,但我不能完全工作,如果姜饼API中的方法是的令牌被扫描或ID上登的Nexus S.我要的是硬件能够做的就是设备的NFC芯片的唯一标识符,这样我就可以进行注册(例如,当设备被放弃了RFID阅读器,我可以将设备被放弃与帐户相关联)。这可能与当前的API可用的方法?

这件作品的code,看起来最有前途的(但我不能测试,因为我没有一个设备)

 字节[] TAGID = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
 

解决方案

TAGID被设置为一个字节数组。你需要分析该数组为十六进制的字符串。有很多方法可以做到这一点,但是这code将做到这一点,而不诉诸外部库,并可以很容易地看到发生了什么:

 字符串ByteArrayToHexString(byte []的inarray)
    {
    INT I,J,在;
    串[]己= {0,1,2,3,4,5,6,7,8,9,A,的B,C,D,E,F};
    串出=;

    为(J = 0; J< inarray.length ++ j)条
        {
        在=(INT)inarray [J]放大器; 0xFF的;
        I =(在>→4)及为0x0F;
        OUT + =(十六进制)[我]
        I =在和放大器;为0x0F;
        OUT + =(十六进制)[我]
        }
    返回了;
}
 

I want to do something fairly straightforward, but I can't quite work out if the method in the Gingerbread API is for the ID of the token being scanned or the hardware on-board the Nexus S. What I want to be able to do is get the unique identifier of the NFC chip of the device, so I can register it (eg. when the device is waived over an RFID reader, I can associate the device being waived with an account). Is this possible with the current API methods available?

The piece of code that looks most promising (but I can't test because I don't have a device) is

byte[] tagId = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);

解决方案

tagId is set to an array of bytes. You need to parse that array to a hex string. There's lots of ways to do that, but this code will do it without resorting to external libraries and it's easy to see what's going on:

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

这篇关于获得了NFC硬件ID在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆