0

I am developing an android application creating NFC card ID. I can get info from scanned NFC cards by phone. But I can't create them with my app. First, I tried to do that with this method. Then, I realized need to have Tag object and tried to save it in different ways. Although it didn't work.

Secondly, I found this from android documentations. Then, I tried to do with HostNfcFService and HostApduService. To use them I created XML files and service classes like below;

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();
    }
}

I define both ways in the manifest file like below;

<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>

It is the same for NfcFService. The only difference between them is "apdu" is renamed "nfcf". But still not working even both. I examine the following links to learn somethings.

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

But I didn't understand completely. So, I just did what I saw in there. I thought one of these services run its "processCommand" method when I try to scan my phone with a reader. But nothing happens.

Does anyone know how to do this? Does any special mean of names in XML files? Because I saw somewhere has 3 "aid-filter"s with different names. What am I missing?

NOTE1: I make my tests with Arduino Pro Mini and MFRC522 RFID reader. I tried to scan the NFC ID that was created from my app via MFRC522. Do I have to use another module for that problem?

NOTE2: I use listener in service classes to get NFC ID from activity.

Anıl Köksal
  • 23
  • 2
  • 6

1 Answers1

0

I think your problem is that the MFRC522 RFID reader is a very basic/old design and really only supports MIFARE type cards (which are the non standardised original card format). While Host Card emulation uses ISO/IEC 14443A (mostly) as a base it uses other higher level protocols to emulate a NFC Type 4 card which is not supported by that card reader.

If you look at an Arduino library for the reader https://github.com/miguelbalboa/rfid#troubleshooting

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.

As the library suggest if you want more feature use a PN532 based modules instead.

If you look at an Arduino library for the PN532 https://github.com/Seeed-Studio/PN532#Features

Communicate with android 4.0+(Lists of devices supported)

Andrew
  • 8,198
  • 2
  • 15
  • 35