使用 HCE 模拟公共交通卡 [英] Emulating a public transit card with HCE

查看:85
本文介绍了使用 HCE 模拟公共交通卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向用户显示公共交通卡(IsoDep、NfcB)的 ID,我成功地使用以下 指南.

现在我正在尝试使用以下 指南.

我使用以下代码创建了一个名为 MyHosApduService.java 的新类:

public class MyHostApduService 扩展 HostApduService {@覆盖public byte[] processCommandApdu(byte[] apdu, Bundle extras) {...}@覆盖公共无效 onDeactivated(int 原因){...}}

使用以下代码在 AndroidManifest.xml 中声明服务:

<service android:name=".MyHostApduService" android:exported="true"android:permission="android.permission.BIND_NFC_SERVICE"><意图过滤器><action android:name="android.nfc.cardemulation.action.HOST_APDU_SERVICE"/></意图过滤器><元数据android:name="android.nfc.cardemulation.host_apdu_service"android:resource="@xml/apduservice"/></服务>

apduservice.xml,代码如下:

<aid-group android:description="@string/aiddescription"机器人:类别=其他"><aid-filter android:name="0x00DDA611"/></援助小组></host-apdu-service>

从这里开始,如何将正确的数据发送到服务并正确触发服务?

解决方案

您在 AID 过滤器中选择并注册的 AID 无效.AID 值必须由没有0x"前缀的十六进制数字组成.此外,它必须由至少 5 个字节和最多 16 个字节组成.AID 的有效格式在 ISO/IEC 7816-4 中定义.请参阅此答案了解更多详情.

注意你通过的ID(防冲突标识符/标签标识符/UID等)

byte[] id = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);

byte[] id = tag.getId();

不是智能卡应用程序标识符 (AID).Android HCE 不允许您选择该 ID.相反,您只能模拟由 AID 标识的智能卡应用程序.这意味着智能卡读卡器将通过发出与您的应用程序名称(即 AID)匹配的 SELECT 命令来搜索您手机上的特定 HCE 应用程序.

因此,您首先需要为您的应用程序选择一个有效的 AID,例如F0112233445566.在阅读器端,您可以发送一个 SELECT APDU 来处理您的应用程序

<前>00 A4 04 00 07 F0112233445566 00

您可能还想阅读

I want to display to the user the ID of the public transit card (IsoDep, NfcB), what I managed to do successfully with the following guide.

Now I'm trying to emulate the card with Host Card Emulation with the following guide.

I made a new class named MyHosApduService.java with the following code:

public class MyHostApduService extends HostApduService {
    @Override
    public byte[] processCommandApdu(byte[] apdu, Bundle extras) {
       ...
    }
    @Override
    public void onDeactivated(int reason) {
       ...
    }
}   

Declared the service at the AndroidManifest.xml with the following code:

<service android:name=".MyHostApduService" android:exported="true"
            android:permission="android.permission.BIND_NFC_SERVICE">
            <intent-filter>
                <action android:name="android.nfc.cardemulation.action.HOST_APDU_SERVICE"/>
            </intent-filter>
            <meta-data android:name="android.nfc.cardemulation.host_apdu_service"
                android:resource="@xml/apduservice"/>
        </service>

And apduservice.xml with the following code:

<?xml version="1.0" encoding="utf-8"?>
<host-apdu-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:description="@string/servicedesc"
    android:requireDeviceUnlock="false">
    <aid-group android:description="@string/aiddescription"
        android:category="other">
        <aid-filter android:name="0x00DDA611"/>
    </aid-group>
</host-apdu-service>

From here, how do I send the correct data to the service and trigger the service correctly?

解决方案

The AID that you chose and registered in the AID filter is not valid. The AID value must consist of hexadecimal digits without a "0x" prefix. More over it must consist of at least 5 bytes and up to 16 bytes. Valid formats for AIDs are defined in ISO/IEC 7816-4. See this answer for further details.

Note that the ID (anticollision identifier / tag identifier / UID / etc.) that you get through

byte[] id = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);

or

byte[] id = tag.getId();

is not a smartcard application identifier (AID). Android HCE does not allow you to choose that ID. Instead, you can only emulate smartcard applications that are identified by an AID. This means that the smartcard reader would search for a specific HCE application on your phone by issuing a SELECT command matching the name (i.e. the AID) of your application.

Therefore, you first need to chose a valid AID for your application, e.g. F0112233445566. On the reader side, you can then send a SELECT APDU for addressing your application

00 A4 04 00  07  F0112233445566  00

You might also want to read

这篇关于使用 HCE 模拟公共交通卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