elif şahin
elif şahin

Reputation: 1

Xamarin.android HCE ProcessCommandApdu is not work

`I get the id of the Mifera classic card with NFC and I aim to make my phone act like this card id using hce. But ProcessCommandApdu is not work.

1-I open NFC and read the Mifera card from my phone 2-I save the card I read. 3-I start the hce service and have the RFID Card reader read my phone But the card I read from my Mifera classic card is not an ID and it does not enter the ProcessCommandApdu method at all`

HceService.cs file


[Service]  
public class HceService : HostApduService
{
private long cardId;

      public void SetCardId(long id)
      {
          cardId = id;
      }
    
      public override void OnDeactivated([GeneratedEnum] DeactivationReason reason)
      {
          throw new NotImplementedException();
      }
    
      public override byte[] ProcessCommandApdu(byte[] apdu, Bundle extras)
      {
          if (apdu[0] == 0x00 && apdu[1] == 0xB0)
          {
              var cardIdBytes = BitConverter.GetBytes(cardId);
              return cardIdBytes.Concat(new byte[] { 0x90, 0x00 }).ToArray();
          }
          return new byte[] { 0x6A, 0x00 };
      }

}

Manifest file

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0"
package="com.companyname.pdksnfc"\>
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="28"/\>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"\>

        <service android:name=".HceService" 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_ervice" android:resource="@xml/apduservice"/>
        </service>
    </application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.BIND_NFC_SERVICE"/>
    <uses-feature android:name="android.hardware.nfc.hce" android:required="true" />
    <uses-permission android:name="android.permission.NFC"/>
    <uses-permission android:name="android.permission.NFC_TRANSACTION_EVENT"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

apduservice.xml file

<?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="F0010203040506"/\>        
<aid-filter android:name="F0394148148100"/\>    
</aid-group\>   
<meta-data android:name="android.nfc.cardemulation.host_apdu_service" android:resource="@xml/apduservice"\> 
</host-apdu-service\> 

MainActivity file

var hceService = new HceService(); hceService.SetCardId(cardId); 
Intent intent = new Intent(this, typeof(HceService)); 
StartService(intent)

I don't understand where the mistake is. I want my phone to emulate card ID from Mifera. Can you please help?

Upvotes: 0

Views: 43

Answers (0)

Related Questions