RedDeath
RedDeath

Reputation: 322

MifareClassic connection fails NFC

This is part of my code:

@Override
    protected void onResume() {
        super.onResume();
        adaptadorNFC = NfcAdapter.getDefaultAdapter(this);
        final Intent intent = new Intent(this.getApplicationContext(), this.getClass());
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        final PendingIntent pendingIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, intent, 0);
        adaptadorNFC.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techListsArray);    
        read(this.getIntent());
    }

@Override
    protected void onPause() {
        adaptadorNFC.disableForegroundDispatch(this);
        super.onPause();
    }


private void read(Intent intent)
    {
        Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        MifareClassic mifare = MifareClassic.get(tagFromIntent);
        try {
            mifare.connect();
            System.out.println("success connection");        
        } catch (IOException e) {
            System.out.println("Error");
        } finally {
            if (mifare != null) {
                try {
                    mifare.close();
                }
                catch (IOException e) {
                    System.out.println("Error");
                }
            }
        }

}

But when I reach the mifare.connect() it falls into the IOException. The exception message is null, so I don't have more information. I apreciate very much your help. Everything else goes fine. Only fails when I am going to connect. The Tag I am using is efectivelly Mifare Classic 1K. I am able to read it's ID, but can't connect.

Upvotes: 1

Views: 1376

Answers (2)

RedDeath
RedDeath

Reputation: 322

the code is ok. My cel reads mifare classic. The problem was that I am sending many Toasts (wich stops the thread until it finishes showing the message), and when I am passing the mifare, I take it away from the cell before it reaches the connect method. Thanks @ThomasRS for the code to detect if the device supports Mifare Classic

Upvotes: 2

ThomasRS
ThomasRS

Reputation: 8287

Not all Android devices support Mifare Classic cards - this is not a fully standard card and thus some chip manufacturers do not support it.

In your activity, check this using

    private boolean hasMifareClassic() {
            return getPackageManager().hasSystemFeature("com.nxp.mifare");
    }

See this abstract activity for a full example.

Upvotes: 1

Related Questions