NDEF Message with HCE Android

那年仲夏 提交于 2019-12-18 09:34:25

问题


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?


回答1:


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!




回答2:


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.



来源:https://stackoverflow.com/questions/29122848/ndef-message-with-hce-android

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