How can I read the NFC ID of another Android device?

巧了我就是萌 提交于 2019-12-12 09:19:00

问题


I want to exchange (or only read) the NFC tag ID from one Android device to another but I don't know if I should use peer-to-peer mode or emulate an NFC tag with HCE.

If I use HCE, is the emulated tag ID unique?

What is the better option or is there a simpler one?


回答1:


Neither P2P nor HCE will provide you a unique ID, least not on any phone I'm aware of. With P2P it's required that the ID exchanged in ATR is random. With HCE the emulated tag ID is usually set to 08h plus a random number. There may be API call to set but I'm not aware of such. But it makes a lot of sense that a phone can not be uniquely identified by just anyone reading.




回答2:


How can I read the NFC ID of another Android device?

I assume you are talking about the anti-collision identifier/UID here. On the reading Android device, you can use the reader-mode API to access devices in HCE mode:

nfcAdapter.enableReaderMode(this, new NfcAdapter.ReaderCallback() {
    public void onTagDiscovered (Tag tag) {
        byte[] uid = tag.getId();
        // TODO: do something with the UID ...
    }
}, NfcAdapter.FLAG_READER_NFC_A | NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK, null);

Is the emulated tag ID unique?

See Stephen's answer. Typically, neither P2P mode nor HCE should provide a unique and stable ID. However, there are some exceptions to this and the point of time when a non-stable ID changes may vary (also see this answer). It could be that:

  • The device has a secure element and uses the static UID of that secure element.
  • The device generates a new random UID whenever it is turned on, but continues to use the same UID until it is powered off.
  • The device generates a new random UID on every activation by an external reader device. I.e. whenever an external HF field is applied to the NFC antenna of the Android device.

Also note that some NFC devices will use randomly generated UIDs that do not start with the random-prefix 0x08.

Can I force a device to use a fixed/stable UID?

Android does not provide an API to influence the UID/anti-collision identifier, so the short answer is no. However, if rooting or creating a custom ROM is an option, there are some possibilities to change the UID and other protocol parameters:

  • Editing Functionality of Host Card Emulation in Android
  • Host-based Card Emulation with Fixed Card ID

Should you use the UID to identify (or even authenticate) a device?

No, you should definitely not do this. While you sometimes do not have much choice when you try to integrate a HCE device into some legacy system, you should definitely think about a different design.

First of all, as Stephen already wrote, if an NFC device exposes a fixed/stable identifier, this could possibly introduce a privacy issue as anyone could read and track the ID.

Second, while many systems still use these IDs to authenticate(!) cards, UIDs are not necessarily unique (particularly there are less 4-byte UIDs than cards out there) and UIDs can easily be cloned. See this answer for further details.



来源:https://stackoverflow.com/questions/34413567/how-can-i-read-the-nfc-id-of-another-android-device

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