卡仿真似乎没问题,但未接收字节[]


打印

出的所有日志都很好,但事情不起作用。请帮忙。

安卓清单.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.abc.hcetest">
    <uses-permission android:name="android.permission.NFC" />
    <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/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".services.ApduService"
            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>
    </application>
</manifest>

主活动

public class MainActivity extends AppCompatActivity {
    private static final String LOGTAG = MainActivity.class.toString();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Thread thread = new Thread(){
            public void run(){
                try{sleep(1000l);}catch(Exception e){}
                setPreferredNFCService(true);
            }
        };
        thread.start();
    }

    private void setPreferredNFCService(boolean isSet) {
        Log.d(LOGTAG, "setPreferredNFCService isSet: " + isSet);
        NfcAdapter nfc = NfcAdapter.getDefaultAdapter( this );
        Log.d(LOGTAG, "(nfc != null): " + (nfc != null));
        if ( nfc == null )
            return; //HCEAppError.NFC_NOT_SUPPORT;
        CardEmulation emulation = CardEmulation.getInstance(nfc);
        if(emulation != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                try{
                    if (isSet) {
                        ComponentName comp = new ComponentName(this, ApduService.class);
                        Log.d(LOGTAG, "(comp != null): " + (comp != null));
                        final boolean emulationRetVal = emulation.setPreferredService(this, comp);
                        Log.d(LOGTAG, "emulationRetVal: " + emulationRetVal);
                    } else {
                        emulation.unsetPreferredService(this);
                    }
                }
                catch(Exception e){
                    e.printStackTrace();
                }
            }
            else{
                Log.e(LOGTAG, "unsupported");
            }
        }
        else{
            Log.e(LOGTAG, "card emulation failed");
        }
    }
}

ApduService

public class ApduService extends HostApduService {
    final static String LOGTAG = ApduService.class.getName();
    @Override
    public byte[] processCommandApdu(byte[] commandApdu, Bundle extras) {
        if(commandApdu != null){
            Log.d(LOGTAG, "processCommandApdu " + commandApdu.length);
        }
        else{
            Log.d(LOGTAG, "processCommandApdu " + commandApdu);
        }
        return null;
    }
    private boolean canStartTrans() {
        Log.d(LOGTAG, "in can start trans");
        return true;
    }
    @Override
    public void onDeactivated(int reason) {
        Log.d(LOGTAG, "onDeactivated: " + reason);
    }
}

打印输出为:

setPreferredNFCService isSet: true

(nfc != 空(: 真

(comp != null(:true

emulationRetVal: true

由于emulationRetValtrue的,ApduService应该是活跃的,并与CardEmulation事物相关联。但是,它不起作用。

我必须做些什么才能让 HCE 正常运行?

您没有显示您的apduservice.xml,这是发生 AID 注册的文件。或者,应从应用程序内手动调用CardEmulation::registerAidsForService

确保执行 https://developer.android.com/guide/topics/connectivity/nfc/hce.html 中概述的所有步骤

最新更新