在Android HCE中,“选择辅助”命令并不总是被路由到HostApduService。

时间:2022-01-25 04:06:25

Problem statement

I'm trying to understand why the "Select AID" command is not always successfully routed to the HostApduService of my HCE enabled application.

我试图理解为什么“Select AID”命令并不总是成功地路由到我的HCE启用应用程序的HostApduService。

Sometimes, the "Select AID" is correctly routed to the HostApduService, as can be seen from the following logcat output:

有时,“Select AID”被正确路由到HostApduService,从以下logcat输出可以看到:

07-11 20:11:36.386: D/NxpNfcJni(1486): RoutingManager::stackCallback: event=0x18
07-11 20:11:36.396: D/HostEmulationManager(1486): notifyHostEmulationActivated
07-11 20:11:36.396: D/NxpNfcJni(1486): RoutingManager::stackCallback: event=0x17
07-11 20:11:36.396: D/NxpNfcJni(1486): RoutingManager::stackCallback: NFA_CE_DATA_EVT; stat=0x0; h=0x301; data len=18
07-11 20:11:36.396: D/HostEmulationManager(1486): notifyHostEmulationData
07-11 20:11:36.396: D/HostEmulationManager(1486): Binding to service ComponentInfo{email.HCE/email.HCE.MyHCEService}
07-11 20:11:36.416: D/HostEmulationManager(1486): Waiting for new service.
07-11 20:11:36.466: D/MYHCESERVICE(25504): APDU RECEIVED
07-11 20:11:36.466: D/HostEmulationManager(1486): Sending data
07-11 20:11:37.276: E/SMD(284): DCD ON
07-11 20:11:37.436: D/NxpNfcJni(1486): RoutingManager::stackCallback: event=0x19
07-11 20:11:37.436: D/NxpNfcJni(1486): RoutingManager::stackCallback: NFA_DEACTIVATED_EVT, NFA_CE_DEACTIVATED_EVT
07-11 20:11:37.446: D/HostEmulationManager(1486): notifyHostEmulationDeactivated
07-11 20:11:37.446: D/MYHCESERVICE(25504): DISCONNECT
07-11 20:11:37.446: D/HostEmulationManager(1486): Unbinding from service ComponentInfo{email.HCE/email.HCE.MyHCEService}
07-11 20:11:37.446: D/MYHCESERVICE(25504): Disconnect by DEACTIVATION_LINK_LOSS
07-11 20:11:37.466: E/NfcNfa(1486): UICC/ESE[0x402] is not activated

However, more frequently, the "Select AID" command does not seem to arrive at the HostApduService. Its even worse, as there seems to be no activity at all from NxpNfcJni in the LogCat output. I consider NxpNfcJni to be the entry point given the excerpt from the LogCat output of a successful attempt.

然而,更常见的情况是,“Select AID”命令似乎并没有到达HostApduService。更糟糕的是,在LogCat输出中,NxpNfcJni似乎没有任何活动。我认为NxpNfcJni是在成功尝试的LogCat输出中摘录的入口点。

It must be stated that the frequency of success is a lot lower then the frequency of failure.

必须指出的是,成功的频率要比失败的频率低得多。

Test Setup

Hereafter is a description of the HCE device and corresponding relevant code, followed by the description of the application that controls the reader.

下文是对HCE设备的描述和相应的相关代码,后面是控制阅读器的应用程序的描述。

1. HCE device

1。HCE设备

  • Hardware: Samsung Galaxy S5
  • 硬件:三星Galaxy S5
  • OS: Android 5.0
  • 操作系统:Android 5.0
  • Code for the HostApduService:
  • HostApduService代码:
public class MyHCEService extends HostApduService {
    @Override
    public byte[] processCommandApdu(byte[] apdu, Bundle extras) {
        Log.d("MYHCESERVICE", "APDU RECEIVED");
        return new byte[]{(byte)0x6F,(byte)0x25}; //returned byte array reduced for this example
    }

    @Override
    public void onDeactivated(int reason) {
        Log.d("MYHCESERVICE", "DISCONNECT");
        switch(reason) {
        case MyHCEService.DEACTIVATION_DESELECTED:
            Log.d("MYHCESERVICE", "Disconnect by DEACTIVATION_DESELECTED");
        case MyHCEService.DEACTIVATION_LINK_LOSS:
            Log.d("MYHCESERVICE", "Disconnect by DEACTIVATION_LINK_LOSS");
        }
    }    
}
  • Android Manifest:
  • Android清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="email.HCE"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="22" />

    <uses-permission android:name="android.permission.NFC" />

    <uses-feature android:name="android.hardware.nfc" android:required="true" />
    <uses-feature android:name="android.hardware.nfc.hce" android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".MyHCEService" 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>

        <activity
            android:name=".ListBluetoothDevicesActivity"
            android:label="@string/app_name" 
            android:screenOrientation="portrait"
            android:configChanges="screenSize|orientation|keyboardHidden" >
        </activity>

    </application>

