You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android NFC HCE主机APDU服务:AID过滤器声明异常求助

Android NFC HCE支付应用AID识别失败问题解决

问题概述

开发基于Visa AID的Android NFC HCE支付应用时,系统启动阶段抛出Not adding <aid-group> with empty or invalid AIDs日志,虽将应用识别为可选首选支付应用,但APDU命令无法路由至应用,POS设备无法触发服务处理逻辑。运行环境为API 29-33(Android 10-13)的三星S9、A54设备。

用户配置代码

清单文件(Manifest)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

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

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

    <application
        android:allowBackup="true"
        android:label="@string/app_name"
        android:icon="@mipmap/ic_launcher"
        android:theme="@style/Theme.MyHCE"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:fullBackupContent="@xml/backup_rules"
        android:supportsRtl="true"
        tools:targetApi="31"
        android:dataExtractionRules="@xml/data_extraction_rules">

        <activity
            android:name=".views.MainActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:launchMode="singleInstance"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

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

主机APDU服务XML资源文件

<?xml version="1.0" encoding="utf-8"?>

<host-apdu-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:description="@string/hce_host_apdu_service_description"
    android:requireDeviceUnlock="false"
    android:apduServiceBanner="@raw/my_hce_banner">

    <aid-group android:description="@string/aid_group_description_payment"
        android:category="payment">
        <aid-filter android:name="325041592E5359532E4444463031"
            android:description="@string/aid_description_ppse"/>
        <aid-filter android:name="315041592E5359532E44444630"
            android:description="@string/aid_description_pse"/>
        <aid-filter android:name="A0000000031010"
            android:description="@string/aid_description_visa"/>
    </aid-group>

</host-apdu-service>

Host Apdu Service Java代码

public class MyHostApduService extends HostApduService {

    @Override public byte[] processCommandApdu(byte[] commandApdu, Bundle extras) {
        vibrate(); // personal function that makes the device vibrate
        logMessage("APDU command detected"); // function that logs a tagged message
        Toast.makeText(getApplicationContext(), "APDU detected", Toast.LENGTH_SHORT);
        // to be developped...
        return new byte[0];
    }

    @Override public void onDeactivated(int reason) {
        logMessage("desactivation");
        Toast.makeText(getApplicationContext(), "desactivation", Toast.LENGTH_SHORT);
    }
}

Gradle依赖配置

dependencies {
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'com.google.android.material:material:1.9.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

解决方案

1. 校验AID格式与资源完整性

  • AID格式验证:确保所有AID为偶数长度的十六进制字符串(0-9、A-F),你的配置中AID长度均合法,若存在小写字母建议统一改为大写,提升设备兼容性。
  • Banner资源检查:若@raw/my_hce_banner资源缺失,部分系统会拒绝加载整个AID组。可临时注释android:apduServiceBanner="@raw/my_hce_banner"字段测试,若问题消失则补充合法的Banner资源。

2. 适配Android 10+ HCE安全限制

  • 设置默认支付应用:仅设置为"可选首选"无法触发APDU路由,需在系统设置(连接->NFC和支付->默认支付应用)中手动将应用设为默认支付应用。
  • 使用正式签名打包:三星部分设备对Debug签名的HCE应用有路由限制,尝试用Release签名打包应用测试。

3. 修复HostApduService逻辑错误

  • Toast提示补充show():原代码中Toast.makeText未调用.show(),无法显示提示,需补充该方法。
  • 返回标准APDU响应:返回空字节数组不符合ISO 7816-4规范,POS设备会终止会话。修改为返回标准成功响应0x9000:
@Override public byte[] processCommandApdu(byte[] commandApdu, Bundle extras) {
    vibrate();
    logMessage("APDU command detected");
    Toast.makeText(getApplicationContext(), "APDU detected", Toast.LENGTH_SHORT).show();
    // 返回ISO 7816-4标准成功响应
    return new byte[]{(byte)0x90, (byte)0x00};
}

@Override public void onDeactivated(int reason) {
    logMessage("desactivation");
    Toast.makeText(getApplicationContext(), "desactivation", Toast.LENGTH_SHORT).show();
}

4. 验证系统NFC配置

  • 确保设备NFC功能已开启,且未启用其他优先级更高的支付服务(如Samsung Pay),部分设备需关闭第三方支付服务才能触发自定义HCE应用。

内容的提问来源于stack exchange,提问作者20101

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 04:16:03