简体   繁体   中英

NDEF Message with HCE Android

I wanna emulate a host card with the HCE feature from Android. For that I extend the service class HostApduService and overwrite following method:

 public byte[] processCommandApdu(byte[] commandApdu, Bundle extras) {
    if (Arrays.equals(SELECT_APDU, commandApdu)) {
        NdefMessage message = new NdefMessage(new NdefRecord  [] {NdefRecord.createTextRecord("en", "test"});
       return message.toByteArray();
    } else {
        return UNKNOWN_CMD_SW;
    }
}

With a second device its possible to receive data from the HCE Service. The problem is that I always receive "Type A" Tag, but I need a NDEF Message.

Can anybody help me?

For anyone who stuck on this issue, I have read NFCForum-TS-Type-4-Tag which proposed by @Michael Roland. The whole idea is correct. All you need is to simulate the process SEND and RECEIVED commands to convert the byte array to a NDEF message. I created two repositories, one conclude the whole package about converting string to a NDEF message and the other one is a iOS Reader NDEF TAG to verify whether Android HCE is correct or not.

So Good luck!

Emulating a tag that is detected as NDEF tag using Android HCE is not as simple as sending an NDEF message in response to a SELECT APDU. You would need to implement the NFC Forum Type 4 Tag Operation specification. You can get that specification from the NFC Forum website .

Basically you would need to register a HCE service for the AID D2760000850101 that implements a couple of APDU commands that the reader-side uses to access a Type 4 tag:

  • SELECT NDEF tag application

     00 A4 04 00 07 D2760000850101 [00] 
  • SELECT capability container

     00 A4 00 0C 02 E103 
  • SELECT NDEF data file

     00 A4 00 0C 02 xxyy 

    Where xxyy is the file ID of the NDEF data file as specified in the capability container.

  • READ BINARY (for reading data from capability container or NDEF data file, whichever is currently selected)

     00 B0 xx yy zz 

    Where xx yy is the offset to read at and zz is the number of bytes to read.

Important note: Be aware that such an NFC Forum Type 4 tag emulated by an Android device cannot be used to automatically trigger an app on a second Android device (at least not reliably?). Putting two Android devices together will usually result in them establishing a peer-to-peer link (even if Beam is turned off!). Only a foreground app on the second Android device could use the NFC Reader mode API to bypass Android Beam and reliably detect the emulated tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM