1 year ago

#385937

test-img

Ashvin solanki

NFC HCE HostApduService processCommandApdu not called

I am new to NFC technology.

I want to implement custom NFC communication, just like sending and receiving specific values over NFC using two devices.

What is the issue ? .

I have two devices

  1. SAM A31
  2. SAM M51

Both supports NFC and HCE. When i am writing something from A31, M51 Device Able to detect tag when app is in the reader mode and able to communicate with A31 HostApduService. That works fine.

But When i am writing something from M51, A31 Device Able to detect tag when app is in the reader mode and but when app connects with TAG using isoDep.connect();, M51 is not getting callback in processCommandApdu when i execute isoDep.transceive(command); and command is failing.

Video : https://www.dropbox.com/s/1xv3yoix221fcep/NFC-Communication.mp4?dl=0

Service Class HostApduService Implementation

public class NfcHceService extends HostApduService 
{
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) 
    {
      return Service.START_STICKY;
    }
    @Override
    public byte[] processCommandApdu(byte[] commandApdu, Bundle extras) 
    {
      // Handle Command and Send Replay
    }

    @Override
    public void onDeactivated(int reason) {
        Toast.makeText(this, "TAG Connection lost because of " + reason, Toast.LENGTH_SHORT).show();
    }
}

AndroidManifest

<uses-feature
    android:name="android.hardware.nfc"
    android:required="true" />
<uses-feature
    android:name="android.hardware.nfc.hce"
    android:required="true" />
<uses-feature
    android:name="android.hardware.nfc.hcef"
    android:required="true" />

<application>
        <service
            android:name=".nfc.NfcHceService"
            android:enabled="true"
            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>


        <activity
            android:launchMode="singleTop"
            android:name=".ui.NfcActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>
            <meta-data
                android:name="android.nfc.action.TECH_DISCOVERED"
                android:resource="@xml/nfc_tech_filter" />
        </activity>
</application>

apduservice.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/aiddescription" android:category="other">
        <aid-filter android:name="D5780000850102"/>
    </aid-group>
</host-apdu-service>

nfc_tech_filter.xml

<?xml version="1.0" encoding="utf-8"?>
<resources  xmlns:android="http://schemas.android.com/apk/res/android">>
    <tech-list>
        <tech>android.nfc.tech.Ndef</tech>
        <tech>android.nfc.tech.NfcA</tech>
        <tech>android.nfc.tech.IsoDep</tech>
    </tech-list>
</resources>

NfcActivity

private void activateReadMode(boolean mode) {
    allowReading = mode;
    if (!nfcAdapter.isEnabled()) {
        showWirelessSettings();
        return;
    }
    if (allowReading) {
        binding.readMode.setText("Disable Reader Mode");
        nfcAdapter.enableReaderMode(this, dataReader, READER_FLAGS, null);
        binding.editText.setVisibility(View.GONE);
        binding.textView.setVisibility(View.VISIBLE);
    } else {
        binding.readMode.setText("Enable  Reader Mode");
        nfcAdapter.disableReaderMode(this);
        binding.editText.setVisibility(View.VISIBLE);
        binding.textView.setVisibility(View.GONE);
    }
}

DataReader

public class DataReader implements NfcAdapter.ReaderCallback 
{
    @Override
    public void onTagDiscovered(Tag tag) 
    {
       IsoDep isoDep = IsoDep.get(tag);
       if (!isoDep.isConnected()) isoDep.connect();
       //Communicate
       isoDep.close();
    }
}

java

android

nfc

hce

0 Answers

Your Answer

Accepted video resources