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