Trương Văn Trọng Trương Văn Trọng - 4 months ago 55
Android Question

Card Emulation, RC522 and Nexus 4's NFC UID

I have 2 problems:

First, I want to emulation card and NDEF Message.
I implemented

using this code:

public class MyHostApduService extends HostApduService {
private int messageCount = 0;
public byte[] processCommandApdu(byte[] apdu, Bundle extras) {
String n1="Hello World";
byte[] response=n1.getBytes();
Log.d("HCEDEMO", "Application selected");
return response;

public void onDeactivated(int reason) {
Log.d("HCEDEMO", "Deactivated: " + reason);

When my phone emulates a tag, put it near NFC Reader RC522.
But these code not run. Is there a problem with AID?
I used AIDs:

  • F04E66E75C02D8

  • F0394148148100

  • D2760000850101

  • F222222222

Second, I want to know what is UID of Android Devices's NFC when it communicate with another devices. Nothing is found with Google :(

Could you help me?


The AIDs that you are using look okay (one should be enough though). If your HCE service is not triggered, you likely do not issue a SELECT (by AID) command on the other end. So without knowing what you do on the reader-side of the communication, it's pretty much impossible to diagnose what's going wrong there.

Note that your above code has nothing to do with NDEF messages. If you want to properly transfer NDEF messages over HCE, you would need to implement the NFC Forum's Type 4 Tag Operation specification in your HCE service. Moreover, you should avoid responding with arbitrary strings in your processCommandApdu() method. Instead you should implement a protocol based on ISO/IEC 7816-4 APDUs.

Regarding your second question, the Nexus 4 (like most other HCE-capabled devices) will typically use a randomly generated anti-collision identifier (UID). On most devices, this random ID is generated for every activation of the magnetic field of the reader device (or whenever the phone is brought into the range of a reader device). Some devices also generate a new ID whenever the screen is turned on.