java如何在NFC天线上用智能手机模拟NFC标签(非卡)
我使用一个小型的NFC天线,连接在带有USB接口的PC上,通过标准输出(如键盘)传输读取数据。所以我放了一个小标签(MIFARE Ultralight),我在控制台上收到标签id
我希望用我的智能手机(三星S4)做同样的事情。用于标签模拟,而不是卡模拟
如何做到这一点(java、Android Studio)?我找到了用于卡模拟的示例,但没有找到用于标记的示例
我是否需要先转储标记内容(NdefMessage和NdefRecord)才能发回相同的内容
哪个java示例专用于此特定行为
我发现了一个小样本http://www.frugalprototype.com/安卓-nfc-hce/#comment-22,它似乎有效。。。但不是我
我的NFC天线触发“嘟嘟声”,并在控制台上显示随机id值,但我从未进入processCommandApdu()
方法。错在哪里
主要活动。爪哇:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("MainActivity",">>> onCreate...");
setContentView(R.layout.activity_main);
}
}
反应性。爪哇:
public class HceService extends HostApduService {
/* Il s’agit en réalité ici de la commande SELECT AID + la taille en octet de l’AID + l’AID.
Dans une application en production, il est préférable de déclarer la commande
SELECT AID = {0x00, (byte) 0xA4, 0x04,0x00}
et de déclarer l’AID séparément : AID = {0xF0, 0x46, 0x52, 0x55, 0x47, 0x41, 0x4c}.
Sans oublier d’ajouter la taille de l’AID à la commande SELECT AID*/
private static final byte[] SELECT_AID = {0x00,
(byte) 0xA4, 0x04,0x00,0x07,
(byte) 0xF0, 0x46, 0x52, 0x55, 0x47, 0x41, 0x4C};
private static final byte[] MY_UID = {0x01, 0x02, 0x03, 0x04, (byte) 0x90, 0x00};
private static final byte[] MY_ERROR = {0x6F, 0x00};
@Override
public byte[] processCommandApdu(byte[] apdu, Bundle extras) {
Log.d("HceService",">>> processCommandApdu...");
if (Arrays.equals(SELECT_AID, apdu)) {
return MY_UID;
} else return MY_ERROR;
}
AndroidManifest。xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
package="com.example.pdf.cardemulation">
<!-- Demander la permission la permission à l'utilisateur pour l'utilisation du NFC -->
<uses-feature
安卓:name="安卓.hardware.nfc.hce"
安卓:required="true" />
<uses-permission
安卓:name="安卓.permission.NFC" />
<application
安卓:allowBackup="true"
安卓:icon="@mipmap/ic_launcher"
安卓:label="@string/app_name"
安卓:supportsRtl="true"
安卓:theme="@style/AppTheme">
<activity 安卓:name=".MainActivity">
<intent-filter>
<action 安卓:name="安卓.intent.action.MAIN" />
<category 安卓:name="安卓.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Déclaration de notre service HceService -->
<service
安卓:name=".HceService"
安卓:exported="true"
安卓:permission="安卓.permission.BIND_NFC_SERVICE" >
<intent-filter>
<action
安卓:name="安卓.nfc.cardemulation.action.HOST_APDU_SERVICE" />
</intent-filter>
<!-- Fichier dans lequel est déclaré l’AID. Nous devons créer un dossier xml dans le dossier values
et créer un fichier apduservice.xml dans lequel nous allons déclarer l’AID de notre application -->
<meta-data
安卓:name="安卓.nfc.cardemulation.host_apdu_service"
安卓:resource="@xml/apduservice" />
</service>
</application>
</manifest>
最后:
<host-apdu-service xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:description="@string/app_name"
安卓:requireDeviceUnlock="false">
<aid-group
安卓:description="@string/membership_number"
安卓:category="other">
<aid-filter 安卓:name="F046525547414c"/>
</aid-group>
</host-apdu-service>
# 1 楼答案
标签和卡片是一样的
标签不能向读卡器(例如电话设备)发送数据。读卡器向标签发送APDU命令。标记为读卡器提供对命令的响应
也许你的Mifare Ultralight是
NDEF
格式的。NDEF定义了交换命令和响应的协议,以及数据如何存储在标签上你可以在安卓系统上用
HCE
(主机卡模拟)复制同样的功能更新
你为什么有随机的UID? 请看一下文档https://developer.android.com/guide/topics/connectivity/nfc/hce.html#ProtocolParams