</manifest>
  • AID registration file:
  • 援助登记文件:
<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="11223344556677889900112233"/>
    </aid-group>
</host-apdu-service>

2. Reader application

2。阅读器应用程序

  • Contactless reader: ASK RDR 417 http://ask-contactless.com/en-us/contactus/technicalsupport.aspx#gen4xx
  • 非接触阅读器:询问RDR 417 http://ask-contactless.com/en-us/contactus/technicalsupport.aspx#gen4xx。
  • C# based application
  • 基于c#的应用
  • using PCSC-sharp to interface the contactless reader.
  • 使用PCSC-sharp接口来连接非接触式读取器。
  • Select application command:
  • 选择应用程序命令:
byte[] CMD_SelectApp = { 
    0x00, //CLA 
    0xA4, //INS
    0x04, //P1
    0x00, //P2
    0x0D, //LC
    0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22, 0x33 //AID
};
  • Pseudo-code for connecting connecting to the reader and transmitting the select application command:
  • 用于连接到阅读器的伪代码,并传输select应用程序命令:
SCardContext _Context = new SCardContext();
_Context.Establish(SCardScope.System);
SCardReader _Reader = new SCardReader(_Context);
_Reader.Connect(SelectedReader, SCardShareMode.Direct, SCardProtocol.Any);
_Reader.Transmit(CMD_SelectApp, CMD_SelectApp.Length, RecvBuffer, ref ReceiveBufferLength);

Question

Does anyone see why the "Select AID" command is not always successful? Or someone that can give some indications to debug this one?

有人知道为什么“选择援助”命令并不总是成功吗?或者有人能给出一些提示来调试这个?

2 个解决方案

#1


1  

I would highly suggest using this open source project which will allow you to test AID selection by running this app on another Android phone. It has been very helpful for me in the past. There are preprogrammed AIDs in the app for payment apps, but you can manually enter a custom AID as well. This is easier for testing (in my opinion) vs. having to use the USB reader.

我强烈建议使用这个开源项目,它允许您通过在另一个Android手机上运行这个应用程序来测试辅助选择。在过去,它对我很有帮助。在应用程序中有预先编程的辅助程序,但是你也可以手动输入一个自定义的辅助工具。这对于测试(在我看来)和使用USB阅读器比较容易。

https://github.com/doc-rj/smartcard-reader

https://github.com/doc-rj/smartcard-reader

在Android HCE中,“选择辅助”命令并不总是被路由到HostApduService。

#2


1  

It seems that by selecting only 1 type of polled cards for the ASK RDR 417 through the 'ASK RDR 417 (PC/SC) Properties' (see image below), the HostApduService is responding correctly and as expected.

通过“ASK RDR 417 (PC/SC)属性”(请参见下图),通过只选择1种类型的轮询卡,HostApduService的响应是正确的和预期的。

I'm interested to know why, at first sight this seems to limit the reader capabilities.

我很想知道为什么,乍一看这似乎限制了读者的能力。

在Android HCE中,“选择辅助”命令并不总是被路由到HostApduService。

#1


1  

I would highly suggest using this open source project which will allow you to test AID selection by running this app on another Android phone. It has been very helpful for me in the past. There are preprogrammed AIDs in the app for payment apps, but you can manually enter a custom AID as well. This is easier for testing (in my opinion) vs. having to use the USB reader.

我强烈建议使用这个开源项目,它允许您通过在另一个Android手机上运行这个应用程序来测试辅助选择。在过去,它对我很有帮助。在应用程序中有预先编程的辅助程序,但是你也可以手动输入一个自定义的辅助工具。这对于测试(在我看来)和使用USB阅读器比较容易。

https://github.com/doc-rj/smartcard-reader

https://github.com/doc-rj/smartcard-reader

在Android HCE中,“选择辅助”命令并不总是被路由到HostApduService。

#2


1  

It seems that by selecting only 1 type of polled cards for the ASK RDR 417 through the 'ASK RDR 417 (PC/SC) Properties' (see image below), the HostApduService is responding correctly and as expected.

通过“ASK RDR 417 (PC/SC)属性”(请参见下图),通过只选择1种类型的轮询卡,HostApduService的响应是正确的和预期的。

I'm interested to know why, at first sight this seems to limit the reader capabilities.

我很想知道为什么,乍一看这似乎限制了读者的能力。

在Android HCE中,“选择辅助”命令并不总是被路由到HostApduService。