Writing data into Nexus 4 in card emulation mode w

2019-02-21 06:10发布

问题:

I am trying to send some data to the Nexus 4 through NFC (i.e. the card emulation mode). I tried a number of the command APDUs such as writing and updating APDUs, but I couldn't get them to work.

What I am trying to say is, I want to send some data (that is not the AID) to the phone after the select APDU command.

Thanks in advance,

Bader

回答1:

The HCE emulated card will understand exactly those commands that your HCE app's APDU service processes. So, for instance, if your HCE service's processCommandApdu() callback method looks like this:

final static byte[] SW_NO_ERROR = new byte[]{ (byte)0x90, (byte)0x00 };
final static byte[] SW_INCORRECT_P1P2 = new byte[]{ (byte)0x6A, (byte)0x86 };
final static byte[] SW_INS_NOT_SUPPORTED = new byte[]{ (byte)0x6D, (byte)0x00 };
final static byte[] SW_ERR_UNKNOWN = new byte[]{ (byte)0x6F, (byte)0x00 };

@Override
public byte[] processCommandApdu(byte[] apdu, Bundle extras) {
    if (apdu.length >= 4) {
        if ((apdu[1] == (byte)0xA4) && (apdu[2] == (byte)0x04)) {
             // SELECT APPLICATION
             return SW_NO_ERROR;
        } else if ((apdu[1] == (byte)0xCA) && (apdu[2] == (byte)0x02)) {
             // GET DATA (SIMPLE TLV)
             switch (apdu[3] & 0x0FF) {
                 case 0x001:
                     return new byte[]{ apdu[3], (byte)0x02, (byte)0x01, (byte)0x00, (byte)0x90, (byte)0x00 };
                 case 0x002:
                     return new byte[]{ apdu[3], (byte)0x02, (byte)0x12, (byte)0x34, (byte)0x90, (byte)0x00 };
                 case 0x003:
                     return new byte[]{ apdu[3], (byte)0x06, (byte)0xAA, (byte)0xBB, (byte)0xCC, (byte)0xDD, (byte)0xEE, (byte)0xFF, (byte)0x90, (byte)0x00 };
                 default:
                     return SW_INCORRECT_P1P2;
             }
        } else {
            return SW_INS_NOT_SUPPORTED;
        }
    }

    return SW_ERR_UNKNOWN;
}

Your HCE app would understand the following command APDUs:

  • SELECT APPLICATION (by AID)

    00 A4 04 xx ...
    
  • GET DATA for data object 0201

    00 CA 02 01 00
    
  • GET DATA for data object 0202

    00 CA 02 02 00
    
  • GET DATA for data object 0203

    00 CA 02 03 00
    

Other commands will result in various errors.