Android HCE Service未被识别,执行返回6F00错误求助
开发HCE应用时持续收到6F00响应问题排查
开发一个简单HCE应用时,执行后始终得到6F00响应,推测是HCE服务未注册,但找不到问题原因。(注:使用前台服务测试,前台服务相关问题与此无关)
dumpsys nfc输出
mState=on mIsZeroClickRequested=false mScreenState=ON_UNLOCKED mTechMask: default mEnableLPD: true mEnableReader: false mEnableHostRouting: true mEnableP2p: false Registered HCE services for current user: ComponentInfo{com.google.android.gms/com.google.android.gms.nearby.mediums.nearfieldcommunication.NfcAdvertisingService} (Description: Nearby) On Host Service Static AID groups: Category: other AID: F00000FE2C Dynamic AID groups: Settings Activity: null Routing Destination: host Service State: ENABLING ComponentInfo{com.google.android.gms/com.google.android.gms.tapandpay.hce.service.TpHceService} (Description: Google Pay) On Host Service Static AID groups: Category: payment AID: 325041592E5359532E4444463031 Dynamic AID groups: Settings Activity: null Routing Destination: host ComponentInfo{com.google.android.gms/com.google.android.gms.pay.hce.service.PayHceService} (Description: Google Wallet) On Host Service Static AID groups: Dynamic AID groups: Category: other AID: 4F53452E5641532E3031 AID: A000000476D0000111 Settings Activity: null Routing Destination: host Service State: ENABLING ComponentInfo{com.google.android.gms/com.google.android.gms.dck.service.DckNfcApduService} (Description: Android Digital Car Key) On Host Service Static AID groups: Category: other AID: A000000809434343444B467631 Dynamic AID groups: Settings Activity: null Routing Destination: host Service State: ENABLING
apduservice.xml配置
<?xml version="1.0" encoding="utf-8"?> <host-apdu-service xmlns:android="http://schemas.android.com/apk/res/android" android:requireDeviceUnlock="false" android:description="@string/service_description"> <aid-group android:category="other" android:description="@string/aid_group_description"> <aid-filter android:name="00000000000000"/> </aid-group> </host-apdu-service>
MyHostApduService代码
public class MyHostApduService extends HostApduService { private static final String TAG = "MyHostApduService"; private static final String CHANNEL_ID = "my_channel_id"; @Override public void onCreate() { super.onCreate(); createNotificationChannel(); // Create the notification channel startForeground(1, createNotification()); // Start the service in foreground Log.d(TAG, "HostApduService created"); } @Override public byte[] processCommandApdu(byte[] commandApdu, Bundle extras) { Log.d(TAG, "Command APDU received: " + bytesToHex(commandApdu)); if (commandApdu == null || commandApdu.length == 0) { Log.d(TAG, "Command APDU is null or empty"); return getUnknownCommandResponse(); } // Handle your APDU commands here return hexStringToByteArray("9000"); // Success status word } @Override public void onDeactivated(int reason) { Log.d(TAG, "Service deactivated with reason: " + reason); } private void createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel serviceChannel = new NotificationChannel( CHANNEL_ID, "Foreground Service Channel", NotificationManager.IMPORTANCE_DEFAULT ); NotificationManager manager = getSystemService(NotificationManager.class); if (manager != null) { manager.createNotificationChannel(serviceChannel); } } } private Notification createNotification() { Intent notificationIntent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE); return new NotificationCompat.Builder(this, CHANNEL_ID) .setContentTitle("Host APDU Service") .setContentText("Service is running in foreground") .setSmallIcon(R.drawable.ic_notification) .setContentIntent(pendingIntent) .build(); } private byte[] getUnknownCommandResponse() { Log.d(TAG, "Received unknown command"); return hexStringToByteArray("6F00"); // Unknown command response } //... }
Manifest配置
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.CardEmulator"> <activity android:name=".MainActivity" android:exported="true" android:label="@string/app_name" android:theme="@style/Theme.CardEmulator"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".MyHostApduService" android:permission="android.permission.BIND_NFC_SERVICE" android:exported="true"> <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> </application> <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/> </manifest>
补充说明:已在两台不同设备测试,非接触式支付选项未设置任何特定服务为默认值,尝试通过前台服务运行该服务。
问题分析与解决建议
从dumpsys nfc输出可以看到,你的HCE服务没有出现在已注册服务列表中,这是导致6F00响应的核心原因,以下是具体排查点和修复建议:
1. AID格式问题
apduservice.xml中使用的全零AID(00000000000000)可能触发系统特殊路由逻辑,部分读卡器也无法识别。建议更换为符合规范的测试AID,比如F0000000000001。
2. 服务注册配置优化
- 确认
strings.xml中存在service_description和aid_group_description的字符串定义,系统可能因缺少描述忽略服务注册。 - 在
host-apdu-service标签中添加android:priority="1000",提升服务优先级,确保系统优先匹配你的服务。修改后的apduservice.xml示例:
<?xml version="1.0" encoding="utf-8"?> <host-apdu-service xmlns:android="http://schemas.android.com/apk/res/android" android:requireDeviceUnlock="false" android:description="@string/service_description" android:priority="1000"> <aid-group android:category="other" android:description="@string/aid_group_description"> <aid-filter android:name="F0000000000001"/> </aid-group> </host-apdu-service>
3. 系统设置验证
进入设备NFC设置,查看“非接触式支付”或“HCE服务”列表,若你的应用不在列表中,说明注册失败,需重新检查Manifest和apduservice.xml的配置正确性。
4. 日志验证
检查MyHostApduService的onCreate日志是否打印,若未打印,说明服务从未被启动,进一步确认注册失败。
5. 重新安装应用
修改配置后,卸载旧应用并重新安装,确保系统重新读取服务配置。
内容的提问来源于stack exchange,提问作者Diogo Mizusaki
相关产品推荐
相关产品推荐

