Android 如何使用基于主机的卡模拟(HCE)将移动设备设置为NFC标签

Android 如何使用基于主机的卡模拟(HCE)将移动设备设置为NFC标签,android,nfc,uniqueidentifier,apdu,hce,Android,Nfc,Uniqueidentifier,Apdu,Hce,我想写一个程序,可以存储一个唯一的ID,然后将它作为NFC标签发送到另一个NFC阅读器设备。如何将代码(nfcTag)发送到NFC阅读器?我可以在没有申请帮助的情况下这样做吗 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

我想写一个程序,可以存储一个唯一的ID,然后将它作为NFC标签发送到另一个NFC阅读器设备。如何将代码(
nfcTag
)发送到NFC阅读器?我可以在没有申请帮助的情况下这样做吗

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,您首先需要创建一个服务组件(这是一个单独的类,不是您活动的一部分):

智能卡读卡器(实际上是通过它们访问卡的软件)使用ISO/IEC 7816-4 APDU命令序列与(模拟的)非接触式智能卡通信。因此,您的HCE服务需要处理这些APDU并为读者创建适当的响应。此命令序列将至少包括用于选择HCE服务的SELECT(by AID)命令。您应该使用状态字
9000
(即
新字节[]{(字节)0x90,(字节)0x00}
)和可选的文件控制信息结构来响应该SELECT命令

您还需要为您的HCE服务定义一个辅助过滤器:

<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>
将AID
F0010203040506
替换为读卡器(软件)用于访问(模拟)智能卡上应用程序的任何内容

您在Android HCE上不能做的事情:

  • 模拟特定的防冲突标识符(UID)
  • 选择射频信令协议(可以是NFC-A或NFC-B)
  • 与未发送ISO SELECT(通过AID/DF名称)命令的读卡器通信

“@Override public byte[]processCommandApdu(b..”,在onCreate()中,这甚至不会编译。@greenapps:是:(我不知道该怎么办。我在许多网站上读到了有关HCE的内容,但我不明白如何编译我的项目。我想将字符串发送给HCE阅读器。我应该用字符串初始化哪个变量?(我是否应该初始化从
processCommandApdu
返回的字节[]以及在MainActivity中调用
MyHostApduService
,以将我的字符串发送到HCE读取器?@farshid,这实际上取决于读取器所使用的应用程序级协议。您需要在
processCommandApdu()中实现该协议的卡端。)
方法。
<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>
<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>