簡體   English   中英

如何通過基於主機的卡仿真(HCE)將移動設備設置為NFC標簽

[英]How to set mobile as NFC tag with host-based card emulation (HCE)

我想編寫一個可以存儲唯一ID的程序,然后將其作為NFC標簽發送到另一個NFC閱讀器設備。 如何將我的代碼( nfcTag )發送到NFC閱讀器? 我可以在沒有應用程序AID的情況下這樣做嗎?

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        PackageManager pm = this.getPackageManager();
        String nfcTag="123456789";                          //the code wants to work as NFC tag
        TextView msg = (TextView) findViewById(R.id.txtShow);
        byte[] t2 = new byte[9];                            //t2 return from processCommandApdu

        t2 = nfcTag.getBytes();

        @Override
        public byte[] processCommandApdu(byte[] apdu, Bundle extras) {
            msg.setText(nfcTag);



            return t2;                  //return nfcTag too read with NFC tag reader devices
        }
        @Override
        public void onDeactivated(int reason) {
            msg.setText("Your phone does not has HCE hardware.");

            if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
                // HCE is not available on the device.
                Toast.makeText(this, "The device does not has HCE hardware.",
                        Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(this, "HCE supported on your device.",
                        Toast.LENGTH_SHORT).show();
            }
        }
    }
}

這是AndroidManifest.xml

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <service android:name=".MyHostApduService" android:exported="true"
            android:permission="android.permission.BIND_NFC_SERVICE">
            <intent-filter>
                <action android:name="android.nfc.cardemulation.action.HOST_APDU_SERVICE"/>
            </intent-filter>
        </service>

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

對於Android HCE,您首先需要創建一個服務組件(這是一個單獨的類,而不是您的活動的一部分):

public class MyHostApduService extends HostApduService {
    @Override
    public byte[] processCommandApdu(byte[] apdu, Bundle extras) {
        // TODO: process the incoming APDU command(s) and give reasonable responses
    }

    @Override
    public void onDeactivated(int reason) {
    }
}

智能卡讀取器(實際上是通過它們訪問卡的軟件)使用ISO / IEC 7816-4 APDU命令序列與(模擬)非接觸式智能卡進行通信。 因此,您的HCE服務需要處理這些APDU並為讀取器創建正確的響應。 該命令序列將至少包含用於選擇HCE服務的SELECT(通過AID)命令。 您應該以狀態字9000 (即new byte[] { (byte)0x90, (byte)0x00 } )和一個可選的文件控制信息結構來響應SELECT命令。

您還需要為您的HCE服務定義AID過濾器:

<service android:name=".MyHostApduService" android:exported="true"
         android:permission="android.permission.BIND_NFC_SERVICE">
    <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>

並在res/xml/apduservice.xml

<host-apdu-service xmlns:android="http://schemas.android.com/apk/res/android"
           android:description="My HCE Service"
           android:requireDeviceUnlock="false">
    <aid-group android:description="My HCE Service" android:category="other">
        <aid-filter android:name="F0010203040506"/>
    </aid-group>
</host-apdu-service>

用您的閱讀器(軟件)用來訪問(模擬)智能卡上的應用程序的內容替換AID F0010203040506

無法使用Android HCE執行以下操作:

  • 模擬特定的防沖突標識符(UID)
  • 選擇RF信令協議(可以是NFC-A或NFC-B)
  • 與不發送ISO SELECT(通過AID / DF名稱)命令的閱讀器進行通信

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM