Android 使用 Arduino MFRC522 进行 NFC 卡仿真

Android NFC Card Emulation With Arduino MFRC522

我正在开发一个 android 应用程序来创建 NFC 卡 ID。我可以通过 phone 从扫描的 NFC 卡中获取信息。但我无法用我的应用程序创建它们。首先,我尝试使用 this method 来做到这一点。然后,我意识到需要有 Tag 对象并尝试以不同的方式保存它。虽然没用。

其次,我找到了this from android documentations. Then, I tried to do with HostNfcFService and HostApduService。为了使用它们,我创建了 XML 文件和服务 类,如下所示;

nfcfservice.xml:

<!--HostNfcFService XML-->
<host-nfcf-service xmlns:android="http://schemas.android.com/apk/res/android"
android:description="NFCService">
<system-code-filter android:name="4000"/>
<nfcid2-filter android:name="02FE000000000000"/>
<t3tPmm-filter android:name="FFFFFFFFFFFFFFFF"/>
</host-nfcf-service>
<!--HostNfcFService XML-->

apduservice.xml:

<!--HostApduService XML-->
<?xml version="1.0" encoding="utf-8"?>
<host-apdu-service xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/servicedesc"
android:requireDeviceUnlock="false">
<aid-group android:description="@string/servicedesc"
    android:category="other">
    <aid-filter android:name="A0000002471001"/>
</aid-group>
</host-apdu-service>
<!--HostApduService XML-->

HostCardEmulatorService.java:

public class HostCardEmulatorService extends HostApduService {
    private static HostCardEmulatorService.ScannedByRemoteListener listener;

    @Override
    public byte[] processCommandApdu(byte[] commandApdu, Bundle extras) {
        byte[] response = null;
        if(listener != null)
        {
            response = listener.onScannedByRemote();
        }
        return response;
    }

    @Override
    public void onDeactivated(int reason) {

    }

    public static void  setOnScannedByRemoteListener(HostCardEmulatorService.ScannedByRemoteListener listener)
    {
        HostCardEmulatorService.listener = listener;
    }

    public interface ScannedByRemoteListener
    {
        public byte[] onScannedByRemote();
    }
}

MyHostNfcFService.java:

public class MyHostNfcFService extends HostNfcFService {

    private static ScannedByRemoteListener listener;


    @Override
    public byte[] processNfcFPacket(byte[] commandPacket, Bundle extras) {
        byte[] response = null;
        if(listener != null)
        {
            response = listener.onScannedByRemote();
        }
        return response;
    }

    @Override
    public void onDeactivated(int reason) {

    }

    public static void  setOnScannedByRemoteListener(ScannedByRemoteListener listener)
    {
        MyHostNfcFService.listener = listener;
    }

    public interface ScannedByRemoteListener
    {
        public byte[] onScannedByRemote();
    }
}

我在清单文件中定义了两种方式,如下所示;

<manifest>

<uses-permission android:name="android.permission.NFC" />

<uses-feature
    android:name="android.hardware.nfc"
    android:required="true" />
...
<application>
...
<service
        android:name=".HostCardEmulatorService"
        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>
    </application>
</manifest>

NfcFService同理。它们之间的唯一区别是“apdu”更名为“nfcf”。但仍然无法同时工作。我检查以下链接以学习一些东西。

https://medium.com/the-almanac/how-to-build-a-simple-smart-card-emulator-reader-for-android-7975fae4040f

Android Host Card Emulation with Arduino

NFC - Help to exchange data between RC522 & Android HCE

但是我完全没看懂。所以,我只是做了我在那里看到的。当我尝试用 reader 扫描我的 phone 时,我认为这些服务之一 运行 是它的“processCommand”方法。但是没有任何反应。

有人知道怎么做吗? XML 文件中的名称有什么特殊含义吗?因为我看到某处有 3 个不同名称的“辅助过滤器”。我错过了什么?

注意 1:我使用 Arduino Pro Mini 和 MFRC522 RFID reader 进行测试。我尝试通过 MFRC522 扫描从我的应用创建的 NFC ID。我必须使用另一个模块来解决这个问题吗?

注意 2:我在服务 类 中使用侦听器从 activity 获取 NFC ID。

我认为你的问题是 MFRC522 RFID reader 是一个非常 basic/old 的设计,实际上只支持 MIFARE 类型的卡(这是非标准化的原始卡格式)。虽然主机卡模拟使用 ISO/IEC 14443A(大部分)作为基础,但它使用其他更高级别的协议来模拟 NFC 类型 4 卡,该卡 reader.

不支持

如果您查看 reader https://github.com/miguelbalboa/rfid#troubleshooting

的 Arduino 库

My mobile phone doesn't recognize the MFRC522 or my MFRC522 can't read data from other MFRC522
Card simulation is not supported.
Communication with mobile phones is not supported.
Peer to peer communication is not supported.

正如图书馆所建议的那样,如果您想要更多功能,请改用基于 PN532 的模块。

如果您查看 PN532 的 Arduino 库 https://github.com/Seeed-Studio/PN532#Features

Communicate with android 4.0+(Lists of devices supported)