gpt4 book ai didi

android - 如何使用基于主机的卡仿真 (HCE) 将手机设置为 NFC 标签

转载 作者:行者123 更新时间:2023-11-29 20:38:51 28 4
gpt4 key购买 nike

我想编写一个可以存储唯一 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,您首先需要创建一个服务组件(这是一个单独的类,不属于您的 Activity):

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)
  • 选择射频信号协议(protocol)(可以是 NFC-A 或 NFC-B)
  • 与不发送 ISO SELECT(按 AID/DF 名称)命令的读者交流

关于android - 如何使用基于主机的卡仿真 (HCE) 将手机设置为 NFC 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31179143/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com