我在创建一个简单的 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 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>
我在2个不同的设备上进行了测试,非接触式支付选项默认未设置为任何特定服务。
尝试在前台执行服务。
这可能是因为根据 ISO IEC 7816-4 的“8.2.1.2 应用程序标识符”部分,您使用的 AID 值“00000000000000”是为向后兼容 ISO/IEC 7812-1 而保留的
这可能会导致问题,并且不是正确的使用值。
所有未注册的 AID 均应以“F”开头
因此尝试使用 AID“F0000000000000”作为 apduservice.xml 中的
aid-filter
值