java - CardEmulation 似乎正常,但未接收 byte[]

标签 java android hce

所有日志打印出来都很好,但是事情不工作。请帮忙。

AndroidManifest.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>

主要 Activity

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");
        }
    }
}

Apdu服务

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 != null): true

(comp != null): true

emulationRetVal: true

由于 emulationRetValtrue,ApduService 应该处于 Activity 状态并与 CardEmulation 事物相关联。然而,它不起作用。

我必须做什么才能让 HCE 继续进行?

最佳答案

您没有显示 apduservice.xml,该文件是进行 AID 注册的文件。或者,您应该从应用程序中手动调用 CardEmulation::registerAidsForService

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

关于java - CardEmulation 似乎正常,但未接收 byte[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44017344/

相关文章:

java - 为什么testng要依赖junit?

android - 在 MPAndroidChart 中的 x 轴上的第一个和最后一个标签上添加填充

android - 在 android 中使用 SSL 进行通信

android - 使用 NFC 将文本数据从 Android 发送到 IOS

android - 通过 NFC 将数据从 Android 设备传输到 PC

java - 在服务器上部署springboot项目报错

java - 设置非常大的打开文件限制的副作用?

android - Android Kitkat 4.4 的 Java Card Applet 和基于主机的卡模拟

java - 从 Class<*> 获取对象实例(反射)

android - 如何构建 _just_ libwebcore.so 而不是整个 froyo 源