4

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?

user3431818
  • 51
  • 2
  • 3
  • Possible duplicate: please have a look at this thread : http://stackoverflow.com/questions/24143832/android-4-4-2-pn532-nfc-reader-and-nfc-card-emulation#24157624 – LaurentY Mar 18 '15 at 14:36

2 Answers2

10

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!

underwood
  • 199
  • 1
  • 4
7

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.

Michael Roland
  • 39,663
  • 10
  • 99
  • 206
  • thanks for your response, but i cant follow you. do u know any example code? – user3431818 Mar 18 '15 at 17:56
  • No, I don't have any example code at hand (at least not when it comes to HCE). I suggest that you start your research by searching for "ISO 7816-4" and "APDU" and by reading the NFC Forum Type 4 Tag Operation specification. – Michael Roland Mar 18 '15 at 18:20
  • Can you please explain why it cannot be used to "reliably" trigger an app or open url on a second device? – electronix384128 Apr 15 '20 at 22:21
  • @electronix384128 The problem here is that R/W mode and P2P mode run simultaneously (sequentially polling different P2P and R/W technologies). Thus the other device could **either** be activated in card emulation mode (seeing the NDEF Tag Application) **or** be activated in P2P mode (seeing the SNEP service, etc). I assume this issue is also the reason why Google is abandoning P2P mode now. – Michael Roland Apr 17 '20 at 07:40