如何从android nfc读写mifare经典卡

时间:2019-08-17 07:53:20

标签: android nfc rfid mifare

我想用我的android设备读写Mifare Classic卡,我尝试了很多代码,但无法正常工作,任何人都可以帮助我。我已经尝试过下面的代码来读取mifare标签,但是在连接时出现错误Mifareclassic。

  package com.acyuta.testingnew;

  import androidx.appcompat.app.AppCompatActivity;

  import android.app.PendingIntent;
  import android.content.Intent;
  import android.content.IntentFilter;
  import android.nfc.NfcAdapter;
  import android.nfc.Tag;
  import android.nfc.tech.MifareClassic;
  import android.os.Bundle;
  import android.util.Log;

  import java.io.IOException;

 public class MainActivity extends AppCompatActivity {
PendingIntent mPendingIntent;
NfcAdapter mAdapter;
IntentFilter[] mFilters;
 String[][] mTechLists;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     mAdapter = NfcAdapter.getDefaultAdapter(this);

     mPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, 
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);


    IntentFilter ndef = new 
IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);

    try {
        ndef.addDataType("*/*");
    } catch (IntentFilter.MalformedMimeTypeException e) {
        throw new RuntimeException("fail", e);
    }
    mFilters = new IntentFilter[]{
            ndef,
    };

    // Setup a tech list for all NfcF tags
    mTechLists = new String[][]{new String[] 
   {MifareClassic.class.getName()}};

    Intent intent = getIntent();

    resolveIntent(intent);
  }

   void resolveIntent(Intent intent) {
    // 1) Parse the intent and get the action that triggered this intent
    String action = intent.getAction();
    // 2) Check if it was triggered by a tag discovered interruption.
    if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
        //  3) Get an instance of the TAG from the NfcAdapter
        Tag tagFromIntent = 
    intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        // 4) Get an instance of the Mifare classic card from this TAG 
     intent
        MifareClassic mfc = MifareClassic.get(tagFromIntent);
        byte[] data;

        try {       //  5.1) Connect to card
            mfc.connect();
            boolean auth = false;
            String cardData = null;
            // 5.2) and get the number of sectors this card has..and loop 
      thru these sectors
            int secCount = mfc.getSectorCount();
            int bCount = 0;
            int bIndex = 0;
            for (int j = 0; j < secCount; j++) {
                // 6.1) authenticate the sector
                auth = mfc.authenticateSectorWithKeyA(j, 
            MifareClassic.KEY_DEFAULT);
                if (auth) {
                    // 6.2) In each sector - get the block count
                    bCount = mfc.getBlockCountInSector(j);
                    bIndex = 0;
                    for (int i = 0; i < bCount; i++) {
                        bIndex = mfc.sectorToBlock(j);
                        // 6.3) Read the block
                        data = mfc.readBlock(bIndex);
                        // 7) Convert the data into a string from Hex 
                     format.
                        // Log.i(TAG, getHexString(data, data.length));
                        bIndex++;
                    }
                } else { // Authentication failed - Handle it

                }
            }
        } catch (IOException e) {
            // Log.e(TAG, e.getLocalizedMessage());
            // showAlert(3);
        }
    }// End of method
    }

    @Override
     public void onResume() {
    super.onResume();
    mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, 
  mTechLists);
   }

@Override
public void onNewIntent(Intent intent) {
    Log.i("Foreground dispatch", "Discovered tag with intent: " + intent);
    resolveIntent(intent);
}

@Override
public void onPause() {
    super.onPause();
    mAdapter.disableForegroundDispatch(this);
}

}

从上面的代码中,我得到mfc.connect()错误,请引用源代码以使用android设备读写mifare卡

0 个答案:

没有答案